Skip to content
Myr

Fix a subprocess which failed to spawn signaling the current process and never ending its streams

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 9842f1a · Browse files · 3 files+129 added−18 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+129 added−18 removed

  • tests:+78 added−0 removed
  • other:+51 added−18 removed

3 files+129 added−18 removed

modified:lib/return/early-error.js+43 added-17 removed
modified:lib/terminate/kill-descendants.js+8 added-1 removed
modified:test/return/early-error.js+78 added
1import {arch} from 'node:os';2import process from 'node:process';3import {finished} from 'node:stream/promises';4import test from 'ava';5import {execa, execaSync, $} from '../../index.js';6import {setFixtureDirectory} from '../helpers/fixtures-directory.js';7import {foobarString, foobarUint8Array} from '../helpers/input.js';8import {fullStdio} from '../helpers/stdio.js';9import {10 earlyErrorOptions,11 getEarlyErrorSubprocess,
6 unmodified lines
1819const isWindows = process.platform === 'win32';20const ENOENT_REGEXP = isWindows ? /failed with exit code 1/ : /spawn.* ENOENT/;2122// A nonexistent command fails asynchronously, so it does not go through the early error path, but the subprocess still has no process to signal23test('kill() does not signal the current process when the command does not exist', async t => {24 const subprocess = execa('nonexistent-command-for-execa');25 t.is(subprocess.pid, undefined);26 t.is(subprocess.kill(), false);27 t.is((await t.throwsAsync(subprocess)).code, 'ENOENT');28});2930test('execaSync() throws error if ENOENT', t => {31 t.throws(() => {32 execaSync('foo');33 }, {message: ENOENT_REGEXP});
10 unmodified lines
4445test('child_process.spawn() early errors have correct shape', testEarlyErrorShape, true);46test('child_process.spawn() early errors have correct shape - reject false', testEarlyErrorShape, false);4748// The subprocess never spawned, so there is no process to signal.49// `ChildProcess.prototype.kill()` would signal the current process group instead, since its process id is `0`.50// The handle is replaced by a stub, so that a regression is caught by an assertion instead of killing this test process.51test('kill() does not signal the current process on early errors', async t => {52 const subprocess = getEarlyErrorSubprocess({reject: false});53 let isSignaled = false;54 subprocess.nodeChildProcess._handle = {55 kill() {56 isSignaled = true;57 return 0;58 },59 };6061 t.is(subprocess.kill(), false);62 t.false(isSignaled);63 const {failed, isCanceled} = await subprocess;64 t.true(failed);65 t.false(isCanceled);66});6768test('child_process.spawn() early errors are propagated', async t => {69 await t.throwsAsync(getEarlyErrorSubprocess(), expectedEarlyError);70});71
70 unmodified lines
142test('child_process.spawn() early errors can use .readableStream()', testEarlyErrorWebConvertor, 'readableStream');143test('child_process.spawn() early errors can use .writableStream()', testEarlyErrorWebConvertor, 'writableStream');144test('child_process.spawn() early errors can use .transformStream()', testEarlyErrorWebConvertor, 'transformStream');145146// The subprocess never started, so those streams have no contents.147// They must end right away, like `subprocess.stdout` and `.iterable()` do, instead of hanging forever.148const testEarlyErrorReadableEnd = async (t, streamMethod) => {149 const subprocess = getEarlyErrorSubprocess();150 const chunks = await Array.fromAsync(subprocess[streamMethod]());151152 t.deepEqual(chunks, []);153 await t.throwsAsync(subprocess);154};155156test('child_process.spawn() early errors end .readable()', testEarlyErrorReadableEnd, 'readable');157test('child_process.spawn() early errors end .duplex()', testEarlyErrorReadableEnd, 'duplex');158test('child_process.spawn() early errors end .readableStream()', testEarlyErrorReadableEnd, 'readableStream');159160const testEarlyErrorWritableEnd = async (t, streamMethod) => {161 const subprocess = getEarlyErrorSubprocess();162 const stream = subprocess[streamMethod]();163 stream.end(foobarString);164 await finished(stream, {readable: false});165166 t.false(stream.writable);167 await t.throwsAsync(subprocess);168};169170test('child_process.spawn() early errors end .writable()', testEarlyErrorWritableEnd, 'writable');171test('child_process.spawn() early errors end .duplex() writable side', testEarlyErrorWritableEnd, 'duplex');172173test('child_process.spawn() early errors end .writableStream()', async t => {174 const subprocess = getEarlyErrorSubprocess();175 const writer = subprocess.writableStream().getWriter();176 await writer.write(foobarUint8Array);177 await writer.close();178179 await t.throwsAsync(subprocess);180});181182test('child_process.spawn() early errors end .transformStream()', async t => {183 const subprocess = getEarlyErrorSubprocess();184 const {readable, writable} = subprocess.transformStream();185 const writer = writable.getWriter();186 await writer.write(foobarUint8Array);187 await writer.close();188 const chunks = await Array.fromAsync(readable);189190 t.deepEqual(chunks, []);191 await t.throwsAsync(subprocess);192});193194const testEarlyErrorIpc = async (t, runIpcMethod) => {195 const subprocess = getEarlyErrorSubprocess({ipc: true});196 t.throws(() => {197 runIpcMethod(subprocess);
39 unmodified lines