Fix `execa()` crashing when spawning fails with `EMFILE` or `ENFILE`
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fixes #1262
authored by Sindre Sorhus · committed by Sindre Sorhus · parent 54d7404 · Browse files · 4 files·+64 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+64 added−5 removed
- tests:+34 added−0 removed
- other:+30 added−5 removed
Provenance
- Commit
15c46cd- Tree
f79c04c- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
54d7404
4 files changed+64 added−5 removed
modified:lib/methods/main-async.js+15 added-1 removed
1-import {setMaxListeners} from 'node:events';1+import {setMaxListeners, once} from 'node:events';2 import {spawn} from 'node:child_process';3 import {MaxBufferError} from 'get-stream';4 import {handleCommand} from '../arguments/command.js';5 import {normalizeOptions} from '../arguments/options.js';87 unmodified lines93 verboseInfo,94 });95 }96 97+ // With EMFILE or ENFILE, Node.js does not create the subprocess' streams, and emits the error on the next tick instead of throwing it.98+ // The listener must be added right away, or that error would crash the current process.99+ if (subprocess.stdio === undefined) {100+ return handleEarlyError({101+ error: once(subprocess, 'error').then(([error]) => error),102+ command,103+ escapedCommand,104+ fileDescriptors,105+ options,106+ startTime,107+ verboseInfo,108+ });109+ }110+111 const controller = new AbortController();112 setMaxListeners(Infinity, controller.signal);113 114 const originalStreams = [...subprocess.stdio];131 unmodified linesmodified:lib/return/early-error.js+15 added-4 removed
19 unmodified lines202122232424252627282930313132333334353661 unmodified lines989910010110310210310410510610710810911011111211311419 unmodified lines // `ChildProcess.prototype.kill()` would signal the current process group instead, since its process id is `0`. subprocess.kill = () => false; const all = createDummyStreams(subprocess, fileDescriptors);
const earlyError = makeEarlyError({ const promise = handleDummyPromise({ error, command, escapedCommand, fileDescriptors, options, startTime, isSync: false, verboseInfo, }); const promise = handleDummyPromise(earlyError, verboseInfo, options); return { subprocess, promise, all: options.all ? all : undefined,61 unmodified lines [Symbol.asyncIterator]: iterable, };};
const handleDummyPromise = async (error, verboseInfo, options) => handleResult(error, verboseInfo, options);// `error` is a promise when the spawn error is only emitted on the next tickconst handleDummyPromise = async ({error, command, escapedCommand, fileDescriptors, options, startTime, verboseInfo}) => { const earlyError = makeEarlyError({ error: await error, command, escapedCommand, fileDescriptors, options, startTime, isSync: false, }); return handleResult(earlyError, verboseInfo, options);};added:test/fixtures/emfile.js+27 added
123456789101112131415161718192021222324252627#!/usr/bin/env nodeimport {openSync, closeSync} from 'node:fs';import process from 'node:process';import {execa} from '../../index.js';
// Open files until none can be opened anymore, so that spawning fails with EMFILEconst fileDescriptors = [];try { while (true) { fileDescriptors.push(openSync(process.execPath, 'r')); }} catch { // No file descriptor is left}
let error;try { await execa(process.execPath, ['--version']);} catch (error_) { error = error_;}
for (const fileDescriptor of fileDescriptors) { closeSync(fileDescriptor);}
console.log(`${error.name} ${error.code}`);modified:test/return/early-error.js+7 added
111 unmodified lines112 assert.equal(failed, true);113 });114 115 if (!isWindows) {116+ // Without any file descriptor left, `child_process.spawn()` fails with EMFILE.117+ // Node.js then emits the error on the next tick, without creating the subprocess' streams.118+ test('execa() rejects if spawning fails with EMFILE', async () => {119+ const {stdout} = await execa('sh', ['-c', 'ulimit -n 64 && exec emfile.js']);120+ assert.equal(stdout, 'ExecaError EMFILE');121+ });122+123 test('execa() rejects if running non-executable', async () => {124 await assertRejects(execa('non-executable.js'));125 });126 160 unmodified lines