Job log copies drop control characters and survive one bad row

This commit is contained in:
aiongg committed 2026-11-22 15:45:53 +00:00
1 parent 2a7a5a4db1
commit 3b4b04ca10
2 files changed
+45 -3

No files matched your search

+22 -3
View File
@@ -11,6 +11,9 @@ export const JOB_LOG_KEEP_BYTES = 256 * 1024;
/** Chunks are dropped once the kept text passes the cap by this much, so the DELETE runs once per this many bytes, not per copy. */
const DROP_SLACK_BYTES = 64 * 1024;
export const JOB_LOG_FLUSH_MS = 5_000;
/** Postgres text cannot hold NUL; the other C0 controls (bar tab, newline, carriage return) are terminal noise. */
// eslint-disable-next-line no-control-regex
const CONTROLS = /[\u0000-\u0008\u000b\u000c\u000e-\u001f]/g;
/** The bytes of `path` from `from` (an offset, or one computed from the size) to its end; null when the file cannot be read. */
function readRange(path: string, from: number | ((size: number) => number)): { buf: Buffer; start: number; size: number } | null {
@@ -81,7 +84,7 @@ export class JobLogCopy {
buf = buf.subarray(0, nl + 1);
}
if (!buf.length) return null;
return { job_id: this.job, seq: this.#seq + 1, start, next_start: start + buf.length, chunk: scrub(buf.toString("utf8")), at: new Date(nowMs).toISOString() };
return { job_id: this.job, seq: this.#seq + 1, start, next_start: start + buf.length, chunk: scrub(buf.toString("utf8").replace(CONTROLS, "")), at: new Date(nowMs).toISOString() };
}
/** Records a written chunk; the highest seq to drop once the kept text passes the cap, else 0. */
@@ -110,8 +113,24 @@ export function copyLogs(db: Db, copies: JobLogCopy[], final = false): Promise<v
const nowMs = Date.now();
const chunks = copies.flatMap((c) => { const n = c.next(final, nowMs); return n ? [{ copy: c, n }] : []; });
if (!chunks.length) return;
await db.run(db.k.insertInto("job_log_chunks").values(chunks.map(({ n }) => n)));
const drops = chunks.flatMap(({ copy, n }) => { const upTo = copy.wrote(n); return upTo ? [{ job: copy.job, upTo }] : []; });
let stored = chunks;
try {
await db.run(db.k.insertInto("job_log_chunks").values(chunks.map(({ n }) => n)));
} catch (e) {
// One bad row must not block the others: write each job's chunk on its own.
if (chunks.length < 2) throw e;
stored = [];
for (const c of chunks) {
try {
await db.run(db.k.insertInto("job_log_chunks").values(c.n));
stored.push(c);
} catch (err) {
console.error(`job log copy failed for job ${c.copy.job}: ${(err as Error).message}`);
}
}
if (!stored.length) throw e;
}
const drops = stored.flatMap(({ copy, n }) => { const upTo = copy.wrote(n); return upTo ? [{ job: copy.job, upTo }] : []; });
if (drops.length) await db.run(db.k.deleteFrom("job_log_chunks").where((eb) => eb.or(drops.map((d) => eb.and([eb("job_id", "=", d.job), eb("seq", "<=", d.upTo)])))));
});
queue = run.catch(() => { /* the caller sees the failure; the next copy reads the lines again */ });
+23
View File
@@ -46,3 +46,26 @@ test("a long job log keeps its end within the cap, and the job page reads it wit
rmSync(dir, { recursive: true, force: true });
}
});
test("a log chunk with a NUL byte is copied without it, and the next chunk still starts at the right offset", async () => {
const dir = mkdtempSync(join(SCRATCH, "job-log-"));
const db = await connectDb(join(dir, "jobs.db"));
try {
const path = join(dir, "job.log");
const t = new Date().toISOString();
const id = Number((await db.get(db.k.insertInto("jobs").values({ step: "test-echo", params: "{}", requested_by: "boss", requested_at: t, state: "running", started_at: t, log_path: path })
.returning("id")))!.id);
const copy = await JobLogCopy.open(db, id, path);
appendFileSync(path, "a\u0000b\u0007c\td\n");
await copyLogs(db, [copy], true);
appendFileSync(path, "second\n");
await copyLogs(db, [copy], true);
const rows = await db.all(db.k.selectFrom("job_log_chunks").select(["chunk", "start", "next_start"]).where("job_id", "=", id).orderBy("seq"));
assert.equal(rows[0]!.chunk, "abc\td\n");
assert.equal(Number(rows[1]!.start), Number(rows[0]!.next_start));
assert.equal(rows[1]!.chunk, "second\n");
} finally {
await db.close();
rmSync(dir, { recursive: true, force: true });
}
});