Rename `forceKillAfterTimeout` to `forceKillAfterDelay` (#723)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 195369f · Browse files · 6 files·+50 added−50 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+50 added−50 removed
- tests:+21 added−21 removed
- other:+29 added−29 removed
Provenance
- Commit
0ff8b34- Tree
ddbc284- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
195369f
6 files changed+50 added−50 removed
modified:lib/kill.js+14 added-14 removed
2 unmodified lines34567878910101112131415141516171819202021222324252526272829303132333132333435363637383940414041424344444546474830 unmodified lines2 unmodified linesimport {onExit} from 'signal-exit';
const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;
// Monkey-patches `childProcess.kill()` to add `forceKillAfterTimeout` behaviorexport const spawnedKill = (kill, {forceKillAfterTimeout}, controller, signal) => {// Monkey-patches `childProcess.kill()` to add `forceKillAfterDelay` behaviorexport const spawnedKill = (kill, {forceKillAfterDelay}, controller, signal) => { const killResult = kill(signal); setKillTimeout({kill, signal, forceKillAfterTimeout, killResult, controller}); setKillTimeout({kill, signal, forceKillAfterDelay, killResult, controller}); return killResult;};
const setKillTimeout = async ({kill, signal, forceKillAfterTimeout, killResult, controller}) => { if (!shouldForceKill(signal, forceKillAfterTimeout, killResult)) {const setKillTimeout = async ({kill, signal, forceKillAfterDelay, killResult, controller}) => { if (!shouldForceKill(signal, forceKillAfterDelay, killResult)) { return; }
try { await setTimeout(forceKillAfterTimeout, undefined, {signal: controller.signal}); await setTimeout(forceKillAfterDelay, undefined, {signal: controller.signal}); kill('SIGKILL'); } catch {}};
const shouldForceKill = (signal, forceKillAfterTimeout, killResult) => isSigterm(signal) && forceKillAfterTimeout !== false && killResult;const shouldForceKill = (signal, forceKillAfterDelay, killResult) => isSigterm(signal) && forceKillAfterDelay !== false && killResult;
const isSigterm = signal => signal === undefined || signal === os.constants.signals.SIGTERM || (typeof signal === 'string' && signal.toUpperCase() === 'SIGTERM');
export const normalizeForceKillAfterTimeout = forceKillAfterTimeout => { if (forceKillAfterTimeout === false) { return forceKillAfterTimeout;export const normalizeForceKillAfterDelay = forceKillAfterDelay => { if (forceKillAfterDelay === false) { return forceKillAfterDelay; }
if (forceKillAfterTimeout === true) { if (forceKillAfterDelay === true) { return DEFAULT_FORCE_KILL_TIMEOUT; }
if (!Number.isFinite(forceKillAfterTimeout) || forceKillAfterTimeout < 0) { throw new TypeError(`Expected the \`forceKillAfterTimeout\` option to be a non-negative integer, got \`${forceKillAfterTimeout}\` (${typeof forceKillAfterTimeout})`); if (!Number.isFinite(forceKillAfterDelay) || forceKillAfterDelay < 0) { throw new TypeError(`Expected the \`forceKillAfterDelay\` option to be a non-negative integer, got \`${forceKillAfterDelay}\` (${typeof forceKillAfterDelay})`); }
return forceKillAfterTimeout; return forceKillAfterDelay;};
const killAfterTimeout = async ({spawned, timeout, killSignal, context, controller}) => { await setTimeout(timeout, undefined, {signal: controller.signal});30 unmodified linesmodified:test/kill.js+21 added-21 removed
8 unmodified lines9 setFixtureDir();10 11 const TIMEOUT_REGEXP = /timed out after/;12 13-const spawnNoKillable = async (forceKillAfterTimeout, options) => {13+const spawnNoKillable = async (forceKillAfterDelay, options) => {14 const subprocess = execa('no-killable.js', {15 stdio: ['pipe', 'pipe', 'pipe', 'ipc'],16- forceKillAfterTimeout,16+ forceKillAfterDelay,17 ...options,18 });19 await pEvent(subprocess, 'message');20 return {subprocess};11 unmodified lines32 33 // `SIGTERM` cannot be caught on Windows, and it always aborts the process (like `SIGKILL` on Unix).34 // Therefore, this feature and those tests do not make sense on Windows.35 if (process.platform !== 'win32') {36- const testNoForceKill = async (t, forceKillAfterTimeout, killArgument) => {37- const {subprocess} = await spawnNoKillable(forceKillAfterTimeout);36+ const testNoForceKill = async (t, forceKillAfterDelay, killArgument) => {37+ const {subprocess} = await spawnNoKillable(forceKillAfterDelay);38 39 subprocess.kill(killArgument);40 41 await setTimeout(6e3);4 unmodified lines46 t.true(isTerminated);47 t.is(signal, 'SIGKILL');48 };49 50- test('`forceKillAfterTimeout: false` should not kill after a timeout', testNoForceKill, false);51- test('`forceKillAfterTimeout` should not kill after a timeout with other signals', testNoForceKill, true, 'SIGINT');50+ test('`forceKillAfterDelay: false` should not kill after a timeout', testNoForceKill, false);51+ test('`forceKillAfterDelay` should not kill after a timeout with other signals', testNoForceKill, true, 'SIGINT');52 53- const testForceKill = async (t, forceKillAfterTimeout, killArgument) => {54- const {subprocess} = await spawnNoKillable(forceKillAfterTimeout);53+ const testForceKill = async (t, forceKillAfterDelay, killArgument) => {54+ const {subprocess} = await spawnNoKillable(forceKillAfterDelay);55 56 subprocess.kill(killArgument);57 58 const {isTerminated, signal} = await t.throwsAsync(subprocess);59 t.true(isTerminated);60 t.is(signal, 'SIGKILL');61 };62 63- test('`forceKillAfterTimeout: number` should kill after a timeout', testForceKill, 50);64- test('`forceKillAfterTimeout: true` should kill after a timeout', testForceKill, true);65- test('`forceKillAfterTimeout: undefined` should kill after a timeout', testForceKill, undefined);66- test('`forceKillAfterTimeout` should kill after a timeout with the killSignal', testForceKill, 50, 'SIGTERM');63+ test('`forceKillAfterDelay: number` should kill after a timeout', testForceKill, 50);64+ test('`forceKillAfterDelay: true` should kill after a timeout', testForceKill, true);65+ test('`forceKillAfterDelay: undefined` should kill after a timeout', testForceKill, undefined);66+ test('`forceKillAfterDelay` should kill after a timeout with the killSignal', testForceKill, 50, 'SIGTERM');67 68- const testInvalidForceKill = async (t, forceKillAfterTimeout) => {68+ const testInvalidForceKill = async (t, forceKillAfterDelay) => {69 t.throws(() => {70- execa('empty.js', {forceKillAfterTimeout});70+ execa('empty.js', {forceKillAfterDelay});71 }, {instanceOf: TypeError, message: /non-negative integer/});72 };73 74- test('`forceKillAfterTimeout` should not be NaN', testInvalidForceKill, Number.NaN);75- test('`forceKillAfterTimeout` should not be negative', testInvalidForceKill, -1);74+ test('`forceKillAfterDelay` should not be NaN', testInvalidForceKill, Number.NaN);75+ test('`forceKillAfterDelay` should not be negative', testInvalidForceKill, -1);76 77- test('`forceKillAfterTimeout` works with the "signal" option', async t => {77+ test('`forceKillAfterDelay` works with the "signal" option', async t => {78 const abortController = new AbortController();79 const {subprocess} = await spawnNoKillable(1, {signal: abortController.signal});80 abortController.abort();81 const {isTerminated, signal, isCanceled} = await t.throwsAsync(subprocess);1 unmodified line83 t.is(signal, undefined);84 t.true(isCanceled);85 });86 87- test.serial('`forceKillAfterTimeout` works with the "timeout" option', async t => {87+ test.serial('`forceKillAfterDelay` works with the "timeout" option', async t => {88 const {subprocess} = await spawnNoKillable(1, {timeout: 2e3});89 const {isTerminated, signal, timedOut} = await t.throwsAsync(subprocess);90 t.true(isTerminated);91 t.is(signal, 'SIGTERM');92 t.true(timedOut);93 });94 95- test('`forceKillAfterTimeout` works with the "maxBuffer" option', async t => {95+ test('`forceKillAfterDelay` works with the "maxBuffer" option', async t => {96 const {subprocess} = await spawnNoKillable(1, {maxBuffer: 1});97 const {isTerminated, signal} = await t.throwsAsync(subprocess);98 t.false(isTerminated);99 t.is(signal, undefined);100 });101 102- test('`forceKillAfterTimeout` works with "error" events on childProcess', async t => {102+ test('`forceKillAfterDelay` works with "error" events on childProcess', async t => {103 const {subprocess} = await spawnNoKillable(1);104 subprocess.emit('error', new Error('test'));105 const {isTerminated, signal} = await t.throwsAsync(subprocess);106 t.false(isTerminated);107 t.is(signal, undefined);108 });109 110- test('`forceKillAfterTimeout` works with "error" events on childProcess.stdout', async t => {110+ test('`forceKillAfterDelay` works with "error" events on childProcess.stdout', async t => {111 const {subprocess} = await spawnNoKillable(1);112 subprocess.stdout.destroy(new Error('test'));113 const {isTerminated, signal} = await t.throwsAsync(subprocess);114 t.false(isTerminated);193 unmodified linesmodified:index.d.ts+1 added-1 removed
484 unmodified lines485 Also, this does not work on Windows, because Windows [doesn't support signals](https://nodejs.org/api/process.html#process_signal_events): `SIGKILL` and `SIGTERM` both terminate the process immediately. Other packages (such as [`taskkill`](https://github.com/sindresorhus/taskkill)) can be used to achieve fail-safe termination on Windows.486 487 @default 5000488 */489- forceKillAfterTimeout?: number | false;489+ forceKillAfterDelay?: number | false;490 491 /**492 If `true`, no quoting or escaping of arguments is done on Windows. Ignored on other platforms. This is set to `true` automatically when the `shell` option is `true`.493 792 unmodified linesmodified:index.js+4 added-4 removed
8 unmodified lines9 import {makeError} from './lib/error.js';10 import {handleInputAsync, pipeOutputAsync} from './lib/stdio/async.js';11 import {handleInputSync, pipeOutputSync} from './lib/stdio/sync.js';12 import {normalizeStdioNode} from './lib/stdio/normalize.js';13-import {spawnedKill, validateTimeout, normalizeForceKillAfterTimeout} from './lib/kill.js';13+import {spawnedKill, validateTimeout, normalizeForceKillAfterDelay} from './lib/kill.js';14 import {addPipeMethods} from './lib/pipe.js';15 import {getSpawnedResult, makeAllStream} from './lib/stream.js';16 import {mergePromise} from './lib/promise.js';17 import {joinCommand, parseCommand, parseTemplates, getEscapedCommand} from './lib/command.js';34 unmodified lines52 const options = addDefaultOptions(initialOptions);53 validateTimeout(options);54 options.shell = normalizeFileUrl(options.shell);55 options.env = getEnv(options);56- options.forceKillAfterTimeout = normalizeForceKillAfterTimeout(options.forceKillAfterTimeout);56+ options.forceKillAfterDelay = normalizeForceKillAfterDelay(options.forceKillAfterDelay);57 58 if (process.platform === 'win32' && path.basename(file, '.exe') === 'cmd') {59 // #11660 args.unshift('/q');19 unmodified lines80 all = false,81 windowsHide = true,82 verbose = verboseDefault,83 killSignal = 'SIGTERM',84- forceKillAfterTimeout = true,84+ forceKillAfterDelay = true,85 ...options86 }) => ({87 ...options,88 maxBuffer,10 unmodified lines99 all,100 windowsHide,101 verbose,102 killSignal,103- forceKillAfterTimeout,103+ forceKillAfterDelay,104 });105 106 const handleOutput = (options, value) => {107 if (value === undefined || value === null) {236 unmodified linesmodified:index.test-d.ts+8 added-8 removed
1185 unmodified lines1186 execa('unicorns', {killSignal: 'SIGTERM'});1187 execaSync('unicorns', {killSignal: 'SIGTERM'});1188 execa('unicorns', {killSignal: 9});1189 execaSync('unicorns', {killSignal: 9});1190-execa('unicorns', {forceKillAfterTimeout: false});1191-execaSync('unicorns', {forceKillAfterTimeout: false});1192-execa('unicorns', {forceKillAfterTimeout: 42});1193-execaSync('unicorns', {forceKillAfterTimeout: 42});1194-execa('unicorns', {forceKillAfterTimeout: undefined});1195-execaSync('unicorns', {forceKillAfterTimeout: undefined});1196-expectError(execa('unicorns', {forceKillAfterTimeout: 'true'}));1197-expectError(execaSync('unicorns', {forceKillAfterTimeout: 'true'}));1190+execa('unicorns', {forceKillAfterDelay: false});1191+execaSync('unicorns', {forceKillAfterDelay: false});1192+execa('unicorns', {forceKillAfterDelay: 42});1193+execaSync('unicorns', {forceKillAfterDelay: 42});1194+execa('unicorns', {forceKillAfterDelay: undefined});1195+execaSync('unicorns', {forceKillAfterDelay: undefined});1196+expectError(execa('unicorns', {forceKillAfterDelay: 'true'}));1197+expectError(execaSync('unicorns', {forceKillAfterDelay: 'true'}));1198 execa('unicorns', {signal: new AbortController().signal});1199 expectError(execaSync('unicorns', {signal: new AbortController().signal}));1200 execa('unicorns', {windowsVerbatimArguments: true});1201 execaSync('unicorns', {windowsVerbatimArguments: true});112 unmodified linesmodified:readme.md+2 added-2 removed
48 unmodified lines49 - [Cleans up](#cleanup) child processes when the parent process ends.50 - Redirect [`stdin`](#stdin)/[`stdout`](#stdout-1)/[`stderr`](#stderr-1) from/to files, streams, iterables, strings, `Uint8Array` or [objects](docs/transform.md#object-mode).51 - [Transform](docs/transform.md) `stdin`/`stdout`/`stderr` with simple functions.52 - Iterate over [each text line](docs/transform.md#binary-data) output by the process.53-- [Fail-safe process termination](#forcekillaftertimeout).53+- [Fail-safe process termination](#forcekillafterdelay).54 - Get [interleaved output](#all) from `stdout` and `stderr` similar to what is printed on the terminal.55 - [Strips the final newline](#stripfinalnewline) from the output so you don't have to do `stdout.trim()`.56 - Convenience methods to pipe processes' [input](#input) and [output](#redirect-output-to-a-file).57 - Can specify file and arguments [as a single string](#execacommandcommand-options) without a shell.695 unmodified lines753 Default: `SIGTERM`754 755 Signal value to be used when the spawned process will be killed.756 757-#### forceKillAfterTimeout757+#### forceKillAfterDelay758 759 Type: `number | false`\760 Default: `5000`761 179 unmodified lines