Repository navigation
fork child process does not receive message event when using ES modules #34785
Description
Activity
- changed the title
[-]fork child process does not receive message event when using ES6 modules[/-][+]fork child process does not receive message event when using ES modules[/+]on Aug 15, 2020 I think that's explained by the asynchronous nature of ES module loading.
Your script has a race condition, it's just less likely to trigger with (synchronous) CJS. The solution is to have the child notify the parent that it's ready for operation and only then send the message.
I'm going to close this as a wontfix but I can move the issue to nodejs/help if you have follow-up questions.
Reacted by dheeraj br, Raj Thakar, Nico Jansen and Zirafnik- addedchild_processIssues and PRs related to the child_process subsystem.Issues and PRs related to the child_process subsystem.esmIssues and PRs related to the ECMAScript Modules implementation.Issues and PRs related to the ECMAScript Modules implementation.wontfixIssues that will not be fixed.Issues that will not be fixed.
on Aug 15, 2020 @bnoordhuis i believe there is no built-in event to notify the parent, cluster has 'online' event to notify when a fork process has started. a feature where maybe the child process can implicitly emit an event once its started, if that's plausible i can follow-up, if not we can end this here.
Not built-in, no, but there couldn't be - node doesn't know when your script is ready. Call
process.send('ready')in the child and make the parent wait for that event before it starts sending messages of its own.Reacted by João Távora, Frank Lemanschik and Patrick StaarinkReacted by dheeraj brCall process.send('ready') in the child and make the parent wait for that event before it starts sending messages of its own.
Right. This works. But I think at least one should update the documentation (https://nodejs.org/api/child_process.html#child_process_subprocess_send_message_sendhandle_options_callback) to call attention to this very elusive gotcha.
Reacted by Evan Sherwood, tskapo, Simon Siefke, vnc5, Erick Wendel, Nico Jansen, INNOVAWEB, Zirafnik, Nicholas Kyriakides and TylorYao
steps to reproduce the bug
im trying to have two way messaging between child process and the parent process, the parent process works as expected it sends and receives messages from child process, but the child process is only able to send message and not receive them
ive enabled ES6 modules from
package.jsonusing this key-value"type": "module"app.js
script.js
expected behavior
output recieved
Additional information
this issue is not present while using commonJs modules (ie: require('child_process') )
same issue closed without bug fix, nodejs/help#1383
although the workaround would be to call the child.send in the next tick like shown below, this is not homologous to the commonJs behavior