Skip to content
Myr

Fix calling `.kill()` many times (#768)

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

authored by ehmicky · committed by GitHub · parent efb4275 · Browse files · 2 files+73 added−17 removed
This commit

State

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

Diff stats

2 files+73 added−17 removed

  • tests:+71 added−17 removed
  • other:+2 added−0 removed

2 files+73 added−17 removed

modified:test/kill.js+71 added-17 removed
1import process from 'node:process';2import {once} from 'node:events';2import {once, defaultMaxListeners} from 'node:events';3import {constants} from 'node:os';4import {setTimeout} from 'node:timers/promises';5import test from 'ava';6import {pEvent} from 'p-event';
14 unmodified lines
21 await pEvent(subprocess, 'message');22 return {subprocess};23};2425const spawnNoKillableSimple = options => execa('forever.js', {killSignal: 'SIGWINCH', forceKillAfterDelay: 1, ...options});2627const spawnNoKillableOutput = options => execa('noop-forever.js', ['.'], {killSignal: 'SIGWINCH', forceKillAfterDelay: 1, ...options});2829test('kill("SIGKILL") should terminate cleanly', async t => {30 const {subprocess} = await spawnNoKillable();3132 subprocess.kill('SIGKILL');
2 unmodified lines
35 t.true(isTerminated);36 t.is(signal, 'SIGKILL');37});3839const testInvalidForceKill = async (t, forceKillAfterDelay) => {40 t.throws(() => {41 execa('empty.js', {forceKillAfterDelay});42 }, {instanceOf: TypeError, message: /non-negative integer/});43};4445test('`forceKillAfterDelay` should not be NaN', testInvalidForceKill, Number.NaN);46test('`forceKillAfterDelay` should not be negative', testInvalidForceKill, -1);4748// `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') {49// Therefore, this feature and those tests must be different on Windows.50if (process.platform === 'win32') {51 test('Can call `.kill()` with `forceKillAfterDelay` on Windows', async t => {52 const {subprocess} = await spawnNoKillable(1);53 subprocess.kill();5455 const {isTerminated, signal} = await t.throwsAsync(subprocess);56 t.true(isTerminated);57 t.is(signal, 'SIGTERM');58 });59} else {60 const testNoForceKill = async (t, forceKillAfterDelay, killArgument, options) => {61 const {subprocess} = await spawnNoKillable(forceKillAfterDelay, options);6263 subprocess.kill(killArgument);
28 unmodified lines
92 test('`forceKillAfterDelay` should kill after a timeout with SIGTERM', testForceKill, 50, 'SIGTERM');93 test('`forceKillAfterDelay` should kill after a timeout with the killSignal string', testForceKill, 50, 'SIGINT', {killSignal: 'SIGINT'});94 test('`forceKillAfterDelay` should kill after a timeout with the killSignal number', testForceKill, 50, constants.signals.SIGINT, {killSignal: constants.signals.SIGINT});9574 const testInvalidForceKill = async (t, forceKillAfterDelay) => {75 t.throws(() => {76 execa('empty.js', {forceKillAfterDelay});77 }, {instanceOf: TypeError, message: /non-negative integer/});78 };7980 test('`forceKillAfterDelay` should not be NaN', testInvalidForceKill, Number.NaN);81 test('`forceKillAfterDelay` should not be negative', testInvalidForceKill, -1);8296 test('`forceKillAfterDelay` works with the "signal" option', async t => {97 const abortController = new AbortController();85 const subprocess = execa('forever.js', {killSignal: 'SIGWINCH', forceKillAfterDelay: 1, signal: abortController.signal});98 const subprocess = spawnNoKillableSimple({signal: abortController.signal});99 await once(subprocess, 'spawn');100 abortController.abort();101 const {isTerminated, signal, isCanceled} = await t.throwsAsync(subprocess);102 t.true(isTerminated);
1 unmodified line
104 t.true(isCanceled);105 });106107 test('`forceKillAfterDelay` works with the "timeout" option', async t => {95 const subprocess = execa('forever.js', {killSignal: 'SIGWINCH', forceKillAfterDelay: 1, timeout: 1});108 const subprocess = spawnNoKillableSimple({timeout: 1});109 const {isTerminated, signal, timedOut} = await t.throwsAsync(subprocess);110 t.true(isTerminated);111 t.is(signal, 'SIGKILL');112 t.true(timedOut);113 });114115 test('`forceKillAfterDelay` works with the "maxBuffer" option', async t => {103 const subprocess = execa('noop-forever.js', ['.'], {killSignal: 'SIGWINCH', forceKillAfterDelay: 1, maxBuffer: 1});116 const subprocess = spawnNoKillableOutput({maxBuffer: 1});117 const {isTerminated, signal} = await t.throwsAsync(subprocess);118 t.true(isTerminated);119 t.is(signal, 'SIGKILL');120 });121122 test('`forceKillAfterDelay` works with "error" events on childProcess', async t => {110 const subprocess = execa('forever.js', {killSignal: 'SIGWINCH', forceKillAfterDelay: 1});123 const subprocess = spawnNoKillableSimple();124 await once(subprocess, 'spawn');125 subprocess.emit('error', new Error('test'));126 const {isTerminated, signal} = await t.throwsAsync(subprocess);127 t.true(isTerminated);128 t.is(signal, 'SIGKILL');129 });130131 test('`forceKillAfterDelay` works with "error" events on childProcess.stdout', async t => {119 const subprocess = execa('forever.js', {killSignal: 'SIGWINCH', forceKillAfterDelay: 1});132 const subprocess = spawnNoKillableSimple();133 await once(subprocess, 'spawn');134 subprocess.stdout.destroy(new Error('test'));135 const {isTerminated, signal} = await t.throwsAsync(subprocess);136 t.true(isTerminated);137 t.is(signal, 'SIGKILL');138 });139140 test.serial('Can call `.kill()` with `forceKillAfterDelay` many times without triggering the maxListeners warning', async t => {141 let warning;142 const captureWarning = warningArgument => {143 warning = warningArgument;144 };145146 process.once('warning', captureWarning);147148 const subprocess = spawnNoKillableSimple();149 for (let index = 0; index < defaultMaxListeners + 1; index += 1) {150 subprocess.kill();151 }152153 const {isTerminated, signal} = await t.throwsAsync(subprocess);154 t.true(isTerminated);155 t.is(signal, 'SIGKILL');156157 t.is(warning, undefined);158 process.off('warning', captureWarning);159 });160161 test('Can call `.kill()` with `forceKillAfterDelay` multiple times', async t => {162 const subprocess = spawnNoKillableSimple();163 subprocess.kill();164 subprocess.kill();165166 const {isTerminated, signal} = await t.throwsAsync(subprocess);167 t.true(isTerminated);168 t.is(signal, 'SIGKILL');169 });170}171172test('Can call `.kill()` multiple times', async t => {173 const subprocess = execa('forever.js');174 subprocess.kill();175 subprocess.kill();176177 const {isTerminated, signal} = await t.throwsAsync(subprocess);178 t.true(isTerminated);179 t.is(signal, 'SIGTERM');180});181182test('execa() returns a promise with kill()', async t => {183 const subprocess = execa('noop.js', ['foo']);184 t.is(typeof subprocess.kill, 'function');185 await subprocess;
214 unmodified lines
modified:index.js+2 added
1import {Buffer} from 'node:buffer';2import {setMaxListeners} from 'node:events';3import path from 'node:path';4import childProcess from 'node:child_process';5import process from 'node:process';6import {fileURLToPath} from 'node:url';
144 unmodified lines
151 return dummySpawned;152 }153154 const controller = new AbortController();155 setMaxListeners(Number.POSITIVE_INFINITY, controller.signal);156157 pipeOutputAsync(spawned, stdioStreamsGroups);158159 spawned.kill = spawnedKill.bind(undefined, spawned.kill.bind(spawned), options, controller);
199 unmodified lines