Skip to content
Myr

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

4 files+64 added−5 removed

modified:lib/methods/main-async.js+15 added-1 removed
1import {setMaxListeners} from 'node:events';1import {setMaxListeners, once} from 'node:events';2import {spawn} from 'node:child_process';3import {MaxBufferError} from 'get-stream';4import {handleCommand} from '../arguments/command.js';5import {normalizeOptions} from '../arguments/options.js';
87 unmodified lines
93 verboseInfo,94 });95 }9697 // 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 }110111 const controller = new AbortController();112 setMaxListeners(Infinity, controller.signal);113114 const originalStreams = [...subprocess.stdio];
131 unmodified lines
modified:lib/return/early-error.js+15 added-4 removed
added:test/fixtures/emfile.js+27 added
modified:test/return/early-error.js+7 added
111 unmodified lines
112 assert.equal(failed, true);113});114115if (!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 });122123 test('execa() rejects if running non-executable', async () => {124 await assertRejects(execa('non-executable.js'));125 });126
160 unmodified lines