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
Provenance
- Commit
61a1623- Tree
91eb790- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
7122767
4 files changed+22 added−10 removed
modified:lib/stdio/async.js+5 added-5 removed
5 unmodified lines67891011121011121314151615161718192026 unmodified lines5 unmodified linesexport const handleInputAsync = options => handleInput(addPropertiesAsync, options);
const addPropertiesAsync = { input: { filePath: ({value}) => ({value: createReadStream(value), autoDestroy: true}), webStream: ({value}) => ({value: Readable.fromWeb(value), autoDestroy: true}), iterable: ({value}) => ({value: Readable.from(value), autoDestroy: true}), filePath: ({value}) => ({value: createReadStream(value)}), webStream: ({value}) => ({value: Readable.fromWeb(value)}), iterable: ({value}) => ({value: Readable.from(value)}), }, output: { filePath: ({value}) => ({value: createWriteStream(value), autoDestroy: true}), webStream: ({value}) => ({value: Writable.fromWeb(value), autoDestroy: true}), filePath: ({value}) => ({value: createWriteStream(value)}), webStream: ({value}) => ({value: Writable.fromWeb(value)}), iterable({optionName}) { throw new TypeError(`The \`${optionName}\` option cannot be an iterable.`); }, },26 unmodified linesmodified:lib/stdio/native.js+1 added-1 removed
43 unmodified lines44454647484843 unmodified lines
return standardStream;};
const STANDARD_STREAMS = [process.stdin, process.stdout, process.stderr];export const STANDARD_STREAMS = [process.stdin, process.stdout, process.stderr];modified:lib/stream.js+4 added-4 removed
1 import {Buffer} from 'node:buffer';2 import {once} from 'node:events';3 import {finished} from 'node:stream/promises';4-import process from 'node:process';4 import getStream, {getStreamAsArrayBuffer} from 'get-stream';5 import mergeStreams from '@sindresorhus/merge-streams';6 import {throwOnTimeout, cleanupOnExit} from './kill.js';7+import {STANDARD_STREAMS} from './stdio/native.js';8 9 // `all` interleaves `stdout` and `stderr`10 export const makeAllStream = (spawned, {all}) => {11 if (!all) {67 unmodified lines79 const throwOnCustomStreamsError = customStreams => customStreams80 .filter(({value, type, direction}) => !shouldWaitForCustomStream(value, type, direction))81 .map(({value}) => throwOnStreamError(value));82 83-const shouldWaitForCustomStream = (value, type, direction) => type === 'filePath'84- || (direction === 'output' && value !== process.stdout && value !== process.stderr);83+const shouldWaitForCustomStream = (value, type, direction) => (type === 'filePath' || direction === 'output')84+ && !STANDARD_STREAMS.includes(value);85 86 const throwIfStreamError = stream => stream === null ? [] : [throwOnStreamError(stream)];87 88 const throwOnStreamError = async stream => {5 unmodified lines94 // 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.96 const 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 linesmodified:test/stdio/array.js+12 added
253 unmodified lines254 test('stdin can be ["overlapped", "pipe"]', testOverlapped, getStdinOption);255 test('stdout can be ["overlapped", "pipe"]', testOverlapped, getStdoutOption);256 test('stderr can be ["overlapped", "pipe"]', testOverlapped, getStderrOption);257 test('stdio[*] can be ["overlapped", "pipe"]', testOverlapped, getStdioOption);258+259+const 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+};266+267+test('Does not destroy process.stdin on errors', testDestroyStandard, 'stdin');268+test('Does not destroy process.stdout on errors', testDestroyStandard, 'stdout');269+test('Does not destroy process.stderr on errors', testDestroyStandard, 'stderr');