Skip to content
Myr

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

3 files+44 added−3 removed

modified:lib/resolve/wait-stream.js+12 added-2 removed
modified:lib/resolve/wait-subprocess.js+3 added-1 removed
134 unmodified lines
135136// 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.139const waitForCustomStreamsEnd = (fileDescriptors, streamInfo) => fileDescriptors.flatMap(({stdioItems}, fdNumber) => stdioItems139const 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 })));147148// Fails when the subprocess emits an `error` event149const throwOnSubprocessError = async (subprocess, {signal}) => {
3 unmodified lines
modified:test/stdio/node-stream-custom.js+29 added
60 unmodified lines
61test('stdout cannot be [Writable, "pipe"] without a file descriptor, sync', testLazyFileWritableSync, 1);62test('stderr cannot be [Writable, "pipe"] without a file descriptor, sync', testLazyFileWritableSync, 2);63test('stdio[*] cannot be [Writable, "pipe"] without a file descriptor, sync', testLazyFileWritableSync, 3);6465/*66An output stream is written to, then handed back to the user.67We must only wait for our writes to be flushed, not for the user to consume its readable side,68otherwise the subprocess promise hangs until they do.69*/70const 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};7576test('stdout does not wait for the user to consume the output stream', testUnconsumedOutputStream, 1);77test('stderr does not wait for the user to consume the output stream', testUnconsumedOutputStream, 2);78test('stdio[*] does not wait for the user to consume the output stream', testUnconsumedOutputStream, 3);7980test('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});8687// The stream is handed back to the user with its data still readable88test('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});9394test('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