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

the child process created by child_process.fork can't receive message correctly when using ES module #39140

Closed
xiefenga opened this issue Jun 24, 2021 · 5 comments
Labels
child_process Issues and PRs related to the child_process subsystem.

Comments

@xiefenga
Copy link

  • Version: v16.4.0、v14.17.1
  • Platform: Windows 10 x64
  • Subsystem: child_process

What steps will reproduce the bug?

when using ES module and child_process to fork child process, even 'spawn' event has happend,child process failed receive message from parent process.

Complete Testcase

package.json:

{
  "type": "module"
}

master.js

import { join } from 'path'
import { fork } from 'child_process'
import { fileURLToPath } from 'url'

const __dirname = join(
  fileURLToPath(import.meta.url),
  '../'
)

const worker = fork(
  join(__dirname, './worker.js')
);

worker.on('spawn', () => {
  console.log('start success');
  worker.send('hello', () => console.log('send success'));
});

worker.js

console.log('child process has started, processID:', process.pid);

process.on('message', (msg) => {
  console.log('child process get msg:', msg);
});

console ouput:

start success
send success
child process has started, processID: 6224

if i delay 1s send message to child process, it works:

worker.on('spawn', () => {
  console.log('start success');
  setTimeout(() => {
    worker.send('hello', () => console.log('send success'));
  }, 1000);
});

console output:

start success
child process has started, processID: 18156
send success
child process get msg: hello

What is the expected behavior?

the expected behavior just like this:

start success
send success
child process has started, processID: 16428
child process get msg: hello

when i rename worker.js to worker.cjs, got expected result.

Possible cause

I suspect this may have something to do with ESM asynchronous execution.

@Ayase-252 Ayase-252 added the child_process Issues and PRs related to the child_process subsystem. label Jun 24, 2021
@Linkgoron
Copy link
Member

Linkgoron commented Jun 28, 2021

I think that this is a duplicate of #37782

tl;dr, yes this is an issue with how loading is now async, and the message is emitted too early before having an option to register

@huseyinacacak-janea
Copy link
Contributor

Hi @xiefenga, I wanted to look into this issue but wasn't able to reproduce it (I tried Node.js v18.20.4, v20.18.0, v22.10.0, and v23.1.0).
Do you think this issue can be closed now?

@xiefenga
Copy link
Author

maybe it has been fixed in new version

@xiefenga
Copy link
Author

but i can't judge whethor can close this issue or not

@xiefenga
Copy link
Author

i tried in v20.15.0, it worked as expected, i think this issue can be closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
child_process Issues and PRs related to the child_process subsystem.
Projects
None yet
Development

No branches or pull requests

4 participants