Skip to content
Myr

Add `error.isForcefullyTerminated` (#1111)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent ff02af6 · Browse files · 16 files+228 added−55 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

16 files+228 added−55 removed

  • tests:+112 added−34 removed
  • other:+116 added−21 removed

16 files+228 added−55 removed

modified:docs/api.md+10 added
667 unmodified lines
668- [Another process](termination.md#inter-process-termination). This case is [not supported on Windows](https://nodejs.org/api/process.html#signal-events).669670[More info.](termination.md#signal-name-and-description)671672### error.isForcefullyTerminated673674_Type:_ `boolean`675676Whether the subprocess was terminated by the [`SIGKILL`](termination.md#sigkill) signal sent by the [`forceKillAfterDelay`](#optionsforcekillafterdelay) option.677678[More info.](termination.md#forceful-termination)679680### error.exitCode681682_Type:_ `number | undefined`683
351 unmodified lines
1035_Default:_ `5000`10361037If the subprocess is terminated but does not exit, forcefully exit it by sending [`SIGKILL`](https://en.wikipedia.org/wiki/Signal_(IPC)#SIGKILL).10381039When this happens, [`error.isForcefullyTerminated`](#errorisforcefullyterminated) becomes `true`.10401041[More info.](termination.md#forceful-termination)10421043### options.killSignal1044
125 unmodified lines
modified:docs/termination.md+2 added
150 unmodified lines
151If the subprocess is terminated but does not exit, [`SIGKILL`](#sigkill) is automatically sent to forcefully terminate it.152153The grace period is set by the [`forceKillAfterDelay`](api.md#optionsforcekillafterdelay) option, which is 5 seconds by default. This feature can be disabled with `false`.154155The [`error.isForcefullyTerminated`](api.md#errorisforcefullyterminated) boolean property can be used to check whether a subprocess was forcefully terminated by the `forceKillAfterDelay` option.156157This works when the subprocess is terminated by either:158- Calling [`subprocess.kill()`](api.md#subprocesskillsignal-error) with no arguments.159- The [`cancelSignal`](#canceling), [`timeout`](#timeout), [`maxBuffer`](output.md#big-output) or [`cleanup`](#current-process-exit) option.160
64 unmodified lines
modified:lib/methods/main-async.js+5 added-3 removed
modified:lib/methods/main-sync.js+1 added
154 unmodified lines
155 escapedCommand,156 timedOut,157 isCanceled: false,158 isMaxBuffer,159 isForcefullyTerminated: false,160 exitCode,161 signal,162 stdio,163 all,
5 unmodified lines
modified:lib/resolve/exit-async.js+7 added-1 removed
6 unmodified lines
7// Since the `exit` event contains the signal name, we want to make sure we are listening for it.8// This function also takes into account the following unlikely cases:9// - `exit` being emitted in the same microtask as `spawn`10// - `error` being emitted multiple times11export const waitForExit = async subprocess => {11export const waitForExit = async (subprocess, context) => {12 const [exitCode, signal] = await waitForExitOrError(subprocess);13 context.isForcefullyTerminated ??= false;14 return [exitCode, signal];15};1617const waitForExitOrError = async subprocess => {18 const [spawnPayload, exitPayload] = await Promise.allSettled([19 once(subprocess, 'spawn'),20 once(subprocess, 'exit'),21 ]);
33 unmodified lines
modified:lib/resolve/wait-subprocess.js+1 added-1 removed
31 unmodified lines
32 originalStreams,33 onInternalError,34 controller,35}) => {36 const exitPromise = waitForExit(subprocess);36 const exitPromise = waitForExit(subprocess, context);37 const streamInfo = {38 originalStreams,39 fileDescriptors,40 subprocess,
88 unmodified lines
modified:lib/return/message.js+36 added-5 removed
2 unmodified lines
3import {isUint8Array, uint8ArrayToString} from '../utils/uint-array.js';4import {fixCwdError} from '../arguments/cwd.js';5import {escapeLines} from '../arguments/escape.js';6import {getMaxBufferMessage} from '../io/max-buffer.js';7import {getSignalDescription} from '../terminate/signal.js';8import {DiscardedError, isExecaError} from './final-error.js';910// Computes `error.message`, `error.shortMessage` and `error.originalMessage`11export const createMessages = ({
7 unmodified lines
19 escapedCommand,20 timedOut,21 isCanceled,22 isMaxBuffer,23 isForcefullyTerminated,24 forceKillAfterDelay,25 killSignal,26 maxBuffer,27 timeout,28 cwd,29}) => {
8 unmodified lines
38 signal,39 signalDescription,40 exitCode,41 isCanceled,42 isForcefullyTerminated,43 forceKillAfterDelay,44 killSignal,45 });46 const originalMessage = getOriginalMessage(originalError, cwd);47 const suffix = originalMessage === undefined ? '' : `\n${originalMessage}`;48 const shortMessage = `${prefix}: ${escapedCommand}${suffix}`;
9 unmodified lines
58 .join('\n\n');59 return {originalMessage, shortMessage, message};60};6155const getErrorPrefix = ({originalError, timedOut, timeout, isMaxBuffer, maxBuffer, errorCode, signal, signalDescription, exitCode, isCanceled}) => {62const getErrorPrefix = ({63 originalError,64 timedOut,65 timeout,66 isMaxBuffer,67 maxBuffer,68 errorCode,69 signal,70 signalDescription,71 exitCode,72 isCanceled,73 isForcefullyTerminated,74 forceKillAfterDelay,75 killSignal,76}) => {77 const forcefulSuffix = getForcefulSuffix(isForcefullyTerminated, forceKillAfterDelay);7879 if (timedOut) {57 return `Command timed out after ${timeout} milliseconds`;80 return `Command timed out after ${timeout} milliseconds${forcefulSuffix}`;81 }8283 if (isCanceled) {61 return 'Command was canceled';84 return `Command was canceled${forcefulSuffix}`;85 }8687 if (isMaxBuffer) {65 return getMaxBufferMessage(originalError, maxBuffer);88 return `${getMaxBufferMessage(originalError, maxBuffer)}${forcefulSuffix}`;89 }9091 if (errorCode !== undefined) {69 return `Command failed with ${errorCode}`;92 return `Command failed with ${errorCode}${forcefulSuffix}`;93 }9495 if (isForcefullyTerminated) {96 return `Command was killed with ${killSignal} (${getSignalDescription(killSignal)})${forcefulSuffix}`;97 }9899 if (signal !== undefined) {100 return `Command was killed with ${signal} (${signalDescription})`;101 }102
3 unmodified lines
106107 return 'Command failed';108};109110const getForcefulSuffix = (isForcefullyTerminated, forceKillAfterDelay) => isForcefullyTerminated111 ? ` and was forcefully terminated after ${forceKillAfterDelay} milliseconds`112 : '';113114const getOriginalMessage = (originalError, cwd) => {115 if (originalError instanceof DiscardedError) {116 return;117 }
27 unmodified lines
modified:lib/return/result.js+19 added-3 removed
1import {signalsByName} from 'human-signals';1import {getSignalDescription} from '../terminate/signal.js';2import {getDurationMs} from './duration.js';3import {getFinalError} from './final-error.js';4import {createMessages} from './message.js';5
15 unmodified lines
21 timedOut: false,22 isCanceled: false,23 isTerminated: false,24 isMaxBuffer: false,25 isForcefullyTerminated: false,26 exitCode: 0,27 stdout: stdio[1],28 stderr: stdio[2],29 all,
18 unmodified lines
48 startTime,49 timedOut: false,50 isCanceled: false,51 isMaxBuffer: false,52 isForcefullyTerminated: false,53 stdio: Array.from({length: fileDescriptors.length}),54 ipcOutput: [],55 options,56 isSync,
7 unmodified lines
64 startTime,65 timedOut,66 isCanceled,67 isMaxBuffer,68 isForcefullyTerminated,69 exitCode: rawExitCode,70 signal: rawSignal,71 stdio,72 all,73 ipcOutput,71 options: {timeoutDuration, timeout = timeoutDuration, cwd, maxBuffer},74 options: {75 timeoutDuration,76 timeout = timeoutDuration,77 forceKillAfterDelay,78 killSignal,79 cwd,80 maxBuffer,81 },82 isSync,83}) => {84 const {exitCode, signal, signalDescription} = normalizeExitPayload(rawExitCode, rawSignal);85 const {originalMessage, shortMessage, message} = createMessages({
7 unmodified lines
93 escapedCommand,94 timedOut,95 isCanceled,96 isMaxBuffer,97 isForcefullyTerminated,98 forceKillAfterDelay,99 killSignal,100 maxBuffer,101 timeout,102 cwd,103 });
5 unmodified lines
109 startTime,110 timedOut,111 isCanceled,112 isMaxBuffer,113 isForcefullyTerminated,114 exitCode,115 signal,116 signalDescription,117 stdio,
13 unmodified lines
131 startTime,132 timedOut,133 isCanceled,134 isMaxBuffer,135 isForcefullyTerminated,136 exitCode,137 signal,138 signalDescription,139 stdio,
13 unmodified lines
153 timedOut,154 isCanceled,155 isTerminated: signal !== undefined,156 isMaxBuffer,157 isForcefullyTerminated,158 exitCode,159 signal,160 signalDescription,161 code: error.cause?.code,
11 unmodified lines
173// We normalize them to `undefined`174const normalizeExitPayload = (rawExitCode, rawSignal) => {175 const exitCode = rawExitCode === null ? undefined : rawExitCode;176 const signal = rawSignal === null ? undefined : rawSignal;161 const signalDescription = signal === undefined ? undefined : signalsByName[rawSignal].description;177 const signalDescription = signal === undefined ? undefined : getSignalDescription(rawSignal);178 return {exitCode, signal, signalDescription};179};
modified:lib/terminate/kill.js+20 added-8 removed
21 unmodified lines
22const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;2324// Monkey-patches `subprocess.kill()` to add `forceKillAfterDelay` behavior and `.kill(error)`25export const subprocessKill = (26 {kill, options: {forceKillAfterDelay, killSignal}, onInternalError, controller},26 {kill, options: {forceKillAfterDelay, killSignal}, onInternalError, context, controller},27 signalOrError,28 errorArgument,29) => {30 const {signal, error} = parseKillArguments(signalOrError, errorArgument, killSignal);
4 unmodified lines
35 signal,36 forceKillAfterDelay,37 killSignal,38 killResult,39 context,40 controller,41 });42 return killResult;43};
22 unmodified lines
66 onInternalError.reject(error);67 }68};6969const setKillTimeout = async ({kill, signal, forceKillAfterDelay, killSignal, killResult, controller}) => {70 if (!shouldForceKill(signal, forceKillAfterDelay, killSignal, killResult)) {70const setKillTimeout = async ({kill, signal, forceKillAfterDelay, killSignal, killResult, context, controller}) => {71 if (signal === killSignal && killResult) {72 killOnTimeout({73 kill,74 forceKillAfterDelay,75 context,76 controllerSignal: controller.signal,77 });78 }79};8081// Forcefully terminate a subprocess after a timeout82export const killOnTimeout = async ({kill, forceKillAfterDelay, context, controllerSignal}) => {83 if (forceKillAfterDelay === false) {84 return;85 }8687 try {75 await setTimeout(forceKillAfterDelay, undefined, {signal: controller.signal});76 kill('SIGKILL');88 await setTimeout(forceKillAfterDelay, undefined, {signal: controllerSignal});89 if (kill('SIGKILL')) {90 context.isForcefullyTerminated ??= true;91 }92 } catch {}93};9480const shouldForceKill = (signal, forceKillAfterDelay, killSignal, killResult) => signal === killSignal81 && forceKillAfterDelay !== false82 && killResult;
modified:lib/terminate/signal.js+4 added
1import {constants} from 'node:os';2import {signalsByName} from 'human-signals';34// Normalize signals for comparison purpose.5// Also validate the signal exists.6export const normalizeKillSignal = killSignal => {
57 unmodified lines
6465const getAvailableSignalIntegers = () => [...new Set(Object.values(constants.signals)66 .sort((signalInteger, signalIntegerTwo) => signalInteger - signalIntegerTwo))]67 .join(', ');6869// Human-friendly description of a signal70export const getSignalDescription = signal => signalsByName[signal].description;
modified:test/return/result.js+2 added
18 unmodified lines
19 'timedOut',20 'isCanceled',21 'isTerminated',22 'isMaxBuffer',23 'isForcefullyTerminated',24 'exitCode',25 'stdout',26 'stderr',27 'all',
21 unmodified lines
49 'timedOut',50 'isCanceled',51 'isTerminated',52 'isMaxBuffer',53 'isForcefullyTerminated',54 'exitCode',55 'stdout',56 'stderr',57 'all',
88 unmodified lines
modified:test/terminate/cancel.js+9 added-9 removed
122 unmodified lines
123});124125test('calling abort with no argument keeps error properties', async t => {126 const abortController = new AbortController();127 const subprocess = execa('noop.js', {cancelSignal: abortController.signal});127 const subprocess = execa('empty.js', {cancelSignal: abortController.signal});128 abortController.abort();129 const {cause, originalMessage, shortMessage, message} = await t.throwsAsync(subprocess);130 t.is(cause.message, 'This operation was aborted');131 t.is(cause.name, 'AbortError');132 t.is(originalMessage, 'This operation was aborted');133 t.is(shortMessage, 'Command was canceled: noop.js\nThis operation was aborted');134 t.is(message, 'Command was canceled: noop.js\nThis operation was aborted');133 t.is(shortMessage, 'Command was canceled: empty.js\nThis operation was aborted');134 t.is(message, 'Command was canceled: empty.js\nThis operation was aborted');135});136137test('calling abort with an error instance keeps error properties', async t => {138 const abortController = new AbortController();139 const subprocess = execa('noop.js', {cancelSignal: abortController.signal});139 const subprocess = execa('empty.js', {cancelSignal: abortController.signal});140 const error = new Error(foobarString);141 error.code = foobarString;142 abortController.abort(error);143 const {cause, originalMessage, shortMessage, message, code} = await t.throwsAsync(subprocess);144 t.is(cause, error);145 t.is(originalMessage, foobarString);146 t.is(shortMessage, `Command was canceled: noop.js\n${foobarString}`);147 t.is(message, `Command was canceled: noop.js\n${foobarString}`);146 t.is(shortMessage, `Command was canceled: empty.js\n${foobarString}`);147 t.is(message, `Command was canceled: empty.js\n${foobarString}`);148 t.is(code, foobarString);149});150151test('calling abort with null keeps error properties', async t => {152 const abortController = new AbortController();153 const subprocess = execa('noop.js', {cancelSignal: abortController.signal});153 const subprocess = execa('empty.js', {cancelSignal: abortController.signal});154 abortController.abort(null);155 const {cause, originalMessage, shortMessage, message} = await t.throwsAsync(subprocess);156 t.is(cause, null);157 t.is(originalMessage, 'null');158 t.is(shortMessage, 'Command was canceled: noop.js\nnull');159 t.is(message, 'Command was canceled: noop.js\nnull');158 t.is(shortMessage, 'Command was canceled: empty.js\nnull');159 t.is(message, 'Command was canceled: empty.js\nnull');160});161162test('calling abort twice should show the same behaviour as calling it once', async t => {163 const abortController = new AbortController();
28 unmodified lines
modified:test/terminate/kill-force.js+101 added-25 removed
5 unmodified lines
6import isRunning from 'is-running';7import {execa} from '../../index.js';8import {setFixtureDirectory} from '../helpers/fixtures-directory.js';9import {assertMaxListeners} from '../helpers/listeners.js';10import {foobarString} from '../helpers/input.js';11import {getEarlyErrorSubprocess} from '../helpers/early-error.js';1213setFixtureDirectory();1415const isWindows = process.platform === 'win32';
7 unmodified lines
23 await subprocess.getOneMessage();24 return {subprocess};25};2627const noKillableSimpleOptions = {killSignal: 'SIGWINCH', forceKillAfterDelay: 1};25const spawnNoKillableSimple = options => execa('forever.js', {killSignal: 'SIGWINCH', forceKillAfterDelay: 1, ...options});28const spawnNoKillableSimple = options => execa('forever.js', {...noKillableSimpleOptions, ...options});2930test('kill("SIGKILL") should terminate cleanly', async t => {31 const {subprocess} = await spawnNoKillable();3233 subprocess.kill('SIGKILL');3432 const {isTerminated, signal} = await t.throwsAsync(subprocess);35 const {isTerminated, signal, isForcefullyTerminated, shortMessage} = await t.throwsAsync(subprocess);36 t.true(isTerminated);37 t.is(signal, 'SIGKILL');38 t.false(isForcefullyTerminated);39 t.is(shortMessage, 'Command was killed with SIGKILL (Forced termination): no-killable.js');40});4142const testInvalidForceKill = async (t, forceKillAfterDelay) => {43 t.throws(() => {
7 unmodified lines
51// `SIGTERM` cannot be caught on Windows, and it always aborts the subprocess (like `SIGKILL` on Unix).52// Therefore, this feature and those tests must be different on Windows.53if (isWindows) {54 test('Can call `.kill()` with `forceKillAfterDelay` on Windows', async t => {50 const {subprocess} = await spawnNoKillable(1);55 const {subprocess} = await spawnNoKillable();56 subprocess.kill();5753 const {isTerminated, signal} = await t.throwsAsync(subprocess);58 const {isTerminated, signal, isForcefullyTerminated, shortMessage} = await t.throwsAsync(subprocess);59 t.true(isTerminated);60 t.is(signal, 'SIGTERM');61 t.false(isForcefullyTerminated);62 t.is(shortMessage, 'Command was killed with SIGTERM (Termination): no-killable.js');63 });64} else {65 const testNoForceKill = async (t, forceKillAfterDelay, killArgument, options) => {66 const {subprocess} = await spawnNoKillable(forceKillAfterDelay, options);
3 unmodified lines
70 await setTimeout(6e3);71 t.true(isRunning(subprocess.pid));72 subprocess.kill('SIGKILL');7367 const {isTerminated, signal} = await t.throwsAsync(subprocess);74 const {isTerminated, signal, isForcefullyTerminated} = await t.throwsAsync(subprocess);75 t.true(isTerminated);76 t.is(signal, 'SIGKILL');77 t.false(isForcefullyTerminated);78 };7980 test('`forceKillAfterDelay: false` should not kill after a timeout', testNoForceKill, false);81 test('`forceKillAfterDelay` should not kill after a timeout with other signals', testNoForceKill, true, 'SIGINT');82 test('`forceKillAfterDelay` should not kill after a timeout with wrong killSignal string', testNoForceKill, true, 'SIGTERM', {killSignal: 'SIGINT'});83 test('`forceKillAfterDelay` should not kill after a timeout with wrong killSignal number', testNoForceKill, true, constants.signals.SIGTERM, {killSignal: constants.signals.SIGINT});8485 // eslint-disable-next-line max-params77 const testForceKill = async (t, forceKillAfterDelay, killArgument, options) => {86 const testForceKill = async (t, forceKillAfterDelay, killSignal, expectedDelay, expectedKillSignal, options) => {87 const {subprocess} = await spawnNoKillable(forceKillAfterDelay, options);8880 subprocess.kill(killArgument);89 subprocess.kill(killSignal);9082 const {isTerminated, signal} = await t.throwsAsync(subprocess);91 const {isTerminated, signal, isForcefullyTerminated, shortMessage} = await t.throwsAsync(subprocess);92 t.true(isTerminated);93 t.is(signal, 'SIGKILL');94 t.true(isForcefullyTerminated);95 const messageSuffix = killSignal instanceof Error ? `\n${killSignal.message}` : '';96 const signalDescription = expectedKillSignal === 'SIGINT' ? 'User interruption with CTRL-C' : 'Termination';97 t.is(shortMessage, `Command was killed with ${expectedKillSignal} (${signalDescription}) and was forcefully terminated after ${expectedDelay} milliseconds: no-killable.js${messageSuffix}`);98 };9987 test('`forceKillAfterDelay: number` should kill after a timeout', testForceKill, 50);88 test('`forceKillAfterDelay: true` should kill after a timeout', testForceKill, true);89 test('`forceKillAfterDelay: undefined` should kill after a timeout', testForceKill, undefined);90 test('`forceKillAfterDelay` should kill after a timeout with SIGTERM', testForceKill, 50, 'SIGTERM');91 test('`forceKillAfterDelay` should kill after a timeout with the killSignal string', testForceKill, 50, 'SIGINT', {killSignal: 'SIGINT'});92 test('`forceKillAfterDelay` should kill after a timeout with the killSignal string, mixed', testForceKill, 50, 'SIGINT', {killSignal: constants.signals.SIGINT});93 test('`forceKillAfterDelay` should kill after a timeout with the killSignal number', testForceKill, 50, constants.signals.SIGINT, {killSignal: constants.signals.SIGINT});94 test('`forceKillAfterDelay` should kill after a timeout with the killSignal number, mixed', testForceKill, 50, constants.signals.SIGINT, {killSignal: 'SIGINT'});95 test('`forceKillAfterDelay` should kill after a timeout with an error', testForceKill, 50, new Error('test'));96 test('`forceKillAfterDelay` should kill after a timeout with an error and a killSignal', testForceKill, 50, new Error('test'), {killSignal: 'SIGINT'});100 test('`forceKillAfterDelay: number` should kill after a timeout', testForceKill, 50, undefined, 50, 'SIGTERM');101 test('`forceKillAfterDelay: true` should kill after a timeout', testForceKill, true, undefined, 5e3, 'SIGTERM');102 test('`forceKillAfterDelay: undefined` should kill after a timeout', testForceKill, undefined, undefined, 5e3, 'SIGTERM');103 test('`forceKillAfterDelay` should kill after a timeout with SIGTERM', testForceKill, 50, 'SIGTERM', 50, 'SIGTERM');104 test('`forceKillAfterDelay` should kill after a timeout with the killSignal string', testForceKill, 50, 'SIGINT', 50, 'SIGINT', {killSignal: 'SIGINT'});105 test('`forceKillAfterDelay` should kill after a timeout with the killSignal string, mixed', testForceKill, 50, 'SIGINT', 50, 'SIGINT', {killSignal: constants.signals.SIGINT});106 test('`forceKillAfterDelay` should kill after a timeout with the killSignal number', testForceKill, 50, constants.signals.SIGINT, 50, 'SIGINT', {killSignal: constants.signals.SIGINT});107 test('`forceKillAfterDelay` should kill after a timeout with the killSignal number, mixed', testForceKill, 50, constants.signals.SIGINT, 50, 'SIGINT', {killSignal: 'SIGINT'});108 test('`forceKillAfterDelay` should kill after a timeout with an error', testForceKill, 50, new Error('test'), 50, 'SIGTERM');109 test('`forceKillAfterDelay` should kill after a timeout with an error and a killSignal', testForceKill, 50, new Error('test'), 50, 'SIGINT', {killSignal: 'SIGINT'});11098 test('`forceKillAfterDelay` works with the "signal" option', async t => {111 test('`forceKillAfterDelay` works with the "cancelSignal" option', async t => {112 const abortController = new AbortController();113 const subprocess = spawnNoKillableSimple({cancelSignal: abortController.signal});114 await once(subprocess, 'spawn');102 abortController.abort();103 const {isTerminated, signal, isCanceled} = await t.throwsAsync(subprocess);115 abortController.abort('');116 const {isTerminated, signal, isCanceled, isForcefullyTerminated, shortMessage} = await t.throwsAsync(subprocess);117 t.true(isTerminated);118 t.is(signal, 'SIGKILL');119 t.true(isCanceled);120 t.true(isForcefullyTerminated);121 t.is(shortMessage, 'Command was canceled and was forcefully terminated after 1 milliseconds: forever.js');122 });123124 test('`forceKillAfterDelay` works with the "timeout" option', async t => {110 const subprocess = spawnNoKillableSimple({timeout: 1});111 const {isTerminated, signal, timedOut} = await t.throwsAsync(subprocess);125 const {isTerminated, signal, timedOut, isForcefullyTerminated, shortMessage} = await t.throwsAsync(spawnNoKillableSimple({timeout: 1}));126 t.true(isTerminated);127 t.is(signal, 'SIGKILL');128 t.true(timedOut);129 t.true(isForcefullyTerminated);130 t.is(shortMessage, 'Command timed out after 1 milliseconds and was forcefully terminated after 1 milliseconds: forever.js');131 });132133 test('`forceKillAfterDelay` works with the "maxBuffer" option', async t => {134 const subprocess = execa('noop-forever.js', ['.'], {...noKillableSimpleOptions, maxBuffer: 1});135 const [chunk] = await once(subprocess.stdout, 'data');136 t.is(chunk.toString(), '.\n');137 subprocess.kill();138 const {isTerminated, signal, isForcefullyTerminated, shortMessage} = await t.throwsAsync(subprocess);139 t.true(isTerminated);140 t.is(signal, 'SIGKILL');141 t.true(isForcefullyTerminated);142 t.is(shortMessage, 'Command\'s stdout was larger than 1 characters and was forcefully terminated after 1 milliseconds: noop-forever.js .\nmaxBuffer exceeded');143 });144145 test('`forceKillAfterDelay` works with the "error" event', async t => {146 const subprocess = spawnNoKillableSimple();147 await once(subprocess, 'spawn');148 const error = new Error(foobarString);149 error.code = 'ECODE';150 subprocess.emit('error', error);151 subprocess.kill();152 const {isTerminated, signal, isForcefullyTerminated, shortMessage, originalMessage, cause} = await t.throwsAsync(subprocess);153 t.true(isTerminated);154 t.is(signal, 'SIGKILL');155 t.true(isForcefullyTerminated);156 t.is(cause, error);157 t.is(originalMessage, error.message);158 t.is(shortMessage, `Command failed with ${error.code} and was forcefully terminated after 1 milliseconds: forever.js\n${error.message}`);159 });160161 test.serial('Can call `.kill()` with `forceKillAfterDelay` many times without triggering the maxListeners warning', async t => {162 const checkMaxListeners = assertMaxListeners(t);163164 const subprocess = spawnNoKillableSimple();165 for (let index = 0; index < defaultMaxListeners + 1; index += 1) {166 subprocess.kill();167 }168125 const {isTerminated, signal} = await t.throwsAsync(subprocess);169 const {isTerminated, signal, isForcefullyTerminated} = await t.throwsAsync(subprocess);170 t.true(isTerminated);171 t.is(signal, 'SIGKILL');172 t.true(isForcefullyTerminated);173174 checkMaxListeners();175 });176
1 unmodified line
178 const subprocess = spawnNoKillableSimple();179 subprocess.kill();180 subprocess.kill();181137 const {isTerminated, signal} = await t.throwsAsync(subprocess);182 const {isTerminated, signal, isForcefullyTerminated} = await t.throwsAsync(subprocess);183 t.true(isTerminated);184 t.is(signal, 'SIGKILL');185 t.true(isForcefullyTerminated);186 });187}188189test('result.isForcefullyTerminated is false on success', async t => {190 const {isForcefullyTerminated} = await execa('empty.js');191 t.false(isForcefullyTerminated);192});193194test('error.isForcefullyTerminated is false on spawn errors', async t => {195 const {isForcefullyTerminated} = await t.throwsAsync(getEarlyErrorSubprocess());196 t.false(isForcefullyTerminated);197});198199test('error.isForcefullyTerminated is false when already terminated', async t => {200 const abortController = new AbortController();201 const final = async function * () {202 try {203 await setTimeout(1e6, undefined, {signal: abortController.signal});204 } catch {}205206 yield * [];207 };208209 const subprocess = execa('forever.js', {stdout: {final}});210 subprocess.kill();211 await setTimeout(6e3);212 abortController.abort();213 const {isForcefullyTerminated, isTerminated, signal} = await t.throwsAsync(subprocess);214 t.false(isForcefullyTerminated);215 t.true(isTerminated);216 t.is(signal, 'SIGTERM');217});
modified:test-d/return/result-main.test-d.ts+4 added
27 unmodified lines
28expectType<boolean>(unicornsResult.timedOut);29expectType<boolean>(unicornsResult.isCanceled);30expectType<boolean>(unicornsResult.isTerminated);31expectType<boolean>(unicornsResult.isMaxBuffer);32expectType<boolean>(unicornsResult.isForcefullyTerminated);33expectType<keyof SignalConstants | undefined>(unicornsResult.signal);34expectType<string | undefined>(unicornsResult.signalDescription);35expectType<string>(unicornsResult.cwd);36expectType<number>(unicornsResult.durationMs);
8 unmodified lines
45expectType<boolean>(unicornsResultSync.timedOut);46expectType<boolean>(unicornsResultSync.isCanceled);47expectType<boolean>(unicornsResultSync.isTerminated);48expectType<boolean>(unicornsResultSync.isMaxBuffer);49expectType<boolean>(unicornsResultSync.isForcefullyTerminated);50expectType<keyof SignalConstants | undefined>(unicornsResultSync.signal);51expectType<string | undefined>(unicornsResultSync.signalDescription);52expectType<string>(unicornsResultSync.cwd);53expectType<number>(unicornsResultSync.durationMs);
9 unmodified lines
63 expectType<boolean>(error.timedOut);64 expectType<boolean>(error.isCanceled);65 expectType<boolean>(error.isTerminated);66 expectType<boolean>(error.isMaxBuffer);67 expectType<boolean>(error.isForcefullyTerminated);68 expectType<keyof SignalConstants | undefined>(error.signal);69 expectType<string | undefined>(error.signalDescription);70 expectType<string>(error.cwd);71 expectType<number>(error.durationMs);
14 unmodified lines
86 expectType<boolean>(errorSync.timedOut);87 expectType<boolean>(errorSync.isCanceled);88 expectType<boolean>(errorSync.isTerminated);89 expectType<boolean>(errorSync.isMaxBuffer);90 expectType<boolean>(errorSync.isForcefullyTerminated);91 expectType<keyof SignalConstants | undefined>(errorSync.signal);92 expectType<string | undefined>(errorSync.signalDescription);93 expectType<string>(errorSync.cwd);94 expectType<number>(errorSync.durationMs);
6 unmodified lines
modified:types/arguments/options.d.ts+2 added
277 unmodified lines
278279 /**280 If the subprocess is terminated but does not exit, forcefully exit it by sending [`SIGKILL`](https://en.wikipedia.org/wiki/Signal_(IPC)#SIGKILL).281282 When this happens, `error.isForcefullyTerminated` becomes `true`.283284 @default 5000285 */286 readonly forceKillAfterDelay?: Unless<IsSync, number | false>;287
97 unmodified lines
modified:types/return/result.d.ts+5 added
111 unmodified lines
112 */113 isTerminated: boolean;114115 /**116 Whether the subprocess was terminated by the `SIGKILL` signal sent by the `forceKillAfterDelay` option.117 */118 isForcefullyTerminated: boolean;119120 /**121 The numeric [exit code](https://en.wikipedia.org/wiki/Exit_status) of the subprocess that was run.122123 This is `undefined` when the subprocess could not be spawned or was terminated by a signal.124 */
74 unmodified lines