Fix invalid `timeout` and `forceKillAfterDelay` values
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Timers fire after 1ms when their delay is larger than 2147483647ms (about 24.8 days), so a large `timeout` or `forceKillAfterDelay` terminated the subprocess right away. Such values now throw instead. Fractional `timeout` values were accepted by asynchronous methods, but `spawnSync()` requires an integer, so synchronous methods failed with a raw `ERR_OUT_OF_RANGE` error. Both options now require integers, as their error messages already documented.
authored by Sindre Sorhus · committed by Sindre Sorhus · parent c4262ff · Browse files · 6 files·+34 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+34 added−10 removed
- tests:+19 added−2 removed
- other:+15 added−8 removed
Provenance
- Commit
d9a5689- Tree
ae844bb- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
c4262ff
6 files changed+34 added−10 removed
modified:docs/api.md+2 added-2 removed
1101 unmodified lines1102 1103 _Type:_ `number`\1104 _Default:_ `0`1105 1106-If `timeout` is greater than `0`, the subprocess will be [terminated](#optionskillsignal) if it runs for longer than that amount of milliseconds.1106+If `timeout` is greater than `0`, the subprocess will be [terminated](#optionskillsignal) if it runs for longer than that amount of milliseconds. The maximum value is `2147483647` (about 24.8 days).1107 1108 On timeout, [`error.timedOut`](#errortimedout) becomes `true`.1109 1110 [More info.](termination.md#timeout)25 unmodified lines1136 1137 _Type:_ `number | false`\1138 _Default:_ `5000`1139 1140-If the subprocess is terminated but does not exit, forcefully exit it by sending [`SIGKILL`](https://en.wikipedia.org/wiki/Signal_(IPC)#SIGKILL).1140+If the subprocess is terminated but does not exit, forcefully exit it by sending [`SIGKILL`](https://en.wikipedia.org/wiki/Signal_(IPC)#SIGKILL). The maximum delay is `2147483647` milliseconds (about 24.8 days).1141 1142 When this happens, [`error.isForcefullyTerminated`](#errorisforcefullyterminated) becomes `true`.1143 1144 [More info.](termination.md#forceful-termination)221 unmodified linesmodified:lib/terminate/kill.js+3 added-2 removed
123456783 unmodified lines12131415151616171819202173 unmodified linesimport {setTimeout} from 'node:timers/promises';import {isErrorInstance} from '../return/final-error.js';import {normalizeSignalArgument} from './signal.js';import {isValidDelay, MAX_DELAY} from './timeout.js';
// Normalize the `forceKillAfterDelay` optionexport const normalizeForceKillAfterDelay = forceKillAfterDelay => { if (forceKillAfterDelay === false) {3 unmodified lines if (forceKillAfterDelay === true) { return DEFAULT_FORCE_KILL_TIMEOUT; }
if (!Number.isFinite(forceKillAfterDelay) || forceKillAfterDelay < 0) { throw new TypeError(`Expected the \`forceKillAfterDelay\` option to be a non-negative integer, got \`${forceKillAfterDelay}\` (${typeof forceKillAfterDelay})`); if (!isValidDelay(forceKillAfterDelay)) { throw new TypeError(`Expected the \`forceKillAfterDelay\` option to be a non-negative integer up to ${MAX_DELAY}, got \`${forceKillAfterDelay}\` (${typeof forceKillAfterDelay})`); }
return forceKillAfterDelay;};73 unmodified linesmodified:lib/terminate/timeout.js+8 added-2 removed
1234567678910111213141516171819207 unmodified linesimport {setTimeout} from 'node:timers/promises';import {DiscardedError} from '../return/final-error.js';
// Validate `timeout` optionexport const validateTimeout = ({timeout}) => { if (timeout !== undefined && (!Number.isFinite(timeout) || timeout < 0)) { throw new TypeError(`Expected the \`timeout\` option to be a non-negative integer, got \`${timeout}\` (${typeof timeout})`); if (timeout !== undefined && !isValidDelay(timeout)) { throw new TypeError(`Expected the \`timeout\` option to be a non-negative integer up to ${MAX_DELAY}, got \`${timeout}\` (${typeof timeout})`); }};
// Fractional numbers are excluded since `spawnSync()` requires an integer `timeout`export const isValidDelay = delay => Number.isInteger(delay) && delay >= 0 && delay <= MAX_DELAY;
// Timers fire right away when their delay is larger than thisexport const MAX_DELAY = (2 ** 31) - 1;
// Fails when the `timeout` option is exceededexport const throwOnTimeout = (kill, timeout, context, controller) => timeout === 0 || timeout === undefined ? [] : [killAfterTimeout(kill, timeout, context, controller)];7 unmodified linesmodified:test/terminate/kill-force.js+4 added
45 unmodified lines46 execa('empty.js', {forceKillAfterDelay});47 }, {instanceOf: TypeError, message: /non-negative integer/});48 };49 50+test('`forceKillAfterDelay` should not be fractional', () => testInvalidForceKill(5000.5));51 test('`forceKillAfterDelay` should not be NaN', () => testInvalidForceKill(NaN));52 test('`forceKillAfterDelay` should not be negative', () => testInvalidForceKill(-1));53+// Larger delays make timers fire right away54+test('`forceKillAfterDelay` should not be larger than the maximum timer delay', () => testInvalidForceKill(2 ** 31));55 56 // `SIGTERM` cannot be caught on Windows, and it always aborts the subprocess (like `SIGKILL` on Unix).57 // Therefore, this feature and those tests must be different on Windows.58 if (isWindows) {23 unmodified lines82 assert.equal(isForcefullyTerminated, false);83 };84 85 test('`forceKillAfterDelay: false` should not kill after a timeout', () => testNoForceKill(false));86+ test('`forceKillAfterDelay` can be the maximum timer delay', () => testNoForceKill((2 ** 31) - 1));87 test('`forceKillAfterDelay` should not kill after a timeout with other signals', () => testNoForceKill(true, 'SIGINT'));88 test('`forceKillAfterDelay` should not kill after a timeout with wrong killSignal string', () => testNoForceKill(true, 'SIGTERM', {killSignal: 'SIGINT'}));89 test('`forceKillAfterDelay` should not kill after a timeout with wrong killSignal number', () => testNoForceKill(true, constants.signals.SIGTERM, {killSignal: constants.signals.SIGINT}));90 134 unmodified linesmodified:test/terminate/timeout.js+15 added-2 removed
47 unmodified lines48 execaMethod('empty.js', {timeout});49 }, {message: INVALID_TIMEOUT_REGEXP});50 };51 52+test('timeout must be an integer', () => testTimeoutValidation(100.5, execa));53+test('timeout must be an integer - sync', () => testTimeoutValidation(100.5, execaSync));54 test('timeout must not be negative', () => testTimeoutValidation(-1, execa));53-test('timeout must be an integer', () => testTimeoutValidation(false, execa));55+test('timeout must be a number', () => testTimeoutValidation(false, execa));56 test('timeout must not be negative - sync', () => testTimeoutValidation(-1, execaSync));55-test('timeout must be an integer - sync', () => testTimeoutValidation(false, execaSync));57+test('timeout must be a number - sync', () => testTimeoutValidation(false, execaSync));58+// Larger delays make timers fire right away59+test('timeout must not be larger than the maximum timer delay', () => testTimeoutValidation(2 ** 31, execa));60+test('timeout must not be larger than the maximum timer delay - sync', () => testTimeoutValidation(2 ** 31, execaSync));61 62+const testMaxTimeout = async execaMethod => {63+ const {timedOut} = await execaMethod('noop.js', {timeout: (2 ** 31) - 1});64+ assert.equal(timedOut, false);65+};66+67+test('timeout can be the maximum timer delay', () => testMaxTimeout(execa));68+test('timeout can be the maximum timer delay - sync', () => testMaxTimeout(execaSync));69+70 test('timedOut is false if timeout is undefined', async () => {71 const {timedOut} = await execa('noop.js');72 assert.equal(timedOut, false);73 });19 unmodified linesmodified:types/arguments/options.d.ts+2 added-2 removed
251 unmodified lines252 */253 readonly reject?: boolean;254 255 /**256- If `timeout` is greater than `0`, the subprocess will be terminated if it runs for longer than that amount of milliseconds.256+ If `timeout` is greater than `0`, the subprocess will be terminated if it runs for longer than that amount of milliseconds. The maximum value is `2147483647` (about 24.8 days).257 258 On timeout, `error.timedOut` becomes `true`.259 260 @default 040 unmodified lines301 */302 readonly gracefulCancel?: Unless<IsSync, boolean>;303 304 /**305- If the subprocess is terminated but does not exit, forcefully exit it by sending [`SIGKILL`](https://en.wikipedia.org/wiki/Signal_(IPC)#SIGKILL).305+ If the subprocess is terminated but does not exit, forcefully exit it by sending [`SIGKILL`](https://en.wikipedia.org/wiki/Signal_(IPC)#SIGKILL). The maximum delay is `2147483647` milliseconds (about 24.8 days).306 307 When this happens, `error.isForcefullyTerminated` becomes `true`.308 309 @default 5000120 unmodified lines