Skip to content
Myr

Fix Node.js streams not being cleaned up on process error (#660)

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

authored by ehmicky · committed by GitHub · parent 7122767 · Browse files · 4 files+22 added−10 removed
This commit

State

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

Diff stats

4 files+22 added−10 removed

  • tests:+12 added−0 removed
  • other:+10 added−10 removed

4 files+22 added−10 removed

modified:lib/stdio/async.js+5 added-5 removed
modified:lib/stdio/native.js+1 added-1 removed
modified:lib/stream.js+4 added-4 removed
1import {Buffer} from 'node:buffer';2import {once} from 'node:events';3import {finished} from 'node:stream/promises';4import process from 'node:process';4import getStream, {getStreamAsArrayBuffer} from 'get-stream';5import mergeStreams from '@sindresorhus/merge-streams';6import {throwOnTimeout, cleanupOnExit} from './kill.js';7import {STANDARD_STREAMS} from './stdio/native.js';89// `all` interleaves `stdout` and `stderr`10export const makeAllStream = (spawned, {all}) => {11 if (!all) {
67 unmodified lines
79const throwOnCustomStreamsError = customStreams => customStreams80 .filter(({value, type, direction}) => !shouldWaitForCustomStream(value, type, direction))81 .map(({value}) => throwOnStreamError(value));8283const shouldWaitForCustomStream = (value, type, direction) => type === 'filePath'84 || (direction === 'output' && value !== process.stdout && value !== process.stderr);83const shouldWaitForCustomStream = (value, type, direction) => (type === 'filePath' || direction === 'output')84 && !STANDARD_STREAMS.includes(value);8586const throwIfStreamError = stream => stream === null ? [] : [throwOnStreamError(stream)];8788const throwOnStreamError = async stream => {
5 unmodified lines
94// However `.pipe()` only does so when the source stream ended, not when it errored.95// Therefore, when `childProcess.stdin|stdout|stderr` errors, those streams must be manually destroyed.96const cleanupStdioStreams = (customStreams, error) => {97 for (const customStream of customStreams) {98 if (customStream.autoDestroy) {98 if (!STANDARD_STREAMS.includes(customStream.value)) {99 customStream.value.destroy(error);100 }101 }102};
45 unmodified lines
modified:test/stdio/array.js+12 added
253 unmodified lines
254test('stdin can be ["overlapped", "pipe"]', testOverlapped, getStdinOption);255test('stdout can be ["overlapped", "pipe"]', testOverlapped, getStdoutOption);256test('stderr can be ["overlapped", "pipe"]', testOverlapped, getStderrOption);257test('stdio[*] can be ["overlapped", "pipe"]', testOverlapped, getStdioOption);258259const testDestroyStandard = async (t, optionName) => {260 await t.throwsAsync(261 execa('forever.js', {timeout: 1, [optionName]: [process[optionName], 'pipe']}),262 {message: /timed out/},263 );264 t.false(process[optionName].destroyed);265};266267test('Does not destroy process.stdin on errors', testDestroyStandard, 'stdin');268test('Does not destroy process.stdout on errors', testDestroyStandard, 'stdout');269test('Does not destroy process.stderr on errors', testDestroyStandard, 'stderr');