Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions doc/api/child_process.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

<!-- YAML
added: REPLACEME
-->

* {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()`

<!-- YAML
Expand Down
1 change: 1 addition & 0 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-child-process-spawn-timeout-kill-signal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down