-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
cluster: worker doesn't react on message #39854
Comments
Could you create a repo which replicates this problem? Can I take this issue up? |
The example above, just a minimal and illustrative example. Pretty simple to test on your PC. Sorry, I did not understand why you need a repo. |
Just with some tests and all for easy debugging . Nevermind, I'll take up this issue |
This is probably a duplicate of #37782 and #39140 e.g. this works: import cluster from 'cluster';
if (cluster.isMaster) {
const worker = cluster.fork();
worker.on('message', message => {
console.log(message);
});
setTimeout(() => {
worker.send('From Master');
}, 1000);
} else {
process.on('message', message => {
console.log(message);
});
process.send('From Worker');
} tl;dr of the other issues, there's a minor race condition (read: bug) because modules are now loaded asynchronously and when starting a module messages are missed as you don't register in time to get them. The safest way to start without a race condition is to get a ping from the child and then you know you're safe. e.g. POC: import cluster from 'cluster';
import { once } from 'events';
if (cluster.isMaster) {
const worker = cluster.fork();
let firstMessage = true
worker.on('message', message => {
if (firstMessage) {
firstMessage = false;
} else {
console.log(message);
}
});
await once(worker, 'message');
worker.send('From Master');
} else {
process.on('message', message => {
console.log(message);
});
process.send('IM READY');
process.send('From Worker');
} |
Yes, i saw #37782 after. My Solution:import cluster from 'cluster';
if (cluster.isMaster) {
const worker = cluster.fork();
worker.on('message', message => {
if (message === 'getPayload') {
worker.send({ payload: 'something' });
}
});
} else {
process.on('message', ({ payload }) => {
console.log(payload);
});
process.send('getPayload');
} |
Dupe of #37782 |
Just to clarify - when I said modules what I meant was that ESM modules (as in |
Hi there.
I have a problem:
The Worker doesn't react on "message" event from the Primary with: type: "module" enabled in package.json
But the Primary is react on "message" event from the worker
Without type: "module" both works.
win10 x64 (node v14.17.5 and v16.7.0)
Guys, any fixes? Thanks.
Example:
Doesn't work ONLY with enabled node.js imports.
The text was updated successfully, but these errors were encountered: