diff --git a/src/process/ExecFileOutput.test.ts b/src/process/ExecFileOutput.test.ts index 8d386e85e9..2ad6cc9a6b 100644 --- a/src/process/ExecFileOutput.test.ts +++ b/src/process/ExecFileOutput.test.ts @@ -502,8 +502,10 @@ describe.skipIf(process.platform === "win32")( const release = join(directory, "release"); const finished = join(directory, "finished.json"); await mkdir(scratch); + // The stop signal reaches the whole group; this descendant outlives it. const childSource = [ 'const fs = require("node:fs");', + 'process.on("SIGTERM", () => {});', `fs.writeFileSync(${JSON.stringify(ready)}, String(process.pid));`, "setInterval(() => {", ` if (!fs.existsSync(${JSON.stringify(release)})) return;`, @@ -635,6 +637,110 @@ describe.skipIf(process.platform === "win32")( }, ); +describe.skipIf(process.platform === "win32")( + "execFileOutput process group", + () => { + it("stops a running launcher's descendants with it", async () => { + const directory = await createTestTempDirectory("rea-exec-group-stop-"); + const ready = join(directory, "descendant.pid"); + const marker = join(directory, "descendant-marker"); + const childSource = [ + 'const fs = require("node:fs");', + `fs.writeFileSync(${JSON.stringify(ready)}, String(process.pid));`, + `setInterval(() => fs.existsSync(${JSON.stringify(marker)}), 10);`, + ].join("\n"); + const script = [ + 'const { spawn } = require("node:child_process");', + `spawn(process.execPath, ["-e", ${JSON.stringify(childSource)}], { stdio: "inherit" });`, + "setInterval(() => {}, 1000);", + ].join("\n"); + const controller = new AbortController(); + const execution = execFileOutput(process.execPath, ["-e", script], { + stopSignal: "SIGTERM", + signal: controller.signal, + }).catch((cause: unknown) => cause); + let killFailure: unknown; + try { + expect(await waitForFile(ready)).toBe(true); + controller.abort("caller stopped"); + let timer: NodeJS.Timeout | undefined; + const outcome = await Promise.race([ + execution, + new Promise((resolve) => { + timer = setTimeout( + () => resolve("descendant kept the pipes"), + 4_000, + ); + }), + ]).finally(() => clearTimeout(timer)); + expect(outcome).toMatchObject({ + _tag: "ExecFileOutputCancelledError", + cause: "caller stopped", + }); + } finally { + controller.abort("test cleanup"); + const pid = Number(await readFile(ready, "utf8").catch(() => "")); + killFailure = await stopOwnedDescendant(pid, marker); + await execution; + } + if (killFailure !== undefined) throw killFailure; + }); + + it("escalates to the group when the launcher and its worker ignore the stop signal", async () => { + const directory = await createTestTempDirectory( + "rea-exec-group-escalate-", + ); + const ready = join(directory, "descendant.pid"); + const marker = join(directory, "descendant-marker"); + const ignoreStop = + 'process.on("SIGTERM", () => {}); setInterval(() => {}, 1000);'; + const childSource = [ + 'const fs = require("node:fs");', + ignoreStop, + `fs.writeFileSync(${JSON.stringify(ready)}, String(process.pid));`, + `setInterval(() => fs.existsSync(${JSON.stringify(marker)}), 10);`, + ].join("\n"); + const script = [ + 'const { spawn } = require("node:child_process");', + ignoreStop, + `spawn(process.execPath, ["-e", ${JSON.stringify(childSource)}], { stdio: "inherit" });`, + ].join("\n"); + vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] }); + const controller = new AbortController(); + const execution = execFileOutput(process.execPath, ["-e", script], { + stopSignal: "SIGTERM", + signal: controller.signal, + }).catch((cause: unknown) => cause); + let killFailure: unknown; + try { + expect(await waitForFile(ready)).toBe(true); + controller.abort("caller stopped"); + // Both processes ignore SIGTERM; only the grace SIGKILL ends them. + await vi.advanceTimersByTimeAsync(5_000); + vi.useRealTimers(); + let timer: NodeJS.Timeout | undefined; + const outcome = await Promise.race([ + execution, + new Promise((resolve) => { + timer = setTimeout(() => resolve("worker kept the pipes"), 4_000); + }), + ]).finally(() => clearTimeout(timer)); + expect(outcome).toMatchObject({ + _tag: "ExecFileOutputCancelledError", + cause: "caller stopped", + }); + } finally { + vi.useRealTimers(); + controller.abort("test cleanup"); + const pid = Number(await readFile(ready, "utf8").catch(() => "")); + killFailure = await stopOwnedDescendant(pid, marker); + await execution; + } + if (killFailure !== undefined) throw killFailure; + }); + }, +); + describe("execFileOutput failure metadata", () => { it("preserves raw byte counts on a failed command with invalid UTF-8", async () => { const failure: unknown = await execFileOutput(process.execPath, [ diff --git a/src/process/ExecFileOutput.ts b/src/process/ExecFileOutput.ts index 09563bd531..0ca2d3a219 100644 --- a/src/process/ExecFileOutput.ts +++ b/src/process/ExecFileOutput.ts @@ -11,13 +11,17 @@ type ExecFileOutputOptions = Omit< > & { readonly maxBuffer?: number; /** - * Signal that stops the process on abort or timeout. The promise then - * settles only after that process's stdio pipes close, so a caller can - * remove files the command wrote. A descendant that inherited those pipes - * keeps them open until it exits; destroying the readers is not - * process-family completion and must not unblock this settlement. A process - * still running after a grace period is killed. Without this signal, Node's - * execFile sends SIGTERM on abort and rejects before the process exits. + * Signal that stops the process on abort or timeout. On POSIX the command + * leads its own process group, and the stop signal goes to that group + * while its leader is unreaped, so launcher scripts and their workers stop + * together. The promise then settles only after the stdio pipes close, so + * a caller can remove files the command wrote. A descendant that inherited + * those pipes keeps them open until it exits; destroying the readers is not + * process-family completion and must not unblock this settlement. A group + * whose leader is still running after a grace period is killed. Once the + * leader has been reaped its PID no longer identifies the group, so no + * further group signal is sent. Without this signal, Node's execFile sends + * SIGTERM to the direct child on abort and rejects before it exits. */ readonly stopSignal?: NodeJS.Signals; /** Wait for inherited copies of an extra drained descriptor to close. Requires stopSignal. */ @@ -296,6 +300,9 @@ const startStoppableCommand = ( ? ["ignore", "pipe", "pipe", "pipe"] : ["ignore", "pipe", "pipe"], killSignal: stopSignal, + // POSIX detach starts a new session, so the command leads a process group + // REA can stop as one family. Windows detach would only open a console. + detached: process.platform !== "win32", }); child.on("error", (error: Error) => { processError ??= error; @@ -307,12 +314,31 @@ const startStoppableCommand = ( const pipe = stdio?.[3]; if (pipe != null && "resume" in pipe) pipe.resume(); } + let signalled = false; + /** + * Signal the command's process group while its leader is unreaped. Node + * sets exitCode or signalCode in the same callback that reaps the leader, + * so until then the leader's PID still names the group this call created. + */ + const signalFamily = (signalName: NodeJS.Signals): void => { + if (child.exitCode !== null || child.signalCode !== null) return; + const pid = child.pid; + if (process.platform !== "win32" && pid !== undefined) + try { + process.kill(-pid, signalName); + signalled = true; + return; + } catch { + // The leader left its group; it is still the direct child. + } + if (child.kill(signalName)) signalled = true; + }; // The first stop request names the failure; a later one is not resent. const stop = (reason: "abort" | "timeout" | "output-limit"): void => { if (stopped !== undefined || rootFailed || processError !== null) return; stopped = reason; - child.kill(stopSignal); - grace = setTimeout(() => child.kill("SIGKILL"), STOP_GRACE_MS); + signalFamily(stopSignal); + grace = setTimeout(() => signalFamily("SIGKILL"), STOP_GRACE_MS); }; const onAbort = (): void => { stop("abort"); @@ -362,7 +388,7 @@ const startStoppableCommand = ( ); Reflect.set(processError, "code", code); Reflect.set(processError, "signal", exitSignal); - Reflect.set(processError, "killed", child.killed); + Reflect.set(processError, "killed", signalled); } if (processError !== null) Reflect.set(processError, "cmd", invocation); if (outputError !== undefined) Reflect.set(outputError, "cmd", invocation);