I have landed into a weird problem when debugging my node.js application i.e. when running the node.js application using either of the flags i.e. 'debug' or '--debug-brk=portno'.
Note: The problem mentioned below would never happen when running the application in normal mode.
The problem seems to with the process's message event or with cluster's message send operation.
Below is the complete executable code:
const cluster = require('cluster');
const process = require('process');
if (cluster.isMaster) {
console.log('@Master :', process.pid);
[1, 2].forEach(() => {
cluster.fork();
});
} else {
process.on('message', (msg) => {
console.log('## Process.on -> ', msg);
});
}
cluster.on('online', (worker) => {
console.log('Worker online: ', worker.process.pid);
worker.send('Hey Hi from: ' + worker.process.pid)
});
Based on the above code one would think that the output similar to:
@Master : 17648
Worker online: 17552
Worker online: 8300
## Process.on -> Hey Hi from: 17552
## Process.on -> Hey Hi from: 8300
should be shown. But, instead the line
## Process.on -> Hey Hi from: 17552
## Process.on -> Hey Hi from: 8300
is never outputted in the console.
Here is the source file debugClusterTest.zip
If I'm doing something wrong in this test code - please let me know.
The problem might be that the worker processes are stopped at breakpoints.
@cjihrig If you are talking about custom breakpoints then for the above code even if there is none, we will see the same behaviour. But, I guess you are talking about the start breakpoint that is automatically hit when the debugger is attached.
@gauravmahto Correct. --debug-brk is passed to the workers and suspends them on start-up - and that's the intended behavior, otherwise you wouldn't be able to attach a debugger.
I'm going to close this as "working as intended." If that's incorrect, we can reopen.
@gauravmahto what you could do is use NODE_OPTIONS to enable the debugger, and then remove the option from the env before you fork.
NODE_OPTIONS=--inspect-brk node
...
let env = process.env;
delete env.NODE_OPTIONS
cluster.fork(env);
...
Unfortunately that only works in Node 8, hasn't been backported to Node 6 yet (see https://github.com/nodejs/node/pull/12677, but it won't land until the next Node 6 semver-minor release).
@gibfahn Thanks for the information. But unfortunately, I won't be able to use this method as I 'm on the latest LTS version of node.js and can't update to v8.
I'll wait for the above-mentioned technique to be backported to v6.