From 51766b0267b83c47f9b4ef8c67beb7b95960df9a Mon Sep 17 00:00:00 2001 From: FranciscoMateusVG Date: Tue, 30 Jan 2024 13:51:31 -0300 Subject: [PATCH 1/3] lib: add timedOut flag on child process spawn --- lib/child_process.js | 1 + lib/internal/child_process.js | 1 + 2 files changed, 2 insertions(+) diff --git a/lib/child_process.js b/lib/child_process.js index 449013906e93e5..d3d6699e823ceb 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -764,6 +764,7 @@ function spawn(file, args, options) { let timeoutId = setTimeout(() => { if (timeoutId) { try { + child.timedOut = true; child.kill(killSignal); } catch (err) { child.emit('error', err); diff --git a/lib/internal/child_process.js b/lib/internal/child_process.js index 49edaba5b558e9..39a878742ff489 100644 --- a/lib/internal/child_process.js +++ b/lib/internal/child_process.js @@ -263,6 +263,7 @@ function ChildProcess() { this.exitCode = null; this.killed = false; this.spawnfile = null; + this.timedOut = false; this._handle = new Process(); this._handle[owner_symbol] = this; From 0b1f81318b003ad3fcc584c749b6922d5318358a Mon Sep 17 00:00:00 2001 From: FranciscoMateusVG Date: Tue, 30 Jan 2024 13:52:10 -0300 Subject: [PATCH 2/3] test: add test for checking timedOut flag is set on child process spawn --- .../test-child-process-spawn-timeout-kill-signal.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/parallel/test-child-process-spawn-timeout-kill-signal.js b/test/parallel/test-child-process-spawn-timeout-kill-signal.js index 59a974d0e0b448..0d8357166d6490 100644 --- a/test/parallel/test-child-process-spawn-timeout-kill-signal.js +++ b/test/parallel/test-child-process-spawn-timeout-kill-signal.js @@ -35,6 +35,17 @@ const aliveForeverFile = 'child-process-stay-alive-forever.js'; }), /ERR_OUT_OF_RANGE/); } +{ + // Verify timedOut flag is set true + const cp = spawn(process.execPath, [fixtures.path(aliveForeverFile)], { + timeout: 6, + }); + strictEqual(cp.timedOut, false); + cp.on('exit', mustCall(() => { + strictEqual(cp.timedOut, true); + })); +} + { // Verify abort signal gets unregistered const controller = new AbortController(); From 2e3cb97b470dc7b88a71b2a43d1a9f5754c2401e Mon Sep 17 00:00:00 2001 From: rust Date: Sun, 8 Feb 2026 22:08:49 -0300 Subject: [PATCH 3/3] doc: add API docs for subprocess.timedOut property Document the timedOut boolean property on ChildProcess that indicates whether the child process was killed due to a timeout option. Refs: https://github.com/nodejs/node/pull/51561 --- doc/api/child_process.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/doc/api/child_process.md b/doc/api/child_process.md index eb4095b6b71a3e..c58a00539d81be 100644 --- a/doc/api/child_process.md +++ b/doc/api/child_process.md @@ -1785,6 +1785,29 @@ 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` + + + +* {boolean} + +The `subprocess.timedOut` property indicates whether the child process +was killed due to the `timeout` option set in [`child_process.spawn()`][], +[`child_process.exec()`][], [`child_process.execFile()`][], or +[`child_process.fork()`][]. + +```js +const { spawn } = require('node:child_process'); + +const subprocess = spawn('sleep', ['10'], { timeout: 100 }); + +subprocess.on('exit', () => { + console.log(subprocess.timedOut); // true +}); +``` + ### `subprocess.unref()`