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

Worker message event triggers when worker process sends #26977

Closed
CodeOtter opened this issue Mar 29, 2019 · 4 comments
Closed

Worker message event triggers when worker process sends #26977

CodeOtter opened this issue Mar 29, 2019 · 4 comments
Labels
cluster Issues and PRs related to the cluster subsystem.

Comments

@CodeOtter
Copy link

CodeOtter commented Mar 29, 2019

Running Node v11.12.0 on Windows 7

Filename: IsClusterStillAlpha.js

const cluster = require('cluster')

if (cluster.isMaster) {
  // Master
  const worker = cluster.fork()

  worker.on('message', function (message) {
    console.log('worker got message:', message)
  })
} else if (cluster.isWorker) {
  // Worker
  process.send('sending worker to master')
}

Command to run the script: node IsClusterStillAlpha.js

Core assumption: Worker's can be assigned .on('message', ...) within the master process as per https://github.com/nodejs/node/blame/master/doc/api/cluster.md#L240.
Expected output: Nothing
Actual output: worker got message: sending worker to master

@ZYSzys ZYSzys added the cluster Issues and PRs related to the cluster subsystem. label Mar 29, 2019
@bnoordhuis
Copy link
Member

The sample works as expected and it's unclear to me why you expect it to not print anything. Can you elaborate?

@CodeOtter
Copy link
Author

CodeOtter commented Mar 29, 2019

@bnoordhuis Thanks for getting back to me.

The documentation alludes to process.send notifying the master process, but that the worker.on('message', ...) listener is executed WITHIN the worker process. Is the worker listener executed within the master process?

@bnoordhuis
Copy link
Member

Yes. I think you are confusing it with process.on('message', ...) - that executes in the worker process.

@cjihrig
Copy link
Contributor

cjihrig commented May 2, 2019

Closing as answered.

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

No branches or pull requests

4 participants