Skip to content
Myr

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

5 files+31 added−38 removed

modified:index.d.ts+3 added-8 removed
279 unmodified lines
280 }281282 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.285286 @default true287 */288 forceKill?: boolean;289290 /**284 Milliseconds to wait for the child process to terminate before sending `SIGKILL`.285286 Can be disabled with `false`.287288 @default 5000289 */295 forceKillAfter?: number;290 forceKillAfterTimeout?: number | false;291 }292293 interface ExecaChildPromise<StdoutErrorType> {294 catch<ResultType = never>(
126 unmodified lines
modified:index.js+10 added-6 removed
229 unmodified lines
230 kill('SIGKILL');231 }, timeout).unref();232}233234function shouldForceKill(signal, {forceKill}, killResult) {235 return isSigterm(signal) && forceKill !== false && killResult;234function shouldForceKill(signal, {forceKillAfterTimeout}, killResult) {235 return isSigterm(signal) && forceKillAfterTimeout !== false && killResult;236}237238function isSigterm(signal) {239 return signal === os.constants.signals.SIGTERM ||240 (typeof signal === 'string' && signal.toUpperCase() === 'SIGTERM');241}242243function 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})`);243function getForceKillAfterTimeout({forceKillAfterTimeout = true}) {244 if (forceKillAfterTimeout === true) {245 return DEFAULT_FORCE_KILL_TIMEOUT;246 }247248 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 }251248 return forceKillAfter;252 return forceKillAfterTimeout;253}254255const execa = (file, args, options) => {256 const parsed = handleArgs(file, args, options);
252 unmodified lines
modified:index.test-d.ts+3 added-4 removed
modified:readme.md+5 added-10 removed
115 unmodified lines
116// Kill a process with SIGTERM, and after 2 seconds, kill it with SIGKILL117const subprocess = execa('node');118setTimeout(() => {119 subprocess.kill('SIGTERM', {120 forceKillAfter: 2000120 forceKillAfterTimeout: 2000121 });122}, 1000);123```124
14 unmodified lines
139#### kill([signal], [options])140141Same 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`.142143##### options.forceKill143##### options.forceKillAfterTimeout144145Type: `boolean`<br>146Default: `true`147148If the first signal does not terminate the child process after a specified timeout, a `SIGKILL` signal will be sent to the process.149150##### options.forceKillAfter151152Type: `string`<br>145Type: `number | false`<br>146Default: `5000`147148Milliseconds to wait for the child process to terminate before sending `SIGKILL`.149150Can be disabled with `false`.151152#### cancel()153154Similar 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 lines
modified:test.js+10 added-10 removed
127 unmodified lines
128129// `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.131if (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');135136 subprocess.kill('SIGTERM', {forceKill: false, forceKillAfter: 50});136 subprocess.kill('SIGTERM', {forceKillAfterTimeout: false});137138 t.true(isRunning(subprocess.pid));139 subprocess.kill('SIGKILL');140 });141142 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');145146 subprocess.kill('SIGTERM', {forceKillAfter: 50});146 subprocess.kill('SIGTERM', {forceKillAfterTimeout: 50});147148 const {signal} = await t.throwsAsync(subprocess);149 t.is(signal, 'SIGKILL');150 });151152 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');155156 subprocess.kill('SIGTERM', {forceKill: true});156 subprocess.kill('SIGTERM', {forceKillAfterTimeout: true});157158 const {signal} = await t.throwsAsync(subprocess);159 t.is(signal, 'SIGKILL');160 });
7 unmodified lines
168 const {signal} = await t.throwsAsync(subprocess);169 t.is(signal, 'SIGKILL');170 });171172 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 });177178 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