Fix the subprocess promise hanging until the user consumes an output stream
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sindre Sorhus · committed by Sindre Sorhus · parent 587045a · Browse files · 3 files·+44 added−3 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+44 added−3 removed
- tests:+29 added−0 removed
- other:+15 added−3 removed
Provenance
- Commit
004f3b3- Tree
0b1a394- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
587045a
3 files changed+44 added−3 removed
modified:lib/resolve/wait-stream.js+12 added-2 removed
123456678910111213141516121718192021222324252681 unmodified linesimport {finished} from 'node:stream/promises';
// Wraps `finished(stream)` to handle the following case:// - When the subprocess exits, Node.js automatically calls `subprocess.stdin.destroy()`, which we need to ignore.// - However, we still need to throw if `subprocess.stdin.destroy()` is called before subprocess exit.export const waitForStream = async (stream, fdNumber, streamInfo, {isSameDirection, stopOnExit = false} = {}) => {export const waitForStream = async (stream, fdNumber, streamInfo, { isSameDirection, stopOnExit = false, readable = true, writable = true,} = {}) => { const state = handleStdinDestroy(stream, streamInfo); const abortController = new AbortController(); try { await Promise.race([ ...(stopOnExit ? [streamInfo.exitPromise] : []), finished(stream, {cleanup: true, signal: abortController.signal}), finished(stream, { cleanup: true, signal: abortController.signal, readable, writable, }), ]); } catch (error) { if (!state.stdinCleanedUp) { handleStreamError(error, fdNumber, streamInfo, isSameDirection);81 unmodified linesmodified:lib/resolve/wait-subprocess.js+3 added-1 removed
134 unmodified lines135 136 // Some `stdin`/`stdout`/`stderr` options create a stream, e.g. when passing a file path.137 // The `.pipe()` method automatically ends that stream when `subprocess` ends.138 // This makes sure we wait for the completion of those streams, in order to catch any error.139-const waitForCustomStreamsEnd = (fileDescriptors, streamInfo) => fileDescriptors.flatMap(({stdioItems}, fdNumber) => stdioItems139+const waitForCustomStreamsEnd = (fileDescriptors, streamInfo) => fileDescriptors.flatMap(({stdioItems, direction}, fdNumber) => stdioItems140 .filter(({value, stream = value}) => isNodeStream(stream, {checkOpen: false}) && !isStandardStream(stream))141 .map(({type, value, stream = value}) => waitForStream(stream, fdNumber, streamInfo, {142 isSameDirection: TRANSFORM_TYPES.has(type),143 stopOnExit: type === 'native',144+ // Output streams are destinations which are handed back to the user: waiting for their readable side would mean waiting for the user to consume them.145+ readable: direction !== 'output' || TRANSFORM_TYPES.has(type),146 })));147 148 // Fails when the subprocess emits an `error` event149 const throwOnSubprocessError = async (subprocess, {signal}) => {3 unmodified linesmodified:test/stdio/node-stream-custom.js+29 added
60 unmodified lines61 test('stdout cannot be [Writable, "pipe"] without a file descriptor, sync', testLazyFileWritableSync, 1);62 test('stderr cannot be [Writable, "pipe"] without a file descriptor, sync', testLazyFileWritableSync, 2);63 test('stdio[*] cannot be [Writable, "pipe"] without a file descriptor, sync', testLazyFileWritableSync, 3);64 65+/*66+An output stream is written to, then handed back to the user.67+We must only wait for our writes to be flushed, not for the user to consume its readable side,68+otherwise the subprocess promise hangs until they do.69+*/70+const testUnconsumedOutputStream = async (t, fdNumber) => {71+ const stream = new PassThrough();72+ const {exitCode} = await execa('empty.js', {...getStdio(fdNumber, [stream, 'pipe']), timeout: 1e3});73+ t.is(exitCode, 0);74+};75+76+test('stdout does not wait for the user to consume the output stream', testUnconsumedOutputStream, 1);77+test('stderr does not wait for the user to consume the output stream', testUnconsumedOutputStream, 2);78+test('stdio[*] does not wait for the user to consume the output stream', testUnconsumedOutputStream, 3);79+80+test('stdout does not wait for the user to consume the output stream on subprocess errors', async t => {81+ const stream = new PassThrough();82+ const error = await t.throwsAsync(execa('fail.js', {...getStdio(1, [stream, 'pipe']), timeout: 1e3}));83+ t.false(error.timedOut);84+ t.is(error.exitCode, 2);85+});86+87+// The stream is handed back to the user with its data still readable88+test('output stream data is readable once the subprocess is done', async t => {89+ const stream = new PassThrough();90+ await execa('noop.js', [foobarString], {...getStdio(1, [stream, 'pipe'])});91+ t.is(await text(stream), `${foobarString}\n`);92+});93+94 test('Waits for custom streams destroy on subprocess errors', async t => {95 let isWaitedForDestroy = false;96 const stream = new Writable({97 destroy: callbackify(async error => {92 unmodified lines