// A KILL ON A CHILD THAT NEVER STARTED IS A NO-OP — process-wide. // // Node's ChildProcess.kill() is `this._handle.kill(signal)` with no look at // `this.pid`. When a spawn FAILS (the binary is not there, the cwd is gone) // Node keeps the handle until the next tick, with no pid in it, and a kill in // that window signals pid 0: THE CALLER'S OWN PROCESS GROUP. Verified on Node // 22.23.5 (12 Nov 2026) with a group leader, a missing cwd and // · spawn(...).kill("SIGTERM") in the same tick, // · execa(..., { cancelSignal }) with a signal that is already aborted, // · execa(...).kill("SIGTERM") in the same tick: // in each the caller and a sibling in its group received SIGTERM, and with // the same aborted signal and a cwd that exists, neither did. That is what // took the editor's test server down (and pnpm with it, "Command failed with // signal SIGTERM") whenever a download was cancelled while its channel // directory was being removed — and it would do the same to a live editor. // // execa kills from inside itself (cancelSignal, timeout, forceKillAfterDelay), // so no call site can guard that: the guard is on the prototype. Importing // this module installs it; every module of common/ that spawns with a // `cancelSignal` or calls `.kill()` on a child imports it (the test // lib/childKillGuard.test.ts holds them to it). Idempotent, and per process: // the mark is on the prototype, which Next's two module graphs share. import { ChildProcess } from "node:child_process"; const MARK = Symbol.for("archilyzer.childKillGuard"); type Guarded = { [MARK]?: true; kill: ChildProcess["kill"] }; export function installChildKillGuard(): void { const proto = ChildProcess.prototype as unknown as Guarded; if (proto[MARK]) return; const kill = proto.kill; proto.kill = function guardedKill(this: ChildProcess, signal?: NodeJS.Signals | number): boolean { // No pid: the spawn failed (or has not happened). There is nothing to // signal, and the handle would signal the group. if (this.pid === undefined) return false; return kill.call(this, signal); }; proto[MARK] = true; } installChildKillGuard();