Handle stream errors even with `buffer: false` (#729)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 1907e7c · Browse files · 2 files·+41 added−6 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+41 added−6 removed
- tests:+18 added−5 removed
- other:+23 added−1 removed
Provenance
- Commit
fcc8e11- Tree
626f5d5- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
1907e7c
2 files changed+41 added−6 removed
modified:lib/stream.js+23 added-1 removed
9 unmodified lines1011121314151617181920212223242526272829303132333430 unmodified lines6566676852697071727374757677787980102 unmodified linesExpand all18318418518618718818919019110 unmodified lines9 unmodified linesexport const makeAllStream = ({stdout, stderr}, {all}) => all && (stdout || stderr) ? mergeStreams([stdout, stderr].filter(Boolean)) : undefined;
// `childProcess.stdout|stderr` do not end until they have been consumed by `childProcess.all`.// `childProcess.all` does not end until `childProcess.stdout|stderr` have ended.// That's a good thing, since it ensures those streams are fully read and destroyed, even on errors.// However, this creates a deadlock if `childProcess.all` is not being read.// Doing so prevents the process from exiting, even when it failed.// It also prevents those streams from being properly destroyed.// This can only happen when:// - `all` is `true`// - `buffer` is `false`// - `childProcess.all` is not read by the user// Therefore, we forcefully resume `childProcess.all` flow on errors.const resumeAll = all => { if (all?.readableFlowing === null) { all.resume(); }};
// On failure, `result.stdout|stderr|all` should contain the currently buffered stream// They are automatically closed and flushed by Node.js when the child process exits// We guarantee this by calling `childProcess.kill()`// When `buffer` is `false`, `streamPromise` is `undefined` and there is no buffered data to retrieve30 unmodified lines readableObjectMode: true,};
const getStreamPromise = async ({stream, encoding, buffer, maxBuffer}) => { if (!stream || !buffer) { if (!stream) { return; }
if (!buffer) { return finished(stream); }
if (stream.readableObjectMode) { return getStreamAsArray(stream, {maxBuffer}); }
102 unmodified linesExpand all ...throwOnTimeout(timeout, context, controller), ]); } catch (error) { spawned.kill(); resumeAll(spawned.all); const results = await Promise.all([ error, waitForFailedProcess(processSpawnPromise, processErrorPromise, processExitPromise), Promise.all(stdioPromises.map(stdioPromise => getBufferedData(stdioPromise, encoding))),10 unmodified linesmodified:test/stream.js+18 added-5 removed
122 unmodified lines123 test('Can listen to `data` events on stderr when `buffer` set to `true`', testIterationBuffer, 2, true, true, false);124 test('Can listen to `data` events on stdio[*] when `buffer` set to `true`', testIterationBuffer, 3, true, true, false);125 test('Can listen to `data` events on all when `buffer` set to `true`', testIterationBuffer, 1, true, true, true);126 127+const testNoBufferStreamError = async (t, index, all) => {128+ const subprocess = execa('noop-fd.js', [`${index}`], {...fullStdio, buffer: false, all});129+ const stream = all ? subprocess.all : subprocess.stdio[index];130+ stream.destroy(new Error('test'));131+ await t.throwsAsync(subprocess, {message: /test/});132+};133+134+test('Listen to stdout errors even when `buffer` is `false`', testNoBufferStreamError, 1, false);135+test('Listen to stderr errors even when `buffer` is `false`', testNoBufferStreamError, 2, false);136+test('Listen to stdio[*] errors even when `buffer` is `false`', testNoBufferStreamError, 3, false);137+test('Listen to all errors even when `buffer` is `false`', testNoBufferStreamError, 1, true);138+139 const maxBuffer = 10;140 141 const testMaxBufferSuccess = async (t, index, all) => {142 await t.notThrowsAsync(execa('max-buffer.js', [`${index}`, `${maxBuffer}`], {...fullStdio, maxBuffer, all}));128 unmodified lines271 // This specific behavior does not happen on Windows.272 // Also, on macOS, it randomly happens, which would make those tests randomly fail.273 if (process.platform === 'linux') {274 const testBufferNotRead = async (t, index, all) => {263- const {timedOut} = await t.throwsAsync(execa('max-buffer.js', [`${index}`], {...fullStdio, buffer: false, all, timeout: 1e3}));275+ const subprocess = execa('max-buffer.js', [`${index}`], {...fullStdio, buffer: false, all, timeout: 1e3});276+ const {timedOut} = await t.throwsAsync(subprocess);277 t.true(timedOut);278 };279 267- test.serial('Process buffers stdout, which prevents exit if not read and buffer is false', testBufferNotRead, 1, false);268- test.serial('Process buffers stderr, which prevents exit if not read and buffer is false', testBufferNotRead, 2, false);269- test.serial('Process buffers stdio[*], which prevents exit if not read and buffer is false', testBufferNotRead, 3, false);270- test.serial('Process buffers all, which prevents exit if not read and buffer is false', testBufferNotRead, 1, true);280+ test('Process buffers stdout, which prevents exit if not read and buffer is false', testBufferNotRead, 1, false);281+ test('Process buffers stderr, which prevents exit if not read and buffer is false', testBufferNotRead, 2, false);282+ test('Process buffers stdio[*], which prevents exit if not read and buffer is false', testBufferNotRead, 3, false);283+ test('Process buffers all, which prevents exit if not read and buffer is false', testBufferNotRead, 1, true);284 285 const testBufferRead = async (t, index, all) => {286 const subprocess = execa('max-buffer.js', [`${index}`], {...fullStdio, buffer: false, all, timeout: 1e4});287 const stream = all ? subprocess.all : subprocess.stdio[index];93 unmodified lines