Skip to content
Myr

Fix `.pipe()` with the `stdio` option and orphaned destination subprocesses

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

Piping forces the destination's `stdin` option to `'pipe'`, which cannot be combined with the `stdio` option. That made `to: 'fd3'` and higher unreachable, since those need `stdio`. Piping now leaves `stdin` alone when `stdio` is set. Also, the destination subprocess is spawned before the `to` option is validated. If validation then failed, the destination kept running, e.g. waiting for its `stdin` forever, which prevented the current process from exiting. Its error also became an unhandled rejection, which crashes the current process. A destination spawned by Execa is now terminated, and its error is handled. A destination subprocess passed by the user is left running, since the user still controls it.

authored by Sindre Sorhus · committed by Sindre Sorhus · parent e0b5dbc · Browse files · 5 files+85 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

5 files+85 added−6 removed

  • tests:+49 added−0 removed
  • other:+36 added−6 removed

5 files+85 added−6 removed

modified:docs/pipe.md+7 added-2 removed
modified:lib/pipe/pipe-arguments.js+29 added-4 removed
28 unmodified lines
29 };30};3132const getDestinationStream = (boundOptions, createNested, pipeArguments) => {33 let destinationInfo;3435 try {36 destinationInfo = getDestination(boundOptions, createNested, ...pipeArguments);37 } catch (error) {38 return {destinationError: error};39 }4034 const {destination, pipeOptions} = getDestination(boundOptions, createNested, ...pipeArguments);35 // Keep the null prototype, otherwise a polluted `Object.prototype` would inject option values36 const {from, to, unpipeSignal} = {__proto__: null, ...pipeOptions};41 const {destination, pipeOptions} = destinationInfo;42 // Keep the null prototype, otherwise a polluted `Object.prototype` would inject option values43 const {from, to, unpipeSignal} = {__proto__: null, ...pipeOptions};4445 try {46 const destinationStream = getToStream(destination, to);47 return {48 destination,49 destinationStream,50 from,51 unpipeSignal,52 };53 } catch (error) {54 abortDestination(destination, pipeArguments[0]);55 return {destinationError: error};56 }57};5859/*60The destination has already been spawned at that point, since the `to` option is validated afterwards.61When Execa spawned it, nothing else can end it, so it is terminated. Otherwise, it would keep running, e.g. waiting for its `stdin` forever, which also prevents the current process from exiting. A destination passed by the user is left to them.62In both cases, its error must be handled, otherwise it becomes an unhandled rejection which crashes the current process.63*/64const abortDestination = (destination, firstArgument) => {65 if (destination !== firstArgument) {66 destination.kill();67 }6869 destination.catch(() => {});70};7172// Piping subprocesses can use three syntaxes:73// - source.pipe('command', commandArguments, pipeOptionsOrDestinationOptions)74// - source.pipe`command commandArgument` or source.pipe(pipeOptionsOrDestinationOptions)`command commandArgument`75// - source.pipe(execa(...), pipeOptions)
28 unmodified lines
104const mapDestinationArguments = ({options}) => ({105 options: {106 __proto__: null,107 ...options,85 stdin: 'pipe',108 // Piping writes to the destination's `stdin`, which is forced even if the user set another value.109 // However, `stdin` cannot be combined with `stdio`, which is needed for `to: 'fd3'` and higher.110 ...(options.stdio === undefined ? {stdin: 'pipe'} : {}),111 piped: true,112 },113});114
8 unmodified lines
added:test/fixtures/pipe-destination-error.js+16 added
modified:test/pipe/streaming.js+10 added
436 unmodified lines
437 t.is(await secondPipePromise, destinationResult);438 t.deepEqual(destinationResult.pipedFrom, [sourceResult]);439 t.deepEqual(sourceResult.pipedFrom, []);440});441442// `to: 'fd3'` and higher requires the `stdio` option, which cannot be combined with the `stdin` option that piping sets by default443test('Can pipe to an additional file descriptor of a destination with the "stdio" option', async t => {444 const source = execa('noop.js', [foobarString]);445 const {stdout} = await source.pipe('stdin-fd.js', ['3'], {446 to: 'fd3',447 stdio: ['pipe', 'pipe', 'pipe', {value: 'pipe', input: true}],448 });449 t.is(stdout, foobarString);450});
modified:test/pipe/throw.js+23 added
34 unmodified lines
3536 await assertPipeError(t, pipePromise, 'an Execa subprocess');37 t.like(await source, {stdout: undefined});38});3940// The destination subprocess is spawned before the `to` option is validated, so its error must be handled.41// This runs in another process, since that is where the unhandled rejection crashes it.42test('Destination subprocess error is not unhandled when the "to" option is invalid', async t => {43 const {stdout} = await execa('pipe-destination-error.js', ['fail.js']);44 t.is(stdout, 'REACHED THE END');45});4647test('Destination subprocess is terminated when the "to" option is invalid', async t => {48 const {stdout} = await execa('pipe-destination-error.js', ['stdin.js'], {timeout: 10_000});49 t.is(stdout, 'REACHED THE END');50});5152test('Destination subprocess passed by the user is not terminated when the "to" option is invalid', async t => {53 const source = execa('empty.js', {stdout: 'ignore'});54 const destination = execa('stdin.js');55 const pipePromise = source.pipe(destination, {to: 'fd9'});5657 await assertPipeError(t, pipePromise, 'fd9');58 destination.stdin.end(foobarString);59 const {stdout} = await destination;60 t.is(stdout, foobarString);61});