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: `.kill(`, `?.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 = /(?.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(`(? { 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)`"); });