Merge `forceKill` and `forceKillAfter` options (#285)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by Sindre Sorhus · parent 7e2c835 · Browse files · 5 files·+31 added−38 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+31 added−38 removed
- other:+31 added−38 removed
Provenance
- Commit
0336882- Tree
cd15efb- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
7e2c835
5 files changed+31 added−38 removed
modified:index.d.ts+3 added-8 removed
279 unmodified lines280 }281 282 interface KillOptions {283 /**284- If the first signal does not terminate the child process after a specified timeout, a `SIGKILL` signal will be sent to the process.285-286- @default true287- */288- forceKill?: boolean;289-290- /**284 Milliseconds to wait for the child process to terminate before sending `SIGKILL`.285 286+ Can be disabled with `false`.287+288 @default 5000289 */295- forceKillAfter?: number;290+ forceKillAfterTimeout?: number | false;291 }292 293 interface ExecaChildPromise<StdoutErrorType> {294 catch<ResultType = never>(126 unmodified linesmodified:index.js+10 added-6 removed
229 unmodified lines230 kill('SIGKILL');231 }, timeout).unref();232 }233 234-function shouldForceKill(signal, {forceKill}, killResult) {235- return isSigterm(signal) && forceKill !== false && killResult;234+function shouldForceKill(signal, {forceKillAfterTimeout}, killResult) {235+ return isSigterm(signal) && forceKillAfterTimeout !== false && killResult;236 }237 238 function isSigterm(signal) {239 return signal === os.constants.signals.SIGTERM ||240 (typeof signal === 'string' && signal.toUpperCase() === 'SIGTERM');241 }242 243-function getForceKillAfterTimeout({forceKillAfter = DEFAULT_FORCE_KILL_TIMEOUT}) {244- if (!Number.isInteger(forceKillAfter) || forceKillAfter < 0) {245- throw new TypeError(`Expected the \`forceKillAfter\` option to be a non-negative integer, got \`${forceKillAfter}\` (${typeof forceKillAfter})`);243+function getForceKillAfterTimeout({forceKillAfterTimeout = true}) {244+ if (forceKillAfterTimeout === true) {245+ return DEFAULT_FORCE_KILL_TIMEOUT;246 }247 248+ if (!Number.isInteger(forceKillAfterTimeout) || forceKillAfterTimeout < 0) {249+ throw new TypeError(`Expected the \`forceKillAfterTimeout\` option to be a non-negative integer, got \`${forceKillAfterTimeout}\` (${typeof forceKillAfterTimeout})`);250+ }251+248- return forceKillAfter;252+ return forceKillAfterTimeout;253 }254 255 const execa = (file, args, options) => {256 const parsed = handleArgs(file, args, options);252 unmodified linesmodified:index.test-d.ts+3 added-4 removed
121 unmodified linesExpand all12212312412512612712812912612712812913013113236 unmodified lines121 unmodified linesExpand allexeca('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: false});execa('unicorns').kill('SIGKILL', {forceKillAfterTimeout: 42});execa('unicorns').kill('SIGKILL', {forceKillAfterTimeout: undefined});
expectType<ExecaChildProcess<string>>(execa('unicorns'));expectType<ExecaReturnValue<string>>(await execa('unicorns'));expectType<ExecaReturnValue<string>>(36 unmodified linesmodified:readme.md+5 added-10 removed
115 unmodified lines116 // Kill a process with SIGTERM, and after 2 seconds, kill it with SIGKILL117 const subprocess = execa('node');118 setTimeout(() => {119 subprocess.kill('SIGTERM', {120- forceKillAfter: 2000120+ forceKillAfterTimeout: 2000121 });122 }, 1000);123 ```124 14 unmodified lines139 #### kill([signal], [options])140 141 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`.142 143-##### options.forceKill143+##### options.forceKillAfterTimeout144 145-Type: `boolean`<br>146-Default: `true`147-148-If the first signal does not terminate the child process after a specified timeout, a `SIGKILL` signal will be sent to the process.149-150-##### options.forceKillAfter151-152-Type: `string`<br>145+Type: `number | false`<br>146 Default: `5000`147 148 Milliseconds to wait for the child process to terminate before sending `SIGKILL`.149 150+Can be disabled with `false`.151+152 #### cancel()153 154 Similar to [`childProcess.kill()`](https://nodejs.org/api/child_process.html#child_process_subprocess_kill_signal). This is preferred when cancelling the child process execution as the error is more descriptive and [`childProcessResult.isCanceled`](#iscanceled) is set to `true`.155 317 unmodified linesmodified:test.js+10 added-10 removed
127 unmodified lines128 129 // `SIGTERM` cannot be caught on Windows, and it always aborts the process (like `SIGKILL` on Unix).130 // Therefore, this feature and those tests do not make sense on Windows.131 if (process.platform !== 'win32') {132- test('`forceKill: false` should not kill after a timeout', async t => {132+ test('`forceKillAfterTimeout: false` should not kill after a timeout', async t => {133 const subprocess = execa('node', ['fixtures/no-killable'], {stdio: ['ipc']});134 await pEvent(subprocess, 'message');135 136- subprocess.kill('SIGTERM', {forceKill: false, forceKillAfter: 50});136+ subprocess.kill('SIGTERM', {forceKillAfterTimeout: false});137 138 t.true(isRunning(subprocess.pid));139 subprocess.kill('SIGKILL');140 });141 142- test('`forceKillAfter: number` should kill after a timeout', async t => {142+ test('`forceKillAfterTimeout: number` should kill after a timeout', async t => {143 const subprocess = execa('node', ['fixtures/no-killable'], {stdio: ['ipc']});144 await pEvent(subprocess, 'message');145 146- subprocess.kill('SIGTERM', {forceKillAfter: 50});146+ subprocess.kill('SIGTERM', {forceKillAfterTimeout: 50});147 148 const {signal} = await t.throwsAsync(subprocess);149 t.is(signal, 'SIGKILL');150 });151 152- test('`forceKill: true` should kill after a timeout', async t => {152+ test('`forceKillAfterTimeout: true` should kill after a timeout', async t => {153 const subprocess = execa('node', ['fixtures/no-killable'], {stdio: ['ipc']});154 await pEvent(subprocess, 'message');155 156- subprocess.kill('SIGTERM', {forceKill: true});156+ subprocess.kill('SIGTERM', {forceKillAfterTimeout: true});157 158 const {signal} = await t.throwsAsync(subprocess);159 t.is(signal, 'SIGKILL');160 });7 unmodified lines168 const {signal} = await t.throwsAsync(subprocess);169 t.is(signal, 'SIGKILL');170 });171 172- test('`forceKillAfter` should not be a float', t => {172+ test('`forceKillAfterTimeout` should not be a float', t => {173 t.throws(() => {174- execa('noop').kill('SIGTERM', {forceKillAfter: 0.5});174+ execa('noop').kill('SIGTERM', {forceKillAfterTimeout: 0.5});175 }, {instanceOf: TypeError, message: /non-negative integer/});176 });177 178- test('`forceKillAfter` should not be negative', t => {178+ test('`forceKillAfterTimeout` should not be negative', t => {179 t.throws(() => {180- execa('noop').kill('SIGTERM', {forceKillAfter: -1});180+ execa('noop').kill('SIGTERM', {forceKillAfterTimeout: -1});181 }, {instanceOf: TypeError, message: /non-negative integer/});182 });183 }184 621 unmodified lines