Improve `cleanup` option (code quality, tests, documentation) (#225)
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 06f21c6 · Browse files · 6 files·+42 added−15 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+42 added−15 removed
- other:+42 added−15 removed
Provenance
- Commit
659f444- Tree
32221d0- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
06f21c6
6 files changed+42 added−15 removed
modified:fixtures/sub-process+2 added-1 removed
1 unmodified line2 'use strict';3 const execa = require('..');4 5 const cleanup = process.argv[2] === 'true';6+const detached = process.argv[3] === 'true';6-const subprocess = execa('node', ['./fixtures/forever'], {cleanup});7+const subprocess = execa('node', ['./fixtures/forever'], {cleanup, detached});8 process.send(subprocess.pid);added:fixtures/sub-process-exit+7 added
1+#!/usr/bin/env node2+'use strict';3+const execa = require('..');4+5+const cleanup = process.argv[2] === 'true';6+const detached = process.argv[3] === 'true';7+execa('node', ['./fixtures/noop'], {cleanup, detached});modified:index.d.ts+3 added-1 removed
104 unmodified lines105 */106 readonly reject?: boolean;107 108 /**109- Keep track of the spawned process and `kill` it when the parent process exits.109+ Kill the spawned process when the parent process exits unless either:110+ - the spawned process is [`detached`](https://nodejs.org/api/child_process.html#child_process_options_detached)111+ - the parent process is terminated abruptly, for example, with `SIGKILL` as opposed to `SIGTERM` or a normal exit112 113 @default true114 */115 readonly cleanup?: boolean;309 unmodified linesmodified:index.js+2 added-6 removed
91 unmodified lines92 }93 94 options.stdio = stdio(options);95 96- if (options.detached) {97- // #11598- options.cleanup = false;99- }100-96 if (process.platform === 'win32' && path.basename(command, '.exe') === 'cmd') {97 // #11698 args.unshift('/q');99 }154 unmodified lines254 } catch (error) {255 return Promise.reject(error);256 }257 258+ // #115259 let removeExitHandler;264- if (parsed.options.cleanup) {260+ if (parsed.options.cleanup && !parsed.options.detached) {261 removeExitHandler = onExit(() => {262 spawned.kill();263 });264 }211 unmodified linesmodified:readme.md+3 added-1 removed
259 unmodified lines260 261 Type: `boolean`<br>262 Default: `true`263 264-Keep track of the spawned process and `kill` it when the parent process exits.264+Kill the spawned process when the parent process exits unless either:265+ - the spawned process is [`detached`](https://nodejs.org/api/child_process.html#child_process_options_detached)266+ - the parent process is terminated abruptly, for example, with `SIGKILL` as opposed to `SIGTERM` or a normal exit267 268 #### encoding269 270 Type: `string | null`<br>85 unmodified linesmodified:test.js+25 added-6 removed
461 unmodified lines462 test(command, ' foo bar', 'foo', 'bar');463 test(command, ' baz quz', 'baz', 'quz');464 test(command, '');465 466-async function spawnAndKill(t, signal, cleanup, isKilled) {467- const subprocess = execa('sub-process', [cleanup], {stdio: ['ignore', 'ignore', 'ignore', 'ipc']});466+// When child process exits before parent process467+async function spawnAndExit(t, cleanup, detached) {468+ await t.notThrowsAsync(execa('sub-process-exit', [cleanup, detached]));469+}470 471+test('spawnAndExit', spawnAndExit, false, false);472+test('spawnAndExit cleanup', spawnAndExit, true, false);473+test('spawnAndExit detached', spawnAndExit, false, true);474+test('spawnAndExit cleanup detached', spawnAndExit, true, true);475+476+// When parent process exits before child process477+async function spawnAndKill(t, signal, cleanup, detached, isKilled) {478+ const subprocess = execa('sub-process', [cleanup, detached], {stdio: ['ignore', 'ignore', 'ignore', 'ipc']});479+480 const pid = await pEvent(subprocess, 'message');481 t.true(Number.isInteger(pid));482 t.true(isRunning(pid));483 2 unmodified lines486 await t.throwsAsync(subprocess);487 488 t.false(isRunning(subprocess.pid));489 t.is(isRunning(pid), !isKilled);490+491+ if (!isKilled) {492+ process.kill(pid, 'SIGKILL');493+ }494 }495 496 // Without `options.cleanup`:497 // - on Windows subprocesses are killed if `options.detached: false`, but not1 unmodified line499 // - on Linux subprocesses are never killed regardless of `options.detached`500 // With `options.cleanup`, subprocesses are always killed501 // - `options.cleanup` with SIGKILL is a noop, since it cannot be handled502 const exitIfWindows = process.platform === 'win32';488-test('cleanup true - SIGTERM', spawnAndKill, 'SIGTERM', 'true', true);489-test('cleanup false - SIGTERM', spawnAndKill, 'SIGTERM', 'false', exitIfWindows);490-test('cleanup true - SIGKILL', spawnAndKill, 'SIGKILL', 'true', exitIfWindows);491-test('cleanup false - SIGKILL', spawnAndKill, 'SIGKILL', 'false', exitIfWindows);503+test('spawnAndKill SIGTERM', spawnAndKill, 'SIGTERM', false, false, exitIfWindows);504+test('spawnAndKill SIGKILL', spawnAndKill, 'SIGKILL', false, false, exitIfWindows);505+test('spawnAndKill cleanup SIGTERM', spawnAndKill, 'SIGTERM', true, false, true);506+test('spawnAndKill cleanup SIGKILL', spawnAndKill, 'SIGKILL', true, false, exitIfWindows);507+test('spawnAndKill detached SIGTERM', spawnAndKill, 'SIGTERM', false, true, false);508+test('spawnAndKill detached SIGKILL', spawnAndKill, 'SIGKILL', false, true, false);509+test('spawnAndKill cleanup detached SIGTERM', spawnAndKill, 'SIGTERM', true, true, false);510+test('spawnAndKill cleanup detached SIGKILL', spawnAndKill, 'SIGKILL', true, true, false);511 512 if (process.platform !== 'win32') {513 test('write to fast-exit process', async t => {514 // Try-catch here is necessary, because this test is not 100% accurate178 unmodified lines