`execa()` throws `subprocess.stdio is not iterable` and the process crashes when spawning fails with `EMFILE`
When a process has run out of file descriptors, execa() throws a TypeError synchronously instead of returning a subprocess whose promise rejects. Node then emits the real spawn error on the next tick with no listener attached, and that unhandled 'error' event ends the process, even when the caller has caught the TypeError.
Reproduction
Linux, using prlimit from util-linux:
import {execFileSync} from 'node:child_process';
import {execa} from 'execa';
// Lower this process's soft limit on open files below what it already holds.
execFileSync('prlimit', [`--pid=${process.pid}`, '--nofile=3:']);
setTimeout(() => {
console.log('still running');
}, 200);
try {
await execa('node', ['--version']);
} catch (error) {
console.log(`caught: ${error.constructor.name}: ${error.message}`);
}Output with execa 10.0.1 and Node 22.23.1 on Fedora 44 (exit code 1, still running is never printed):
caught: TypeError: subprocess.stdio is not iterable
node:events:497
throw er; // Unhandled 'error' event
^
Error: spawn node EMFILE
at ChildProcess._handle.onexit (node:internal/child_process:285:19)
at onErrorNT (node:internal/child_process:483:16)
at process.processTicksAndRejections (node:internal/process/task_queues:89:21)
Emitted 'error' event on ChildProcess instance at:
at ChildProcess._handle.onexit (node:internal/child_process:291:12)
at onErrorNT (node:internal/child_process:483:16)
at process.processTicksAndRejections (node:internal/process/task_queues:89:21) {
errno: -24,
code: 'EMFILE',
syscall: 'spawn node',
path: 'node',
spawnargs: [ '--version' ]
}
Node.js v22.23.1For comparison, the other paths behave as expected:
execaSync('node', ['--version'])throwsExecaSyncError: Command failed with EMFILE, and the process keeps running.execa('no-such-command-for-repro')without the lowered limit rejects withExecaError: Command failed with ENOENT, and the process keeps running.- A plain
child_process.spawn()with an'error'listener returns a child whosestdioisundefined, receivesspawn node EMFILEon the listener, and the process keeps running.
Cause
On EMFILE and ENFILE, Node's ChildProcess#spawn queues the error for the next tick and returns before it sets up stdio (lib/internal/child_process.js#L405-L408). spawnSubprocessAsync only catches errors thrown by spawn() itself, and then reads subprocess.stdio right after its try block (lib/methods/main-async.js#L104). That read throws the TypeError before any 'error' listener is attached to the subprocess.
Expected
The same handling as other spawn errors:
execa()returns instead of throwing whenspawn()fails withEMFILEorENFILE.- The returned promise rejects with an
ExecaErrorwhosecodeisEMFILEorENFILE, asexecaSync()andENOENTalready do. - The spawn error is never an unhandled
'error'event.
The change should be small: one branch in spawnSubprocessAsync for a subprocess without stdio that waits for its 'error' event and rejects with it, plus a test.
Source: sindresorhus/execa