Skip to content
Myr

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

6 files+34 added−10 removed

modified:docs/api.md+2 added-2 removed
1101 unmodified lines
11021103_Type:_ `number`\1104_Default:_ `0`11051106If `timeout` is greater than `0`, the subprocess will be [terminated](#optionskillsignal) if it runs for longer than that amount of milliseconds.1106If `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).11071108On timeout, [`error.timedOut`](#errortimedout) becomes `true`.11091110[More info.](termination.md#timeout)
25 unmodified lines
11361137_Type:_ `number | false`\1138_Default:_ `5000`11391140If the subprocess is terminated but does not exit, forcefully exit it by sending [`SIGKILL`](https://en.wikipedia.org/wiki/Signal_(IPC)#SIGKILL).1140If 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).11411142When this happens, [`error.isForcefullyTerminated`](#errorisforcefullyterminated) becomes `true`.11431144[More info.](termination.md#forceful-termination)
221 unmodified lines
modified:lib/terminate/kill.js+3 added-2 removed
modified:lib/terminate/timeout.js+8 added-2 removed
modified:test/terminate/kill-force.js+4 added
45 unmodified lines
46 execa('empty.js', {forceKillAfterDelay});47 }, {instanceOf: TypeError, message: /non-negative integer/});48};4950test('`forceKillAfterDelay` should not be fractional', () => testInvalidForceKill(5000.5));51test('`forceKillAfterDelay` should not be NaN', () => testInvalidForceKill(NaN));52test('`forceKillAfterDelay` should not be negative', () => testInvalidForceKill(-1));53// Larger delays make timers fire right away54test('`forceKillAfterDelay` should not be larger than the maximum timer delay', () => testInvalidForceKill(2 ** 31));5556// `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.58if (isWindows) {
23 unmodified lines
82 assert.equal(isForcefullyTerminated, false);83 };8485 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 lines
modified:test/terminate/timeout.js+15 added-2 removed
47 unmodified lines
48 execaMethod('empty.js', {timeout});49 }, {message: INVALID_TIMEOUT_REGEXP});50};5152test('timeout must be an integer', () => testTimeoutValidation(100.5, execa));53test('timeout must be an integer - sync', () => testTimeoutValidation(100.5, execaSync));54test('timeout must not be negative', () => testTimeoutValidation(-1, execa));53test('timeout must be an integer', () => testTimeoutValidation(false, execa));55test('timeout must be a number', () => testTimeoutValidation(false, execa));56test('timeout must not be negative - sync', () => testTimeoutValidation(-1, execaSync));55test('timeout must be an integer - sync', () => testTimeoutValidation(false, execaSync));57test('timeout must be a number - sync', () => testTimeoutValidation(false, execaSync));58// Larger delays make timers fire right away59test('timeout must not be larger than the maximum timer delay', () => testTimeoutValidation(2 ** 31, execa));60test('timeout must not be larger than the maximum timer delay - sync', () => testTimeoutValidation(2 ** 31, execaSync));6162const testMaxTimeout = async execaMethod => {63 const {timedOut} = await execaMethod('noop.js', {timeout: (2 ** 31) - 1});64 assert.equal(timedOut, false);65};6667test('timeout can be the maximum timer delay', () => testMaxTimeout(execa));68test('timeout can be the maximum timer delay - sync', () => testMaxTimeout(execaSync));6970test('timedOut is false if timeout is undefined', async () => {71 const {timedOut} = await execa('noop.js');72 assert.equal(timedOut, false);73});
19 unmodified lines
modified:types/arguments/options.d.ts+2 added-2 removed
251 unmodified lines
252 */253 readonly reject?: boolean;254255 /**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).257258 On timeout, `error.timedOut` becomes `true`.259260 @default 0
40 unmodified lines
301 */302 readonly gracefulCancel?: Unless<IsSync, boolean>;303304 /**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).306307 When this happens, `error.isForcefullyTerminated` becomes `true`.308309 @default 5000
120 unmodified lines