GeezerVison is showing github.com — child_process: add subprocess.timedOut · nodejs/node@c1b8f0e
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/child_process.md +29-1Lines changed: 29 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -233,7 +233,8 @@ If a `callback` function is provided, it is called with the arguments | 233 | 233 | `error` will be an instance of [`Error`][]. The `error.code` property will be | 234 | 234 | the exit code of the process. By convention, any exit code other than `0` | 235 | 235 | indicates an error. `error.signal` will be the signal that terminated the | 236 | | -process. | | 236 | +process. `error.timedOut` will be `true` if the process was killed because | | 237 | +the `timeout` option expired. | 237 | 238 |
| 238 | 239 | The `stdout` and `stderr` arguments passed to the callback will contain the | 239 | 240 | stdout and stderr output of the child process. By default, Node.js will decode | @@ -2314,6 +2315,32 @@ subprocess.stdout.on('data', (data) => { | 2314 | 2315 | The `subprocess.stdout` property can be `null` or `undefined` | 2315 | 2316 | if the child process could not be successfully spawned. | 2316 | 2317 |
| | 2318 | +### `subprocess.timedOut` | | 2319 | +
| | 2320 | +<!-- YAML | | 2321 | +added: REPLACEME | | 2322 | +--> | | 2323 | +
| | 2324 | +* Type: {boolean} Set to `true` when the child process is killed because the | | 2325 | + `timeout` option expired. | | 2326 | +
| | 2327 | +The `subprocess.timedOut` property indicates whether the `timeout` option of | | 2328 | +[`child_process.spawn()`][], [`child_process.exec()`][], | | 2329 | +[`child_process.execFile()`][] or [`child_process.fork()`][] expired and the | | 2330 | +child process was sent the `killSignal` as a result. Like | | 2331 | +[`subprocess.killed`][], it does not indicate that the child process has | | 2332 | +terminated yet. | | 2333 | +
| | 2334 | +```js | | 2335 | +const { spawn } = require('node:child_process'); | | 2336 | +
| | 2337 | +const subprocess = spawn('sleep', ['10'], { timeout: 100 }); | | 2338 | +
| | 2339 | +subprocess.on('exit', (code, signal) => { | | 2340 | + console.log(subprocess.timedOut); // true | | 2341 | +}); | | 2342 | +``` | | 2343 | +
| 2317 | 2344 | ### `subprocess.unref()` | 2318 | 2345 |
| 2319 | 2346 | <!-- YAML | @@ -2429,6 +2456,7 @@ or [`child_process.fork()`][]. | 2429 | 2456 | [`subprocess.disconnect()`]: #subprocessdisconnect | 2430 | 2457 | [`subprocess.exitCode`]: #subprocessexitcode | 2431 | 2458 | [`subprocess.kill()`]: #subprocesskillsignal | | 2459 | +[`subprocess.killed`]: #subprocesskilled | 2432 | 2460 | [`subprocess.send()`]: #subprocesssendmessage-sendhandle-options-callback | 2433 | 2461 | [`subprocess.signalCode`]: #subprocesssignalcode | 2434 | 2462 | [`subprocess.stderr`]: #subprocessstderr | |
Collapse file Copy file name to clipboardExpand all lines: lib/child_process.js +9-3Lines changed: 9 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -424,6 +424,7 @@ function execFile(file, args, options, callback) { | 424 | 424 | }); | 425 | 425 |
| 426 | 426 | ex.cmd = cmd; | | 427 | + ex.timedOut = child.timedOut; | 427 | 428 | callback(ex, stdout, stderr); | 428 | 429 | } | 429 | 430 |
| @@ -448,16 +449,19 @@ function execFile(file, args, options, callback) { | 448 | 449 |
| 449 | 450 | killed = true; | 450 | 451 | try { | 451 | | - child.kill(options.killSignal); | | 452 | + return child.kill(options.killSignal); | 452 | 453 | } catch (e) { | 453 | 454 | ex = e; | 454 | 455 | exithandler(); | 455 | 456 | } | | 457 | + return false; | 456 | 458 | } | 457 | 459 |
| 458 | 460 | if (options.timeout > 0) { | 459 | 461 | timeoutId = setTimeout(function delayedKill() { | 460 | | - kill(); | | 462 | + // The child may have exited already, with a descendant keeping its | | 463 | + // stdio open: kill() reports whether the signal was actually sent. | | 464 | + child.timedOut = kill(); | 461 | 465 | timeoutId = null; | 462 | 466 | }, options.timeout); | 463 | 467 | } | @@ -863,7 +867,9 @@ function spawn(file, args, options) { | 863 | 867 | let timeoutId = setTimeout(() => { | 864 | 868 | if (timeoutId) { | 865 | 869 | try { | 866 | | - child.kill(killSignal); | | 870 | + // The child may have exited already, with a descendant keeping its | | 871 | + // stdio open: kill() reports whether the signal was actually sent. | | 872 | + child.timedOut = child.kill(killSignal); | 867 | 873 | } catch (err) { | 868 | 874 | child.emit('error', err); | 869 | 875 | } | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/child_process.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -291,6 +291,7 @@ function ChildProcess() { | 291 | 291 | this.signalCode = null; | 292 | 292 | this.exitCode = null; | 293 | 293 | this.killed = false; | | 294 | + this.timedOut = false; | 294 | 295 | this.spawnfile = null; | 295 | 296 |
| 296 | 297 | this._handle = new Process(); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-child-process-exec-timeout-expire.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,6 +29,7 @@ cp.exec(cmd, { | 29 | 29 |
| 30 | 30 | let sigterm = 'SIGTERM'; | 31 | 31 | assert.strictEqual(err.killed, true); | | 32 | + assert.strictEqual(err.timedOut, true); | 32 | 33 | // TODO OpenBSD returns a null signal and 143 for code | 33 | 34 | if (common.isOpenBSD) { | 34 | 35 | assert.strictEqual(err.code, 143); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-child-process-exec-timeout-kill.js +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,7 +21,7 @@ if (process.argv[2] === 'child') { | 21 | 21 | const [cmd, opts] = common.escapePOSIXShell`"${process.execPath}" "${__filename}" child`; | 22 | 22 |
| 23 | 23 | // Test with a different kill signal. | 24 | | -cp.exec(cmd, { | | 24 | +const child = cp.exec(cmd, { | 25 | 25 | ...opts, | 26 | 26 | timeout: kExpiringParentTimer, | 27 | 27 | killSignal: 'SIGKILL' | @@ -30,6 +30,8 @@ cp.exec(cmd, { | 30 | 30 | console.log('[stderr]', stderr.trim()); | 31 | 31 |
| 32 | 32 | assert.strictEqual(err.killed, true); | | 33 | + assert.strictEqual(err.timedOut, true); | | 34 | + assert.strictEqual(child.timedOut, true); | 33 | 35 | assert.strictEqual(err.code, null); | 34 | 36 | assert.strictEqual(err.signal, 'SIGKILL'); | 35 | 37 | assert.strictEqual(err.cmd, cmd); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-child-process-exec-timeout-not-expired.js +39-1Lines changed: 39 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -22,14 +22,52 @@ if (process.argv[2] === 'child') { | 22 | 22 | return; | 23 | 23 | } | 24 | 24 |
| | 25 | +// The property is also present on errors that are not caused by the timeout. | | 26 | +cp.execFile('this-command-does-not-exist', { timeout: 2 ** 30 }, | | 27 | + common.mustCall((err) => { | | 28 | + assert.strictEqual(err.code, 'ENOENT'); | | 29 | + assert.strictEqual(err.timedOut, false); | | 30 | + })); | | 31 | + | | 32 | +// The timeout expires after the child exited, while a descendant still keeps | | 33 | +// its stdio open: the signal is not sent, so timedOut stays false. The child | | 34 | +// prints the pid of the descendant and the test ends it once the timer has | | 35 | +// fired, so the test does not depend on how fast the child starts. | | 36 | +{ | | 37 | + const timeout = common.platformTimeout(5000); | | 38 | + const child = cp.execFile(process.execPath, ['-e', ` | | 39 | + const { spawn } = require('child_process'); | | 40 | + // Detached: on Windows a non-detached descendant is killed with the job | | 41 | + // object of the child when the child exits. | | 42 | + const child = spawn(process.execPath, | | 43 | + ['-e', 'setTimeout(() => {}, ${common.platformTimeout(30000)})'], | | 44 | + { stdio: 'inherit', detached: true }); | | 45 | + child.unref(); | | 46 | + process.stdout.write(String(child.pid)); | | 47 | + `], { timeout }, common.mustSucceed(() => { | | 48 | + assert.strictEqual(child.exitCode, 0); | | 49 | + assert.strictEqual(child.timedOut, false); | | 50 | + })); | | 51 | + const pid = new Promise((resolve) => { | | 52 | + child.stdout.once('data', (chunk) => resolve(Number(String(chunk)))); | | 53 | + }); | | 54 | + child.on('exit', common.mustCall((code) => { | | 55 | + assert.strictEqual(code, 0); | | 56 | + // The timeout of the child started before this timer, so it fires first. | | 57 | + // The descendant is ended after that, which closes the stdio of the child. | | 58 | + setTimeout(async () => process.kill(await pid), timeout); | | 59 | + })); | | 60 | +} | | 61 | + | 25 | 62 | const [cmd, opts] = common.escapePOSIXShell`"${process.execPath}" "${__filename}" child`; | 26 | 63 |
| 27 | | -cp.exec(cmd, { | | 64 | +const child = cp.exec(cmd, { | 28 | 65 | ...opts, | 29 | 66 | timeout: kTimeoutNotSupposedToExpire, | 30 | 67 | }, common.mustSucceed((stdout, stderr) => { | 31 | 68 | assert.strictEqual(stdout.trim(), 'child stdout'); | 32 | 69 | assert.strictEqual(stderr.trim(), 'child stderr'); | | 70 | + assert.strictEqual(child.timedOut, false); | 33 | 71 | })); | 34 | 72 |
| 35 | 73 | cleanupStaleProcess(__filename); |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-child-process-spawn-timeout-kill-signal.js +57-2Lines changed: 57 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | | -const { mustCall } = require('../common'); | | 3 | +const common = require('../common'); | | 4 | +const { mustCall } = common; | 4 | 5 | const assert = require('assert'); | 5 | 6 | const fixtures = require('../common/fixtures'); | 6 | 7 | const { spawn } = require('child_process'); | @@ -12,7 +13,61 @@ const aliveForeverFile = 'child-process-stay-alive-forever.js'; | 12 | 13 | const cp = spawn(process.execPath, [fixtures.path(aliveForeverFile)], { | 13 | 14 | timeout: 5, | 14 | 15 | }); | 15 | | - cp.on('exit', mustCall((code, ks) => assert.strictEqual(ks, 'SIGTERM'))); | | 16 | + assert.strictEqual(cp.timedOut, false); | | 17 | + cp.on('exit', mustCall((code, ks) => { | | 18 | + assert.strictEqual(ks, 'SIGTERM'); | | 19 | + assert.strictEqual(cp.timedOut, true); | | 20 | + })); | | 21 | +} | | 22 | + | | 23 | +{ | | 24 | + // Verify timedOut stays false when the child exits before the timeout | | 25 | + const cp = spawn(process.execPath, ['-e', ''], { timeout: 2 ** 30 }); | | 26 | + cp.on('exit', mustCall((code) => { | | 27 | + assert.strictEqual(code, 0); | | 28 | + assert.strictEqual(cp.timedOut, false); | | 29 | + })); | | 30 | +} | | 31 | + | | 32 | +{ | | 33 | + // Verify timedOut stays false when the child exits in time but a descendant | | 34 | + // keeps its stdio open past the timeout, so that the timer fires after exit. | | 35 | + // The child prints the pid of the descendant and the test ends it once the | | 36 | + // timer has fired, so the test does not depend on how fast the child starts. | | 37 | + const timeout = common.platformTimeout(5000); | | 38 | + const cp = spawn(process.execPath, ['-e', ` | | 39 | + const { spawn } = require('child_process'); | | 40 | + // Detached: on Windows a non-detached descendant is killed with the job | | 41 | + // object of the child when the child exits. | | 42 | + const child = spawn(process.execPath, | | 43 | + ['-e', 'setTimeout(() => {}, ${common.platformTimeout(30000)})'], | | 44 | + { stdio: 'inherit', detached: true }); | | 45 | + child.unref(); | | 46 | + process.stdout.write(String(child.pid)); | | 47 | + `], { timeout }); | | 48 | + const pid = new Promise((resolve) => { | | 49 | + cp.stdout.once('data', (chunk) => resolve(Number(String(chunk)))); | | 50 | + }); | | 51 | + cp.on('exit', mustCall((code) => { | | 52 | + assert.strictEqual(code, 0); | | 53 | + assert.strictEqual(cp.timedOut, false); | | 54 | + // The timeout of the child started before this timer, so it fires first. | | 55 | + // The descendant is ended after that, which closes the stdio of the child. | | 56 | + setTimeout(async () => process.kill(await pid), timeout); | | 57 | + })); | | 58 | + cp.on('close', mustCall(() => assert.strictEqual(cp.timedOut, false))); | | 59 | +} | | 60 | + | | 61 | +{ | | 62 | + // Verify timedOut stays false when the child is killed by the caller | | 63 | + const cp = spawn(process.execPath, [fixtures.path(aliveForeverFile)], { | | 64 | + timeout: 2 ** 30, | | 65 | + }); | | 66 | + cp.on('spawn', mustCall(() => cp.kill())); | | 67 | + cp.on('exit', mustCall(() => { | | 68 | + assert.strictEqual(cp.killed, true); | | 69 | + assert.strictEqual(cp.timedOut, false); | | 70 | + })); | 16 | 71 | } | 17 | 72 |
| 18 | 73 | { | |
|