Skip to content

Commit 9e7fb63

Browse files
authored
fix(ext/node): implement timeout and killSignal for spawn() (#32283)
Fixes the `timeout` and `killSignal` options for `child_process.spawn()` (and `fork()`, which delegates to `spawn()`). Previously these options were accepted but silently ignored. The fix sets a `setTimeout` after spawning that kills the child with the specified `killSignal` (default: `"SIGTERM"`) if the process hasn't exited within `timeout` ms. The timer is cleared if the process exits naturally before the deadline.
1 parent 38f3ffe commit 9e7fb63

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

ext/node/polyfills/child_process.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,27 @@ export function spawn(
222222
options = normalizeSpawnArguments(command, args, options);
223223

224224
validateAbortSignal(options?.signal, "options.signal");
225+
validateTimeout(options?.timeout);
225226

226227
const child = new ChildProcess();
227228
child.spawn(options);
229+
230+
const timeout = options?.timeout;
231+
if (timeout != null && timeout > 0) {
232+
const killSignal = options?.killSignal ?? "SIGTERM";
233+
let timeoutId: ReturnType<typeof setTimeout> | null = setTimeout(() => {
234+
timeoutId = null;
235+
child.kill(killSignal as string);
236+
}, timeout);
237+
238+
child.once("exit", () => {
239+
if (timeoutId) {
240+
clearTimeout(timeoutId);
241+
timeoutId = null;
242+
}
243+
});
244+
}
245+
228246
return child;
229247
}
230248

ext/node/polyfills/internal/child_process.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,13 +1471,7 @@ function _createSpawnError(
14711471
}
14721472

14731473
export interface SpawnOptions extends ChildProcessOptions {
1474-
/**
1475-
* NOTE: This option is not yet implemented.
1476-
*/
14771474
timeout?: number;
1478-
/**
1479-
* NOTE: This option is not yet implemented.
1480-
*/
14811475
killSignal?: string;
14821476
}
14831477

tests/node_compat/config.jsonc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@
191191
"parallel/test-child-process-fork-no-shell.js": {},
192192
"parallel/test-child-process-fork-ref.js": {},
193193
"parallel/test-child-process-fork-ref2.js": {},
194+
"parallel/test-child-process-fork-timeout-kill-signal.js": {},
194195
"parallel/test-child-process-fork3.js": {},
195196
"parallel/test-child-process-ipc-next-tick.js": {},
196197
"parallel/test-child-process-no-deprecation.js": {},
@@ -201,6 +202,7 @@
201202
"parallel/test-child-process-silent.js": {},
202203
"parallel/test-child-process-spawn-error.js": {},
203204
"parallel/test-child-process-spawn-event.js": {},
205+
"parallel/test-child-process-spawn-timeout-kill-signal.js": {},
204206
"parallel/test-child-process-spawn-typeerror.js": {},
205207
"parallel/test-child-process-spawnsync-args.js": {},
206208
"parallel/test-child-process-spawnsync-env.js": {},

0 commit comments

Comments
 (0)