fix(process): stop timed-out commands with the caller's stop signal
execFileOutput forwarded stopSignal only together with an AbortSignal, so a caller with just a timeout (the native UI helper's swiftc build) still got Node's SIGTERM. When the timeout did send the stop signal, nothing killed a process that ignored it, and a process that exited cleanly after the signal resolved as a success. A stoppable command now owns its timeout: it sends the stop signal, kills the process after the grace period, and rejects with ETIMEDOUT and killed: true. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
e6905c18e0
commit
063de5014f
@@ -95,3 +95,50 @@ describe("execFileOutput", () => {
|
|||||||
).rejects.toMatchObject({ name: "AbortError" });
|
).rejects.toMatchObject({ name: "AbortError" });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe.skipIf(process.platform === "win32")(
|
||||||
|
"execFileOutput stop signal at a timeout",
|
||||||
|
() => {
|
||||||
|
it("stops a timed-out process with the caller's signal and reports the timeout", async () => {
|
||||||
|
const directory = await createTestTempDirectory("rea-exec-timeout-");
|
||||||
|
const stopped = join(directory, "stopped");
|
||||||
|
// The child exits cleanly once it has recorded the signal it received.
|
||||||
|
const script = [
|
||||||
|
'const fs = require("node:fs");',
|
||||||
|
'for (const name of ["SIGINT", "SIGTERM"])',
|
||||||
|
` process.on(name, () => setTimeout(() => { fs.writeFileSync(${JSON.stringify(stopped)}, name); process.exit(0); }, 200));`,
|
||||||
|
"setInterval(() => {}, 1000);",
|
||||||
|
].join("\n");
|
||||||
|
|
||||||
|
const failure: unknown = await execFileOutput(
|
||||||
|
process.execPath,
|
||||||
|
["-e", script],
|
||||||
|
{ timeout: 2_000, stopSignal: "SIGINT" },
|
||||||
|
).then(
|
||||||
|
() => undefined,
|
||||||
|
(cause: unknown) => cause,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(failure).toMatchObject({ code: "ETIMEDOUT", killed: true });
|
||||||
|
expect(await readFile(stopped, "utf8")).toBe("SIGINT");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("kills a timed-out process that ignores the stop signal", async () => {
|
||||||
|
const script = [
|
||||||
|
'for (const name of ["SIGINT", "SIGTERM"]) process.on(name, () => {});',
|
||||||
|
"setInterval(() => {}, 1000);",
|
||||||
|
].join("\n");
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
execFileOutput(process.execPath, ["-e", script], {
|
||||||
|
timeout: 1_000,
|
||||||
|
stopSignal: "SIGINT",
|
||||||
|
}),
|
||||||
|
).rejects.toMatchObject({
|
||||||
|
code: "ETIMEDOUT",
|
||||||
|
killed: true,
|
||||||
|
signal: "SIGKILL",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|||||||
@@ -30,6 +30,29 @@ const abortError = (signal: AbortSignal): Error => {
|
|||||||
return error;
|
return error;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A command stopped at its timeout can still exit cleanly, so the timeout is
|
||||||
|
* the failure. Like Node's execFile timeout, it reports `killed: true`.
|
||||||
|
*/
|
||||||
|
const timeoutError = (
|
||||||
|
command: string,
|
||||||
|
timeout: number,
|
||||||
|
exit: Error | null,
|
||||||
|
): Error => {
|
||||||
|
const error = new Error(
|
||||||
|
`Command timed out after ${String(timeout)} ms: ${command}`,
|
||||||
|
exit === null ? undefined : { cause: exit },
|
||||||
|
);
|
||||||
|
Reflect.set(error, "code", "ETIMEDOUT");
|
||||||
|
Reflect.set(error, "killed", true);
|
||||||
|
Reflect.set(
|
||||||
|
error,
|
||||||
|
"signal",
|
||||||
|
exit === null ? null : (Reflect.get(exit, "signal") ?? null),
|
||||||
|
);
|
||||||
|
return error;
|
||||||
|
};
|
||||||
|
|
||||||
/** Captured subprocess output read from an execFileOutput rejection. */
|
/** Captured subprocess output read from an execFileOutput rejection. */
|
||||||
export interface ExecFileOutputFailure {
|
export interface ExecFileOutputFailure {
|
||||||
readonly stdout: string;
|
readonly stdout: string;
|
||||||
@@ -69,35 +92,41 @@ export const execFileOutput = (
|
|||||||
options: ExecFileOutputOptions = {},
|
options: ExecFileOutputOptions = {},
|
||||||
): Promise<{ readonly stdout: string; readonly stderr: string }> =>
|
): Promise<{ readonly stdout: string; readonly stderr: string }> =>
|
||||||
new Promise((resolve, reject) => {
|
new Promise((resolve, reject) => {
|
||||||
const { stopSignal, signal, ...execOptions } = options;
|
const { stopSignal, signal, timeout, ...execOptions } = options;
|
||||||
const stopped =
|
if (stopSignal !== undefined && signal?.aborted === true) {
|
||||||
stopSignal === undefined || signal === undefined
|
reject(abortError(signal));
|
||||||
? undefined
|
|
||||||
: { signal, stopSignal };
|
|
||||||
if (stopped?.signal.aborted === true) {
|
|
||||||
reject(abortError(stopped.signal));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let aborted = false;
|
let stopped: "abort" | "timeout" | undefined;
|
||||||
|
let deadline: NodeJS.Timeout | undefined;
|
||||||
let grace: NodeJS.Timeout | undefined;
|
let grace: NodeJS.Timeout | undefined;
|
||||||
const child = execFile(
|
const child = execFile(
|
||||||
command,
|
command,
|
||||||
[...arguments_],
|
[...arguments_],
|
||||||
{
|
{
|
||||||
...execOptions,
|
...execOptions,
|
||||||
...(stopped === undefined
|
// A stoppable command's abort and timeout are handled below, because
|
||||||
? signal === undefined
|
// execFile neither waits for the exit on abort nor kills a process
|
||||||
? {}
|
// that ignores its timeout signal.
|
||||||
: { signal }
|
...(stopSignal === undefined
|
||||||
: { killSignal: stopped.stopSignal }),
|
? {
|
||||||
|
...(signal === undefined ? {} : { signal }),
|
||||||
|
...(timeout === undefined ? {} : { timeout }),
|
||||||
|
}
|
||||||
|
: { killSignal: stopSignal }),
|
||||||
encoding: "utf8",
|
encoding: "utf8",
|
||||||
maxBuffer: options.maxBuffer ?? Number.POSITIVE_INFINITY,
|
maxBuffer: options.maxBuffer ?? Number.POSITIVE_INFINITY,
|
||||||
},
|
},
|
||||||
(error, stdout, stderr) => {
|
(error, stdout, stderr) => {
|
||||||
stopped?.signal.removeEventListener("abort", onAbort);
|
signal?.removeEventListener("abort", onAbort);
|
||||||
|
clearTimeout(deadline);
|
||||||
clearTimeout(grace);
|
clearTimeout(grace);
|
||||||
const failure =
|
const failure =
|
||||||
aborted && stopped !== undefined ? abortError(stopped.signal) : error;
|
stopped === "abort" && signal !== undefined
|
||||||
|
? abortError(signal)
|
||||||
|
: stopped === "timeout" && timeout !== undefined
|
||||||
|
? timeoutError(command, timeout, error)
|
||||||
|
: error;
|
||||||
if (failure !== null) {
|
if (failure !== null) {
|
||||||
Reflect.set(failure, "stdout", stdout);
|
Reflect.set(failure, "stdout", stdout);
|
||||||
Reflect.set(failure, "stderr", stderr);
|
Reflect.set(failure, "stderr", stderr);
|
||||||
@@ -107,11 +136,20 @@ export const execFileOutput = (
|
|||||||
resolve({ stdout, stderr });
|
resolve({ stdout, stderr });
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
const onAbort = (): void => {
|
// The first stop request names the failure; a later one is not resent.
|
||||||
if (stopped === undefined) return;
|
const stop = (reason: "abort" | "timeout"): void => {
|
||||||
aborted = true;
|
if (stopped !== undefined) return;
|
||||||
child.kill(stopped.stopSignal);
|
stopped = reason;
|
||||||
|
child.kill(stopSignal);
|
||||||
grace = setTimeout(() => child.kill("SIGKILL"), STOP_GRACE_MS);
|
grace = setTimeout(() => child.kill("SIGKILL"), STOP_GRACE_MS);
|
||||||
};
|
};
|
||||||
stopped?.signal.addEventListener("abort", onAbort, { once: true });
|
const onAbort = (): void => {
|
||||||
|
stop("abort");
|
||||||
|
};
|
||||||
|
if (stopSignal === undefined) return;
|
||||||
|
signal?.addEventListener("abort", onAbort, { once: true });
|
||||||
|
if (timeout !== undefined && timeout > 0)
|
||||||
|
deadline = setTimeout(() => {
|
||||||
|
stop("timeout");
|
||||||
|
}, timeout);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user