diff --git a/index.d.ts b/index.d.ts index c2be4cef1d..fe2c04733b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -281,18 +281,13 @@ declare namespace execa { interface KillOptions { /** - If the first signal does not terminate the child process after a specified timeout, a `SIGKILL` signal will be sent to the process. + How many milliseconds to wait for the child process to terminate before sending `SIGKILL`. - @default true - */ - forceKill?: boolean; - - /** - Milliseconds to wait for the child process to terminate before sending a `SIGKILL` signal. + Can be disabled with `false`. @default 5000 */ - forceKillAfter?: number; + forceKillAfterTimeout?: boolean | number; } interface ExecaChildPromise { diff --git a/index.js b/index.js index fe0d0a2148..9d55dbc464 100644 --- a/index.js +++ b/index.js @@ -231,8 +231,8 @@ function setKillTimeout(kill, signal, options, killResult) { }, timeout).unref(); } -function shouldForceKill(signal, {forceKill}, killResult) { - return isSigterm(signal) && forceKill !== false && killResult; +function shouldForceKill(signal, {forceKillAfterTimeout}, killResult) { + return isSigterm(signal) && forceKillAfterTimeout !== false && killResult; } function isSigterm(signal) { @@ -240,12 +240,16 @@ function isSigterm(signal) { (typeof signal === 'string' && signal.toUpperCase() === 'SIGTERM'); } -function getForceKillAfterTimeout({forceKillAfter = DEFAULT_FORCE_KILL_TIMEOUT}) { - if (!Number.isInteger(forceKillAfter) || forceKillAfter < 0) { - throw new TypeError(`Expected the \`forceKillAfter\` option to be a non-negative integer, got \`${forceKillAfter}\` (${typeof forceKillAfter})`); +function getForceKillAfterTimeout({forceKillAfterTimeout = true}) { + if (forceKillAfterTimeout === true) { + return DEFAULT_FORCE_KILL_TIMEOUT; } - return forceKillAfter; + if (!Number.isInteger(forceKillAfterTimeout) || forceKillAfterTimeout < 0) { + throw new TypeError(`Expected the \`forceKillAfterTimeout\` option to be a non-negative integer, got \`${forceKillAfterTimeout}\` (${typeof forceKillAfterTimeout})`); + } + + return forceKillAfterTimeout; } const execa = (file, args, options) => { diff --git a/index.test-d.ts b/index.test-d.ts index 653e97b251..5af9918243 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -123,10 +123,10 @@ execa('unicorns').kill(); execa('unicorns').kill('SIGKILL'); execa('unicorns').kill(undefined); execa('unicorns').kill('SIGKILL', {}); -execa('unicorns').kill('SIGKILL', {forceKill: true}); -execa('unicorns').kill('SIGKILL', {forceKill: false}); -execa('unicorns').kill('SIGKILL', {forceKillAfter: 42}); -execa('unicorns').kill('SIGKILL', {forceKillAfter: undefined}); +execa('unicorns').kill('SIGKILL', {forceKillAfterTimeout: true}); +execa('unicorns').kill('SIGKILL', {forceKillAfterTimeout: false}); +execa('unicorns').kill('SIGKILL', {forceKillAfterTimeout: 42}); +execa('unicorns').kill('SIGKILL', {forceKillAfterTimeout: undefined}); expectType>(execa('unicorns')); expectType>(await execa('unicorns')); diff --git a/readme.md b/readme.md index 79573154e8..5f523167e1 100644 --- a/readme.md +++ b/readme.md @@ -117,7 +117,7 @@ try { const subprocess = execa('node'); setTimeout(() => { subprocess.kill('SIGTERM', { - forceKillAfter: 2000 + forceKillAfterTimeout: 2000 }); }, 1000); ``` @@ -140,19 +140,14 @@ Returns a [`child_process` instance](https://nodejs.org/api/child_process.html#c Same as the original [`child_process#kill()`](https://nodejs.org/api/child_process.html#child_process_subprocess_kill_signal) except: if `signal` is `SIGTERM` (the default value) and the child process is not terminated after 5 seconds, force it by sending `SIGKILL`. -##### options.forceKill +##### options.forceKillAfterTimeout -Type: `boolean`
-Default: `true` - -If the first signal does not terminate the child process after a specified timeout, a `SIGKILL` signal will be sent to the process. - -##### options.forceKillAfter - -Type: `string`
+Type: `boolean | number`
Default: `5000` -Milliseconds to wait for the child process to terminate before sending `SIGKILL`. +How many milliseconds to wait for the child process to terminate before sending `SIGKILL`. + +Can be disabled with `false`. #### cancel() diff --git a/test.js b/test.js index 718e32a0fb..ae20966f0e 100644 --- a/test.js +++ b/test.js @@ -140,8 +140,7 @@ if (process.platform !== 'win32') { await pEvent(subprocess, 'message'); subprocess.kill('SIGTERM', { - forceKill: false, - forceKillAfter: 50 + forceKillAfterTimeout: false }); t.true(isRunning(subprocess.pid)); @@ -156,13 +155,26 @@ if (process.platform !== 'win32') { await pEvent(subprocess, 'message'); subprocess.kill('SIGTERM', { - forceKillAfter: 50 + forceKillAfterTimeout: 50 }); const {signal} = await t.throwsAsync(subprocess); t.is(signal, 'SIGKILL'); }); + test('.kill() `forceKillAfterTimeout` can be `true`', async t => { + const subprocess = execa('node', ['fixtures/no-killable'], { + stdio: ['ipc'] + }); + + await pEvent(subprocess, 'message'); + + subprocess.kill('SIGTERM', {forceKillAfterTimeout: true}); + + const {signal} = await t.throwsAsync(subprocess); + t.is(signal, 'SIGKILL'); + }); + test('execa() with .kill() after it with nothing (undefined) should kill after 50 ms with SIGKILL', async t => { const subprocess = execa('node', ['fixtures/no-killable'], { stdio: ['ipc'] @@ -176,15 +188,15 @@ if (process.platform !== 'win32') { t.is(signal, 'SIGKILL'); }); - test('.kill() `forceKillAfter` should not be a float', t => { + test('.kill() `forceKillAfterTimeout` should not be a float', t => { t.throws(() => { - execa('noop').kill('SIGTERM', {forceKillAfter: 0.5}); + execa('noop').kill('SIGTERM', {forceKillAfterTimeout: 0.5}); }, {instanceOf: TypeError, message: /non-negative integer/}); }); - test('.kill() `forceKillAfter` should not be negative', t => { + test('.kill() `forceKillAfterTimeout` should not be negative', t => { t.throws(() => { - execa('noop').kill('SIGTERM', {forceKillAfter: -1}); + execa('noop').kill('SIGTERM', {forceKillAfterTimeout: -1}); }, {instanceOf: TypeError, message: /non-negative integer/}); }); }