Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help with IPC and child_process with ES6 #1383

Closed
sammyrc34 opened this issue Jul 12, 2018 · 4 comments
Closed

Help with IPC and child_process with ES6 #1383

sammyrc34 opened this issue Jul 12, 2018 · 4 comments

Comments

@sammyrc34
Copy link

  • 8.11.2 & 10.6
  • Linux, debian x86_64
  • runtime
  • child_process

IPC with a ES6 child process created via child_process.fork() appears to go nowhere. When sending a message, the child appears to never receive it. Further testing shows that other events (eg exit) are working.

To demonstrate, parent.mjs:

import cp from 'child_process';
const kid = cp.fork( 'test.mjs' );
kid.send( { msg: "Hey there kid" } );
setTimeout( () => { process.exit(0) }, 2000 );  

And test.mjs

class Test {
  constructor() { }
  msg(message) { console.log( "Kid got", message ); }
}
const test = new Test();
process.on( 'message', (m) => { test.msg(m) } );

To test further, I created my own named socket IPC using net.createServer(path), between such a parent and child, and bidirectional communication is working.

I really just want to know if this is something I should create an issue about.

@vsemozhetbyt
Copy link

cc @nodejs/child_process @nodejs/modules-active-members

@benjamingr
Copy link
Member

Can you try putting the .send in a setTimeout? I think you're calling .send synchronously before the child process has run the process.on( 'message', (m) => { test.msg(m) } ); code.

@bmeck
Copy link
Member

bmeck commented Jul 12, 2018

in general you can see dropped messages due to timing like @benjamingr said, it is a much more foolproof workflow to have your child notify the parent that it is fully loaded before sending it messages from the parent.

@sammyrc34
Copy link
Author

@benjamingr Thank you! You're correct.

@bmeck, yes, that is a much better approach.

Thank you both for the help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants