Skip to content
Myr

Fix an uncaught error when a file target cannot be opened and the subprocess fails to spawn

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 15c46cd · Browse files · 4 files+41 added−5 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+41 added−5 removed

  • tests:+33 added−0 removed
  • other:+8 added−5 removed

4 files+41 added−5 removed

modified:lib/stdio/handle.js+8 added-5 removed
239 unmodified lines
240// If the subprocess spawning fails (e.g. due to an invalid command), the streams need to be manually destroyed.241// We need to create those streams before subprocess spawning, in case their creation fails, e.g. when passing an invalid generator as argument.242// Like this, an exception would be thrown, which would prevent spawning a subprocess.243export const cleanupCustomStreams = fileDescriptors => {244 for (const {stdioItems} of fileDescriptors) {245 for (const {stream} of stdioItems) {246 if (stream !== undefined && !isStandardStream(stream)) {247 stream.destroy();248 }244 for (const {stream} of fileDescriptors.flatMap(({stdioItems}) => stdioItems)) {245 if (stream === undefined || isStandardStream(stream)) {246 continue;247 }248249 // The stream can still error after being destroyed, e.g. when its file cannot be opened.250 // That error would be uncaught and crash the current process, although it is irrelevant since execa already fails with its own error.251 stream.on('error', () => {});252 stream.destroy();253 }254};255256// When the `std*: Iterable | WebStream | URL | filePath`, `input` or `inputFile` option is used, we pipe to `subprocess.std*`.
10 unmodified lines
added:test/fixtures/early-error-file.js+8 added
added:test/fixtures/invalid-stdio-file.js+10 added
modified:test/return/early-error.js+15 added
96 unmodified lines
97test('child_process.spawn() early errors are propagated', async () => {98 await assertRejects(getEarlyErrorSubprocess(), expectedEarlyError);99});100101// File targets are opened before spawning, then closed since the subprocess did not spawn.102// Failing to open them must not be an uncaught exception, which would crash the current process.103const testEarlyErrorFile = async optionName => {104 const {stdout} = await execa('early-error-file.js', [optionName]);105 assert.equal(stdout, expectedEarlyError.code);106};107108test('child_process.spawn() early errors do not crash if an output file cannot be opened', () => testEarlyErrorFile('stdout'));109test('child_process.spawn() early errors do not crash if an input file cannot be opened', () => testEarlyErrorFile('stdin'));110111test('Invalid stdio options do not crash if an output file cannot be opened', async () => {112 const {stdout} = await execa('invalid-stdio-file.js');113 assert.equal(stdout, 'The `stdout` option cannot be an iterable.');114});115116test('child_process.spawn() early errors are returned', async () => {117 const {failed} = await getEarlyErrorSubprocess({reject: false});118 assert.equal(failed, true);119});
182 unmodified lines