// The fixture of childKillGuard.test.ts: run DETACHED (a process group of its // own), it kills a child whose spawn failed and reports who was signalled — // itself, and a sibling in its group. `guard` or `noguard`, then the case. import { spawn } from "node:child_process"; import os from "node:os"; import path from "node:path"; const [guard, mode] = process.argv.slice(2); if (guard === "guard") await import("./childKillGuard"); const { execa } = await import("execa"); let selfSigterm = 0; process.on("SIGTERM", () => { selfSigterm++; }); let canarySignal: string | null = null; const canary = spawn("sleep", ["30"], { stdio: "ignore" }); // same group as this process canary.on("exit", (_c, s) => { canarySignal = s; }); await new Promise((r) => setTimeout(r, 150)); // A directory that does not exist (nothing ever makes it). const missing = path.join(os.tmpdir(), "archilyzer-child-kill-guard-no-such-dir", "gone"); const notes: Record = {}; if (mode === "spawn-kill") { const c = spawn("sleep", ["5"], { cwd: missing }); c.on("error", () => {}); notes.pid = c.pid ?? null; notes.killed = c.kill("SIGTERM"); } else if (mode === "execa-aborted") { const ac = new AbortController(); ac.abort(); const p = execa("sleep", ["5"], { cwd: missing, cancelSignal: ac.signal, reject: false }); notes.pid = p.pid ?? null; const r = await p; notes.failed = r.failed; } else if (mode === "execa-kill") { const p = execa("sleep", ["5"], { cwd: missing, reject: false }); notes.pid = p.pid ?? null; notes.killed = p.kill("SIGTERM"); await p; } else if (mode === "launcher-abort-after") { // The download launcher over a channel directory that is gone, cancelled in // the same tick it launched. const { runOneYtdlp } = await import("../ytdlp/runOneYtdlp"); const ac = new AbortController(); const run = runOneYtdlp({ ytdlpBin: "sleep", onLog: () => {}, signal: ac.signal }, missing, ["5"]); ac.abort(); notes.outcome = await run; } else if (mode === "launcher-aborted-before") { // Already cancelled when asked to launch: nothing is spawned at all. const { runOneYtdlp } = await import("../ytdlp/runOneYtdlp"); const ac = new AbortController(); ac.abort(); const said: string[] = []; notes.outcome = await runOneYtdlp({ ytdlpBin: "sleep", onLog: (l) => said.push(l), signal: ac.signal }, missing, ["5"]); notes.said = said.length; } else if (mode === "launcher-aborted-by-log") { // Cancelled by the log sink, on the launch line itself. const { runOneYtdlp } = await import("../ytdlp/runOneYtdlp"); const ac = new AbortController(); notes.outcome = await runOneYtdlp({ ytdlpBin: "sleep", onLog: () => ac.abort(), signal: ac.signal }, missing, ["5"]); } else if (mode === "started-child") { // The guard leaves a real child alone: it is killed as before. const c = spawn("sleep", ["30"], { stdio: "ignore" }); await new Promise((r) => setTimeout(r, 100)); notes.killed = c.kill("SIGTERM"); notes.signal = await new Promise((r) => c.on("exit", (_c, s) => r(s))); } await new Promise((r) => setTimeout(r, 400)); const out = { selfSigterm, canarySignal, ...notes }; canary.kill("SIGKILL"); console.log(JSON.stringify(out)); process.exit(0);