Repository navigation
Feature request: ChildProcess 'spawn' event #35288
Description
Is your feature request related to a problem? Please describe.
After calling child_process.spawn, I'd like to know when the child process has successfully spawned and there's no longer the possibility of an 'error' event from failing to spawn (i.e. error type # 1 in the docs for that 'error' event, e.g. EPERM, ENOENT).
Currently I just wait 100 milliseconds (after calling child_process.spawn), and if the child process hasn't emitted an 'error' event by that time, I assume it spawned successfully. My code looks something like this:
const { spawn } = require('child_process');
const { promisify } = require('util');
const { once } = require('events');
const timeout = promisify(setTimeout);
async function doSomethingWithChildProcess(){
const subprocess = spawn(...spawnArgs));
await Promise.race([
timeout(100),
once(subprocess, 'error').then(([error]) => Promise.reject(error))
]);
// now do something with the running child process...
}This seems to work, but I'm not sure how reliable it is, and anyways, it is certainly a hack.
I'm wondering if there could be a better (more correct) way..
Describe the solution you'd like
Is there some point of execution in Node where we know there was no error spawning the child process?
If so, we could introduce a new 'spawn' event for the ChildProcess class, to be emitted at that point in execution?
That would remove the need for the unreliable hack I described above.
It would also work nicely with Node.js's events.once function. For example, the code from above could be updated like this:
const { spawn } = require('child_process');
-const { promisify } = require('util');
const { once } = require('events');
-const timeout = promisify(setTimeout);
async function doSomethingWithChildProcess(){
const subprocess = spawn(...spawnArgs);
- await Promise.race([
- timeout(100),
- once(subprocess, 'error').then(([error]) => Promise.reject(error))
- ]);
+ await once(subprocess, 'spawn');
// now do something with the running child process...
}Describe alternatives you've considered
Just the hack I described above (of expecting any error spawning to happen within 100 milliseconds of calling child_process.spawn).
I can't think of any other possible solutions at this time.
Activity
bnoordhuis commentedon Sep 22, 2020
It's not hard to implement, see diff. Feel free to steal for a PR, no attribution needed. Needs docs and tests, however.
Details
Apropos this:
Yes, but there's a subtlety that is likely to trip up some people (mostly the magical thinker kind): while
bash bobspawns successfully,bashthen fails to spawnbob. You'll still get aspawnevent.That caveat also applies to
{ shell: true }and it seems to be a perennial source of confusion to novice users who expect it to DWIM rather than do what the documentation says it does.kaushik94 commentedon Sep 22, 2020
@bnoordhuis can I take this up?
zenflow commentedon Sep 22, 2020
I'd also like to claim this issue 😄 @kaushik94 Would that be ok with you?
kaushik94 commentedon Sep 22, 2020
@zenflow I'm sorry, I thought you were a maintainer. Sure you claim the issue as you raised the issue. Thanks, looking forward!
zenflow commentedon Sep 22, 2020
Thanks @kaushik94 😃
child_process: add ChildProcess 'spawn' event
child_process: add ChildProcess 'spawn' event
alexp1917 commentedon Mar 19, 2021
this this event never fire sometimes? the docs say "if emitted" but don't specify why it wouldn't be. It is sometimes not emitted when things to smoothly (on windows, i am doing
var child = spawn('yarn.cmd');then listening for all events right away - and seeing an exit and close but not a spawn event. thanksdoc: clarify when child process 'spawn' event is *not* emitted
zenflow commentedon Mar 20, 2021
@alexp1917 Yeah I agree the docs could be more explicit about the case where this event does not fire. Please see my PR and let me know if it makes things perfectly clear: https://github.com/nodejs/node/pull/37833/files
@alexp1917
'error'event not firing in these cases where'spawn'is not fired? Either a'spawn'event or an'error'event should fire, every time.If the answer to both of these questions is "yes", then I think we may have a bug in this feature..
alexp1917 commentedon Mar 20, 2021
doc: clarify when child process 'spawn' event is *not* emitted
doc: clarify when child process 'spawn' event is *not* emitted
child_process: add ChildProcess 'spawn' event
doc: clarify when child process 'spawn' event is *not* emitted
sylann commentedon Jun 10, 2021
I also have a case of missing 'spawn' event but only in certain circonstances.
With node v15 on macos, it works
With node v16 on a windows vm on this same mac, it works too.
However, if I run the spawn command from an electron main process on the windows vm,
then I never get a "spawn" event even though the child process has started successfully.
I never get and "error" event either.
So I ended up adding a terrible setTimeout to resolve automatically if no error occured.
Side notes, I have tried spawning .exe files and .bat files or linux programs such as 'ls' and it doesn't change what I've said above.
qnimbus commentedon May 23, 2022
qnimbus commentedon May 23, 2022
Note: I accidentally hid my previous comment - this is an unhidden copy of that comment
I seem to experience this issue too - the 'error' event never fires and my child process seems to spawn succesfully (judging by the output on my console) but the 'spawn' event also never fires. Or maybe it fires before the event handler is registered, but either way - the above code does not work for me.
I've managed to 'fix' this issue with a bit of a hack - With the absence of the 'error' event getting thrown, I'm running a while-loop until
cp.connected== true after which I return the resolved promise:fix(launch): surface X-Plane spawn errors (EACCES, ENOENT) in the dialog
child_process: add ChildProcess 'spawn' event