Skip to content
Myr

Improve `killSignal` option (#728)

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

authored by ehmicky · committed by GitHub · parent a61a028 · Browse files · 9 files+99 added−49 removed
This commit

State

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

Diff stats

9 files+99 added−49 removed

  • tests:+70 added−26 removed
  • other:+29 added−23 removed

9 files+99 added−49 removed

modified:lib/kill.js+15 added-17 removed
modified:lib/stream.js+2 added-2 removed
129 unmodified lines
130131// Retrieve result of child process: exit code, signal, error, streams (stdout/stderr/all)132export const getSpawnedResult = async ({133 spawned,134 options: {encoding, buffer, maxBuffer, timeoutDuration: timeout, killSignal, cleanup, detached},134 options: {encoding, buffer, maxBuffer, timeoutDuration: timeout, cleanup, detached},135 context,136 stdioStreamsGroups,137 controller,138}) => {
19 unmodified lines
158 ]),159 throwOnProcessError(processErrorPromise),160 ...throwOnCustomStreamsError(customStreams),161 ...throwIfStreamError(spawned.stdin),162 ...throwOnTimeout({spawned, timeout, killSignal, context, controller}),162 ...throwOnTimeout(timeout, context, controller),163 ]);164 } catch (error) {165 spawned.kill();166 const results = await Promise.all([
13 unmodified lines
added:test/fixtures/noop-forever.js+5 added
modified:test/error.js+7 added-7 removed
156 unmodified lines
157 t.true(failed);158});159160test('error.isTerminated is true if process was killed directly', async t => {161 const subprocess = execa('forever.js');161 const subprocess = execa('forever.js', {killSignal: 'SIGINT'});162163 subprocess.kill();164165 const {isTerminated, signal} = await t.throwsAsync(subprocess, {message: /was killed with SIGTERM/});165 const {isTerminated, signal} = await t.throwsAsync(subprocess, {message: /was killed with SIGINT/});166 t.true(isTerminated);167 t.is(signal, 'SIGTERM');167 t.is(signal, 'SIGINT');168});169170test('error.isTerminated is true if process was killed indirectly', async t => {171 const subprocess = execa('forever.js');171 const subprocess = execa('forever.js', {killSignal: 'SIGHUP'});172173 process.kill(subprocess.pid, 'SIGINT');174175 // `process.kill()` is emulated by Node.js on Windows
65 unmodified lines
241 const {signal} = await t.throwsAsync(subprocess, {message: /was killed with SIGTERM/});242 t.is(signal, 'SIGTERM');243 });244245 test('custom error.signal', async t => {246 const {signal} = await t.throwsAsync(execa('forever.js', {killSignal: 'SIGHUP', timeout: 1, message: TIMEOUT_REGEXP}));247 t.is(signal, 'SIGHUP');245 test('error.signal uses killSignal', async t => {246 const {signal} = await t.throwsAsync(execa('forever.js', {killSignal: 'SIGINT', timeout: 1, message: TIMEOUT_REGEXP}));247 t.is(signal, 'SIGINT');248 });249250 test('exitCode is undefined on signal termination', async t => {251 const subprocess = execa('forever.js');
43 unmodified lines
modified:test/kill.js+41 added-13 removed
1import process from 'node:process';2import {once} from 'node:events';3import {constants} from 'node:os';4import {setTimeout} from 'node:timers/promises';5import test from 'ava';6import {pEvent} from 'p-event';7import isRunning from 'is-running';
26 unmodified lines
3435// `SIGTERM` cannot be caught on Windows, and it always aborts the process (like `SIGKILL` on Unix).36// Therefore, this feature and those tests do not make sense on Windows.37if (process.platform !== 'win32') {37 const testNoForceKill = async (t, forceKillAfterDelay, killArgument) => {38 const {subprocess} = await spawnNoKillable(forceKillAfterDelay);38 const testNoForceKill = async (t, forceKillAfterDelay, killArgument, options) => {39 const {subprocess} = await spawnNoKillable(forceKillAfterDelay, options);4041 subprocess.kill(killArgument);4243 await setTimeout(6e3);
6 unmodified lines
50 };5152 test('`forceKillAfterDelay: false` should not kill after a timeout', testNoForceKill, false);53 test('`forceKillAfterDelay` should not kill after a timeout with other signals', testNoForceKill, true, 'SIGINT');54 test('`forceKillAfterDelay` should not kill after a timeout with wrong killSignal string', testNoForceKill, true, 'SIGTERM', {killSignal: 'SIGINT'});55 test('`forceKillAfterDelay` should not kill after a timeout with wrong killSignal number', testNoForceKill, true, constants.signals.SIGTERM, {killSignal: constants.signals.SIGINT});5654 const testForceKill = async (t, forceKillAfterDelay, killArgument) => {55 const {subprocess} = await spawnNoKillable(forceKillAfterDelay);57 const testForceKill = async (t, forceKillAfterDelay, killArgument, options) => {58 const {subprocess} = await spawnNoKillable(forceKillAfterDelay, options);5960 subprocess.kill(killArgument);6162 const {isTerminated, signal} = await t.throwsAsync(subprocess);
3 unmodified lines
6667 test('`forceKillAfterDelay: number` should kill after a timeout', testForceKill, 50);68 test('`forceKillAfterDelay: true` should kill after a timeout', testForceKill, true);69 test('`forceKillAfterDelay: undefined` should kill after a timeout', testForceKill, undefined);67 test('`forceKillAfterDelay` should kill after a timeout with the killSignal', testForceKill, 50, 'SIGTERM');70 test('`forceKillAfterDelay` should kill after a timeout with SIGTERM', testForceKill, 50, 'SIGTERM');71 test('`forceKillAfterDelay` should kill after a timeout with the killSignal string', testForceKill, 50, 'SIGINT', {killSignal: 'SIGINT'});72 test('`forceKillAfterDelay` should kill after a timeout with the killSignal number', testForceKill, 50, constants.signals.SIGINT, {killSignal: constants.signals.SIGINT});7374 const testInvalidForceKill = async (t, forceKillAfterDelay) => {75 t.throws(() => {76 execa('empty.js', {forceKillAfterDelay});
4 unmodified lines
81 test('`forceKillAfterDelay` should not be negative', testInvalidForceKill, -1);8283 test('`forceKillAfterDelay` works with the "signal" option', async t => {84 const abortController = new AbortController();80 const {subprocess} = await spawnNoKillable(1, {signal: abortController.signal});85 const subprocess = execa('forever.js', {killSignal: 'SIGWINCH', forceKillAfterDelay: 1, signal: abortController.signal});86 await once(subprocess, 'spawn');87 abortController.abort();88 const {isTerminated, signal, isCanceled} = await t.throwsAsync(subprocess);89 t.true(isTerminated);90 t.is(signal, 'SIGKILL');91 t.true(isCanceled);92 });9388 // For this test to work, the process needs to spawn and setup its `SIGTERM` handler before `timeout` terminates it.89 // This creates a race condition that we can only work around by using `test.serial()` and a higher timeout.90 test.serial('`forceKillAfterDelay` works with the "timeout" option', async t => {91 const {subprocess} = await spawnNoKillable(1, {timeout: 5e3});94 test('`forceKillAfterDelay` works with the "timeout" option', async t => {95 const subprocess = execa('forever.js', {killSignal: 'SIGWINCH', forceKillAfterDelay: 1, timeout: 1});96 const {isTerminated, signal, timedOut} = await t.throwsAsync(subprocess);97 t.true(isTerminated);98 t.is(signal, 'SIGKILL');99 t.true(timedOut);100 });101102 test('`forceKillAfterDelay` works with the "maxBuffer" option', async t => {99 const {subprocess} = await spawnNoKillable(1, {maxBuffer: 1});103 const subprocess = execa('noop-forever.js', ['.'], {killSignal: 'SIGWINCH', forceKillAfterDelay: 1, maxBuffer: 1});104 const {isTerminated, signal} = await t.throwsAsync(subprocess);105 t.true(isTerminated);106 t.is(signal, 'SIGKILL');107 });108109 test('`forceKillAfterDelay` works with "error" events on childProcess', async t => {106 const {subprocess} = await spawnNoKillable(1);110 const subprocess = execa('forever.js', {killSignal: 'SIGWINCH', forceKillAfterDelay: 1});111 await once(subprocess, 'spawn');112 subprocess.emit('error', new Error('test'));113 const {isTerminated, signal} = await t.throwsAsync(subprocess);114 t.true(isTerminated);115 t.is(signal, 'SIGKILL');116 });117118 test('`forceKillAfterDelay` works with "error" events on childProcess.stdout', async t => {114 const {subprocess} = await spawnNoKillable(1);119 const subprocess = execa('forever.js', {killSignal: 'SIGWINCH', forceKillAfterDelay: 1});120 await once(subprocess, 'spawn');121 subprocess.stdout.destroy(new Error('test'));122 const {isTerminated, signal} = await t.throwsAsync(subprocess);123 t.true(isTerminated);124 t.is(signal, 'SIGKILL');
26 unmodified lines
151 const {timedOut} = await execa('delay.js', ['500'], {timeout: 1e8});152 t.false(timedOut);153});154155test('timeout uses killSignal', async t => {156 const {isTerminated, signal, timedOut} = await t.throwsAsync(execa('forever.js', {timeout: 1, killSignal: 'SIGINT'}));157 t.true(isTerminated);158 t.is(signal, 'SIGINT');159 t.true(timedOut);160});161162const INVALID_TIMEOUT_REGEXP = /`timeout` option to be a non-negative integer/;163164const testTimeoutValidation = (t, timeout, execaMethod) => {165 t.throws(() => {
161 unmodified lines
327 const result = await subprocess;328 abortController.abort();329 t.false(result.isCanceled);330});331332test('child process errors are handled', async t => {333 const subprocess = execa('forever.js');334 subprocess.emit('error', new Error('test'));335 await t.throwsAsync(subprocess, {message: 'Command failed: forever.js\ntest'});336});337338test('child process errors use killSignal', async t => {339 const subprocess = execa('forever.js', {killSignal: 'SIGINT'});340 await once(subprocess, 'spawn');341 subprocess.emit('error', new Error('test'));342 const {isTerminated, signal} = await t.throwsAsync(subprocess, {message: /test/});343 t.true(isTerminated);344 t.is(signal, 'SIGINT');345});
modified:test/stream.js+17 added
133 unmodified lines
134test('maxBuffer does not affect stderr if too high', testMaxBufferSuccess, 2, false);135test('maxBuffer does not affect stdio[*] if too high', testMaxBufferSuccess, 3, false);136test('maxBuffer does not affect all if too high', testMaxBufferSuccess, 1, true);137138test('maxBuffer uses killSignal', async t => {139 const {isTerminated, signal} = await t.throwsAsync(140 execa('noop-forever.js', ['.'.repeat(maxBuffer + 1)], {maxBuffer, killSignal: 'SIGINT'}),141 {message: /maxBuffer exceeded/},142 );143 t.true(isTerminated);144 t.is(signal, 'SIGINT');145});146147const testMaxBufferLimit = async (t, index, all) => {148 const length = all ? maxBuffer * 2 : maxBuffer;149 const result = await t.throwsAsync(150 execa('max-buffer.js', [`${index}`, `${length + 1}`], {...fullStdio, maxBuffer, all}),
155 unmodified lines
306test('Errors on stdout should make the process exit', testStreamError, 1);307test('Errors on stderr should make the process exit', testStreamError, 2);308test('Errors on stdio[*] should make the process exit', testStreamError, 3);309310test('Errors on streams use killSignal', async t => {311 const childProcess = execa('forever.js', {killSignal: 'SIGINT'});312 childProcess.stdout.destroy(new Error('test'));313 const {isTerminated, signal} = await t.throwsAsync(childProcess, {message: /test/});314 t.true(isTerminated);315 t.is(signal, 'SIGINT');316});317318const testWaitOnStreamEnd = async (t, index) => {319 const childProcess = execa('stdin-fd.js', [`${index}`], fullStdio);320 await setTimeout(100);321 childProcess.stdio[index].end('foobar');
46 unmodified lines
modified:test/test.js-6 removed
145 unmodified lines
146147test.serial('execPath option', testExecPath, identity);148test.serial('execPath option can be a file URL', testExecPath, pathToFileURL);149150test('child process errors are handled', async t => {151 const subprocess = execa('noop.js');152 subprocess.emit('error', new Error('test'));153 await t.throwsAsync(subprocess, {message: /test/});154});155150test('execa() returns a promise with pid', async t => {151 const subprocess = execa('noop.js', ['foo']);152 t.is(typeof subprocess.pid, 'number');153 await subprocess;
157 unmodified lines
modified:index.d.ts+6 added-2 removed
446 unmodified lines
447 */448 readonly encoding?: EncodingOption;449450 /**451 If `timeout` is greater than `0`, the parent will send the signal identified by the `killSignal` property (the default is `SIGTERM`) if the child runs longer than `timeout` milliseconds.451 If `timeout`` is greater than `0`, the child process will be [terminated](#killsignal) if it runs for longer than that amount of milliseconds.452453 @default 0454 */455 readonly timeout?: number;
5 unmodified lines
461 */462 readonly maxBuffer?: number;463464 /**465 Signal value to be used when the spawned process will be killed.465 Signal used to terminate the child process when:466 - using the `signal`, `timeout`, `maxBuffer` or `cleanup` option467 - calling [`subprocess.kill()`](https://nodejs.org/api/child_process.html#subprocesskillsignal) with no arguments468469 This can be either a name (like `"SIGTERM"`) or a number (like `9`).470471 @default 'SIGTERM'472 */473 readonly killSignal?: string | number;474
815 unmodified lines
modified:readme.md+6 added-2 removed
736 unmodified lines
737738Type: `number`\739Default: `0`740741If timeout is greater than `0`, the parent will send the signal identified by the `killSignal` property (the default is `SIGTERM`) if the child runs longer than timeout milliseconds.741If `timeout`` is greater than `0`, the child process will be [terminated](#killsignal) if it runs for longer than that amount of milliseconds.742743#### maxBuffer744745Type: `number`\
5 unmodified lines
751752Type: `string | number`\753Default: `SIGTERM`754755Signal value to be used when the spawned process will be killed.755Signal used to terminate the child process when:756- using the [`signal`](#signal-1), [`timeout`](#timeout), [`maxBuffer`](#maxbuffer) or [`cleanup`](#cleanup) option757- calling [`subprocess.kill()`](https://nodejs.org/api/child_process.html#subprocesskillsignal) with no arguments758759This can be either a name (like `"SIGTERM"`) or a number (like `9`).760761#### forceKillAfterDelay762763Type: `number | false`\764Default: `5000`
180 unmodified lines