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
Provenance
- Commit
db95b51- Tree
09081ec- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
efb4275
2 files changed+73 added−17 removed
modified:test/kill.js+71 added-17 removed
1 import process from 'node:process';2-import {once} from 'node:events';2+import {once, defaultMaxListeners} from 'node:events';3 import {constants} from 'node:os';4 import {setTimeout} from 'node:timers/promises';5 import test from 'ava';6 import {pEvent} from 'p-event';14 unmodified lines21 await pEvent(subprocess, 'message');22 return {subprocess};23 };24 25+const spawnNoKillableSimple = options => execa('forever.js', {killSignal: 'SIGWINCH', forceKillAfterDelay: 1, ...options});26+27+const spawnNoKillableOutput = options => execa('noop-forever.js', ['.'], {killSignal: 'SIGWINCH', forceKillAfterDelay: 1, ...options});28+29 test('kill("SIGKILL") should terminate cleanly', async t => {30 const {subprocess} = await spawnNoKillable();31 32 subprocess.kill('SIGKILL');2 unmodified lines35 t.true(isTerminated);36 t.is(signal, 'SIGKILL');37 });38 39+const testInvalidForceKill = async (t, forceKillAfterDelay) => {40+ t.throws(() => {41+ execa('empty.js', {forceKillAfterDelay});42+ }, {instanceOf: TypeError, message: /non-negative integer/});43+};44+45+test('`forceKillAfterDelay` should not be NaN', testInvalidForceKill, Number.NaN);46+test('`forceKillAfterDelay` should not be negative', testInvalidForceKill, -1);47+48 // `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.37-if (process.platform !== 'win32') {49+// Therefore, this feature and those tests must be different on Windows.50+if (process.platform === 'win32') {51+ test('Can call `.kill()` with `forceKillAfterDelay` on Windows', async t => {52+ const {subprocess} = await spawnNoKillable(1);53+ subprocess.kill();54+55+ 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);62 63 subprocess.kill(killArgument);28 unmodified lines92 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});95 74- const testInvalidForceKill = async (t, forceKillAfterDelay) => {75- t.throws(() => {76- execa('empty.js', {forceKillAfterDelay});77- }, {instanceOf: TypeError, message: /non-negative integer/});78- };79-80- test('`forceKillAfterDelay` should not be NaN', testInvalidForceKill, Number.NaN);81- test('`forceKillAfterDelay` should not be negative', testInvalidForceKill, -1);82-96 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 line104 t.true(isCanceled);105 });106 107 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 });114 115 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 });121 122 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 });130 131 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 });139+140+ 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+ };145+146+ process.once('warning', captureWarning);147+148+ const subprocess = spawnNoKillableSimple();149+ for (let index = 0; index < defaultMaxListeners + 1; index += 1) {150+ subprocess.kill();151+ }152+153+ const {isTerminated, signal} = await t.throwsAsync(subprocess);154+ t.true(isTerminated);155+ t.is(signal, 'SIGKILL');156+157+ t.is(warning, undefined);158+ process.off('warning', captureWarning);159+ });160+161+ test('Can call `.kill()` with `forceKillAfterDelay` multiple times', async t => {162+ const subprocess = spawnNoKillableSimple();163+ subprocess.kill();164+ subprocess.kill();165+166+ const {isTerminated, signal} = await t.throwsAsync(subprocess);167+ t.true(isTerminated);168+ t.is(signal, 'SIGKILL');169+ });170 }171 172+test('Can call `.kill()` multiple times', async t => {173+ const subprocess = execa('forever.js');174+ subprocess.kill();175+ subprocess.kill();176+177+ const {isTerminated, signal} = await t.throwsAsync(subprocess);178+ t.true(isTerminated);179+ t.is(signal, 'SIGTERM');180+});181+182 test('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 linesmodified:index.js+2 added
1 import {Buffer} from 'node:buffer';2+import {setMaxListeners} from 'node:events';3 import path from 'node:path';4 import childProcess from 'node:child_process';5 import process from 'node:process';6 import {fileURLToPath} from 'node:url';144 unmodified lines151 return dummySpawned;152 }153 154 const controller = new AbortController();155+ setMaxListeners(Number.POSITIVE_INFINITY, controller.signal);156 157 pipeOutputAsync(spawned, stdioStreamsGroups);158 159 spawned.kill = spawnedKill.bind(undefined, spawned.kill.bind(spawned), options, controller);199 unmodified lines