Cleanup child process listeners (#771)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 8db0b09 · Browse files · 3 files·+31 added−13 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+31 added−13 removed
- tests:+10 added−0 removed
- other:+21 added−13 removed
Provenance
- Commit
6943e81- Tree
65faaad- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
8db0b09
3 files changed+31 added−13 removed
modified:lib/kill.js+6 added-2 removed
1234559 unmodified lines656667686869707172737374757677787980import {addAbortListener} from 'node:events';import os from 'node:os';import {setTimeout} from 'node:timers/promises';import {onExit} from 'signal-exit';
59 unmodified lines }};
// `cleanup` option handlingexport const cleanupOnExit = (spawned, cleanup, detached) => {export const cleanupOnExit = (spawned, cleanup, detached, {signal}) => { if (!cleanup || detached) { return; }
return onExit(() => { const removeExitHandler = onExit(() => { spawned.kill(); }); addAbortListener(signal, () => { removeExitHandler(); });};modified:lib/stream.js+15 added-11 removed
1-import {once} from 'node:events';1+import {once, addAbortListener} from 'node:events';2 import {finished} from 'node:stream/promises';3 import {setImmediate} from 'node:timers/promises';4 import getStream, {getStreamAsArrayBuffer, getStreamAsArray} from 'get-stream';5 import mergeStreams from '@sindresorhus/merge-streams';103 unmodified lines109 throw error;110 };111 112 // Like `once()` except it never rejects, especially not on `error` event.113-const pEvent = (eventEmitter, eventName) => new Promise(resolve => {114- eventEmitter.once(eventName, (...payload) => {113+const pEvent = (eventEmitter, eventName, {signal}) => new Promise(resolve => {114+ const listener = (...payload) => {115 resolve([eventName, ...payload]);116+ };117+118+ eventEmitter.once(eventName, listener);119+ addAbortListener(signal, () => {120+ eventEmitter.removeListener(eventName, listener);121 });122 });123 124 const throwOnProcessError = async processErrorPromise => {19 unmodified lines144 context,145 stdioStreamsGroups,146 controller,147 }) => {143- const processSpawnPromise = pEvent(spawned, 'spawn');144- const processErrorPromise = pEvent(spawned, 'error');145- const processExitPromise = pEvent(spawned, 'exit');148+ cleanupOnExit(spawned, cleanup, detached, controller);149 147- const removeExitHandler = cleanupOnExit(spawned, cleanup, detached);150+ const processSpawnPromise = pEvent(spawned, 'spawn', controller);151+ const processErrorPromise = pEvent(spawned, 'error', controller);152+ const processExitPromise = pEvent(spawned, 'exit', controller);153+154 const customStreams = getCustomStreams(stdioStreamsGroups);155 156 const stdioPromises = spawned.stdio.map((stream, index) => getStdioPromise({stream, stdioStreams: stdioStreamsGroups[index], encoding, buffer, maxBuffer}));157 const allPromise = getAllPromise({spawned, encoding, buffer, maxBuffer});14 unmodified lines172 ...throwOnTimeout(timeout, context, controller),173 ]);174 } catch (error) {175 spawned.kill();170- const results = await Promise.all([176+ return await Promise.all([177 error,178 waitForFailedProcess(processSpawnPromise, processErrorPromise, processExitPromise),179 Promise.all(stdioPromises.map(stdioPromise => getBufferedData(stdioPromise, encoding))),180 getBufferedData(allPromise, encoding),181+ Promise.allSettled(customStreamsEndPromises),182 ]);176- await Promise.allSettled(customStreamsEndPromises);177- return results;183 } finally {184 controller.abort();180- removeExitHandler?.();185 }186 };modified:test/stream.js+10 added
392 unmodified lines393 394 test('childProcess.stdout must be read right away', testBufferDestroyOnEnd, 1);395 test('childProcess.stderr must be read right away', testBufferDestroyOnEnd, 2);396 test('childProcess.stdio[*] must be read right away', testBufferDestroyOnEnd, 3);397+398+const testProcessEventsCleanup = async (t, fixtureName) => {399+ const childProcess = execa(fixtureName, {reject: false});400+ t.deepEqual(childProcess.eventNames().sort(), ['error', 'exit', 'spawn']);401+ await childProcess;402+ t.deepEqual(childProcess.eventNames(), []);403+};404+405+test('childProcess listeners are cleaned up on success', testProcessEventsCleanup, 'empty.js');406+test('childProcess listeners are cleaned up on failure', testProcessEventsCleanup, 'fail.js');