Archilyzer · Source

archilyzer

Archilyzer
git clone https://archilyzer.pages.dev/source/archilyzer.git
Log | Files | Refs | README | LICENSE

commit 76e03c2eeeb6e18b80f740cbd25dbc690e85063b
parent 4fc0695269a05ef3c1c536eaba1382385d1e97ee
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date:   Thu, 12 Nov 2026 08:11:04 -0500

umtool, scripts: every kill on a child checks its pid first -- a child that failed to spawn has none, and Node's kill() then signals the caller's own process group; umtool and the standalone scripts never import common's guard, so the queue lock, render-mode's signal forwarder, loudness, voice, tts, diarize-sortformer and e2e-all check it where they call it, and umtool/lib/childKill-sweep.test.mjs (in test:scripts) fails on an unguarded one, member chains included, process.kill(pid) excepted

Co-Authored-By: Claude <noreply@anthropic.com>

Diffstat:
Mscripts/diarize-sortformer.mjs | 7+++++--
Mscripts/e2e-all.mjs | 2+-
Mscripts/queue-lock.mjs | 11++++++-----
Mscripts/tts.mjs | 3++-
Mscripts/voice.mjs | 3++-
Aumtool/lib/childKill-sweep.test.mjs | 79+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mumtool/lib/loudness.ts | 3++-
Mumtool/lib/report/renderMode.mjs | 3++-
8 files changed, 99 insertions(+), 12 deletions(-)

diff --git a/scripts/diarize-sortformer.mjs b/scripts/diarize-sortformer.mjs @@ -124,7 +124,10 @@ engine.on("error", (err) => fail(`failed to run ${bin}: ${err.message}`, 3)); dec.stdout.pipe(engine.stdin); // The engine exiting early (bad model, unusable backend) closes the pipe under // ffmpeg. That is an expected race, not a crash to report. -engine.stdin.on("error", () => dec.kill("SIGTERM")); +engine.stdin.on("error", () => { + // (A child with no pid never started: Node would signal this process's own group — common/lib/childKillGuard.ts.) + if (dec.pid !== undefined) dec.kill("SIGTERM"); +}); let stdout = ""; engine.stdout.setEncoding("utf8"); @@ -164,6 +167,6 @@ engine.on("close", (code) => { // waiting for that close. Three of these accumulated during development, each // holding a decoder resident for hours. The engine's exit is therefore the // signal to tear the decoder down rather than wait for an EOF that cannot come. - if (dec.exitCode === null && !dec.killed) dec.kill("SIGKILL"); + if (dec.pid !== undefined && dec.exitCode === null && !dec.killed) dec.kill("SIGKILL"); finish(); }); diff --git a/scripts/e2e-all.mjs b/scripts/e2e-all.mjs @@ -236,7 +236,7 @@ async function main(argv) { process.on(sig, () => { for (const c of children) { try { - c.kill(sig); + if (c.pid !== undefined) c.kill(sig); } catch { /* already gone */ } diff --git a/scripts/queue-lock.mjs b/scripts/queue-lock.mjs @@ -425,7 +425,8 @@ function release(holder) { if (holder.exitCode != null || holder.signalCode != null) return resolve(); const hard = setTimeout(() => { try { - holder.kill("SIGKILL"); + // (A child with no pid never started: Node would signal this process's own group — common/lib/childKillGuard.ts.) + if (holder.pid !== undefined) holder.kill("SIGKILL"); } catch { /* already gone */ } @@ -438,7 +439,7 @@ function release(holder) { holder.stdin.end(); } catch { try { - holder.kill("SIGKILL"); + if (holder.pid !== undefined) holder.kill("SIGKILL"); } catch { /* already gone */ } @@ -589,7 +590,7 @@ function installSignalHandlers() { if (signalHits === 1) { if (activeForwardTerm && sig !== "SIGINT") { try { - child?.kill(sig); + if (child?.pid !== undefined) child.kill(sig); } catch { /* already gone */ } @@ -606,7 +607,7 @@ function installSignalHandlers() { } process.stderr.write("queue-lock: forcing shutdown\n"); try { - child?.kill("SIGKILL"); + if (child?.pid !== undefined) child.kill("SIGKILL"); } catch { /* already gone */ } @@ -698,7 +699,7 @@ async function holdLock(lock, name, cmd, timeoutMs, what, mode = {}) { const cleanup = () => { dropHolderFile(); try { - holder.kill("SIGKILL"); + if (holder.pid !== undefined) holder.kill("SIGKILL"); } catch { /* already gone */ } diff --git a/scripts/tts.mjs b/scripts/tts.mjs @@ -113,7 +113,8 @@ async function cleanup() { for (const sig of ["SIGTERM", "SIGINT"]) { process.on(sig, () => { - child?.kill("SIGTERM"); + // (A child with no pid never started: Node would signal this process's own group — common/lib/childKillGuard.ts.) + if (child?.pid !== undefined) child.kill("SIGTERM"); void cleanup().finally(() => process.exit(143)); }); } diff --git a/scripts/voice.mjs b/scripts/voice.mjs @@ -124,7 +124,8 @@ let child = null; const cleanup = () => rm(work, { recursive: true, force: true }).catch(() => {}); for (const sig of ["SIGTERM", "SIGINT"]) { process.on(sig, () => { - child?.kill("SIGTERM"); + // (A child with no pid never started: Node would signal this process's own group — common/lib/childKillGuard.ts.) + if (child?.pid !== undefined) child.kill("SIGTERM"); void cleanup().finally(() => process.exit(143)); }); } diff --git a/umtool/lib/childKill-sweep.test.mjs b/umtool/lib/childKill-sweep.test.mjs @@ -0,0 +1,79 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { readdirSync, readFileSync } from "node:fs"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; + +// Run with: node --test umtool/lib/childKill-sweep.test.mjs (part of `pnpm run test:scripts`) +// +// A KILL ON A CHILD THAT NEVER STARTED SIGNALS THE CALLER'S OWN PROCESS GROUP +// (common/lib/childKillGuard.ts: Node's kill does not look at the pid). common/ +// installs a guard on the prototype; umtool and the standalone scripts are +// processes of their own that never import it, so every `.kill()` on a child +// there checks the pid where it is called. This holds each one to it. + +const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.."); +const SKIP_DIRS = new Set(["node_modules", ".next", ".next-e2e-start", "e2e", "test-results", ".e2e-song", "public", "assets"]); + +function sources(dir, out = []) { + for (const e of readdirSync(dir, { withFileTypes: true })) { + if (e.name.startsWith(".") || SKIP_DIRS.has(e.name)) continue; + const p = path.join(dir, e.name); + if (e.isDirectory()) sources(p, out); + else if (/\.(mjs|cjs|js|ts|tsx|mts)$/.test(e.name) && !/\.test\.|\.d\.ts$/.test(e.name)) out.push(p); + } + return out; +} + +// A kill of a child: `<name>.kill(`, `<name>?.kill(`, or on a member chain +// (`this.child.kill(`, `job.child?.kill(`) — never `process.kill(pid)`, which +// takes a pid and refuses an undefined one. +const KILL = /(?<![\w$.?])([A-Za-z_$][\w$]*(?:\??\.[A-Za-z_$][\w$]*)*)\??\.kill\(/g; + +// Each kill on the line whose receiver has no `<receiver>.pid !== undefined` +// (or `?.pid`) on the same line. +function unguardedKills(line) { + const out = []; + for (const m of line.matchAll(KILL)) { + const receiver = m[1]; + if (receiver === "process") continue; + const chain = receiver + .split(/\??\./) + .map((part) => part.replace(/[$]/g, "\\$&")) + .join("\\??\\."); + out.push({ receiver, guarded: new RegExp(`(?<![\\w$])${chain}\\??\\.pid !== undefined`).test(line) }); + } + return out; +} + +test("the sweep's own matcher: a child's kill is found, a guarded one passes, process.kill is not a child's", () => { + const verdict = (line) => unguardedKills(line).map((k) => `${k.receiver}:${k.guarded}`); + assert.deepEqual(verdict("child.kill(sig);"), ["child:false"]); + assert.deepEqual(verdict("child?.kill('SIGTERM');"), ["child:false"]); + assert.deepEqual(verdict("this.child.kill();"), ["this.child:false"]); + assert.deepEqual(verdict("job.child?.kill();"), ["job.child:false"]); + assert.deepEqual(verdict("if (child.pid !== undefined) child.kill(sig);"), ["child:true"]); + assert.deepEqual(verdict("if (child?.pid !== undefined) child.kill(sig);"), ["child:true"]); + assert.deepEqual(verdict("if (job.child?.pid !== undefined) job.child.kill();"), ["job.child:true"]); + // Another child's pid is not this one's check. + assert.deepEqual(verdict("if (other.pid !== undefined) child.kill(sig);"), ["child:false"]); + assert.deepEqual(verdict("if (mychild.pid !== undefined) child.kill(sig);"), ["child:false"]); + assert.deepEqual(verdict("process.kill(pid, 'SIGTERM'); process.kill(-pgid);"), []); +}); + +test("umtool and the scripts check a child's pid at every kill", () => { + const unguarded = []; + let kills = 0; + for (const file of [...sources(path.join(ROOT, "umtool")), ...sources(path.join(ROOT, "scripts"))]) { + const lines = readFileSync(file, "utf8").split("\n"); + lines.forEach((line, i) => { + if (/^\s*(\/\/|\*|\/\*)/.test(line)) return; + for (const k of unguardedKills(line)) { + kills++; + if (!k.guarded) unguarded.push(`${path.relative(ROOT, file)}:${i + 1}: ${line.trim()}`); + } + }); + } + assert.ok(kills >= 10, `the sweep found the kills (${kills})`); + assert.deepEqual(unguarded, [], "check the pid on the line of the kill: `if (child.pid !== undefined) child.kill(sig)`"); +}); diff --git a/umtool/lib/loudness.ts b/umtool/lib/loudness.ts @@ -92,7 +92,8 @@ async function runEbur128(abs: string): Promise<{ stderr: string; tookMs: number err += b.toString(); }); const timer = setTimeout(() => { - ff.kill("SIGKILL"); + // (A child with no pid never started: Node would signal this process's own group — common/lib/childKillGuard.ts.) + if (ff.pid !== undefined) ff.kill("SIGKILL"); reject(new Error(`ebur128 on ${labelFor(abs)} took longer than ${MEASURE_TIMEOUT_MS / 60000} minutes`)); }, MEASURE_TIMEOUT_MS); ff.on("error", (e) => { diff --git a/umtool/lib/report/renderMode.mjs b/umtool/lib/report/renderMode.mjs @@ -308,7 +308,8 @@ async function renderModeChain(cmd, o) { say(` ${RENDER_MODE_REMINDER}`); }, onSignal: (sig) => { - child.kill(sig); + // (A child with no pid never started: Node would signal this process's own group — common/lib/childKillGuard.ts.) + if (child.pid !== undefined) child.kill(sig); }, }, () => exited,