From 3989ee088a47cece9ef36b9909e64ccaaae948b0 Mon Sep 17 00:00:00 2001 From: marcopiraccini Date: Sat, 10 Oct 2026 11:22:41 +0200 Subject: [PATCH] child_process: add subprocess.timedOut When the timeout option of spawn(), exec(), execFile() or fork() expires, the child is sent the kill signal and exits with that signal. Nothing told the caller that the timeout was the reason. Set subprocess.timedOut to true when the kill signal is sent because of the timeout, and add the same flag to the error passed to the exec() and execFile() callback. Fixes: https://github.com/nodejs/node/issues/51561 Refs: https://github.com/nodejs/node/pull/51608 Signed-off-by: marcopiraccini --- doc/api/child_process.md | 30 ++++++++++++- lib/child_process.js | 12 +++-- lib/internal/child_process.js | 1 + .../test-child-process-exec-timeout-expire.js | 1 + .../test-child-process-exec-timeout-kill.js | 4 +- ...-child-process-exec-timeout-not-expired.js | 23 +++++++++- ...child-process-spawn-timeout-kill-signal.js | 45 ++++++++++++++++++- 7 files changed, 108 insertions(+), 8 deletions(-) diff --git a/doc/api/child_process.md b/doc/api/child_process.md index 3f82fb56a88c..87b17ae441bc 100644 --- a/doc/api/child_process.md +++ b/doc/api/child_process.md @@ -233,7 +233,8 @@ If a `callback` function is provided, it is called with the arguments `error` will be an instance of [`Error`][]. The `error.code` property will be the exit code of the process. By convention, any exit code other than `0` indicates an error. `error.signal` will be the signal that terminated the -process. +process. `error.timedOut` will be `true` if the process was killed because +the `timeout` option expired. The `stdout` and `stderr` arguments passed to the callback will contain the stdout and stderr output of the child process. By default, Node.js will decode @@ -2314,6 +2315,32 @@ subprocess.stdout.on('data', (data) => { The `subprocess.stdout` property can be `null` or `undefined` if the child process could not be successfully spawned. +### `subprocess.timedOut` + + + +* Type: {boolean} Set to `true` when the child process is killed because the + `timeout` option expired. + +The `subprocess.timedOut` property indicates whether the `timeout` option of +[`child_process.spawn()`][], [`child_process.exec()`][], +[`child_process.execFile()`][] or [`child_process.fork()`][] expired and the +child process was sent the `killSignal` as a result. Like +[`subprocess.killed`][], it does not indicate that the child process has +terminated yet. + +```js +const { spawn } = require('node:child_process'); + +const subprocess = spawn('sleep', ['10'], { timeout: 100 }); + +subprocess.on('exit', (code, signal) => { + console.log(subprocess.timedOut); // true +}); +``` + ### `subprocess.unref()`