Webstorm debuggin forked node.js process

I'm launching forked node.js processes recursively and I can't seem to be able to debug them.

What I did:

1. Launch forked process:


var ar = '--debug-brk=' + (first ? 5858 : Math.floor(Math.random() * (65000 - 20000) + 20000));
var child = fork(__dirname + '/calcs',
[],
{
execArgv: [ar]
});
child.on('message', function(message) {
if (message.value === 'done') {
callback(null, message.x);
}
});
child.send({
x: x
});
first = false;

 

 

the var first is just to be able to remote debug one of the forked process.

I run the code that calls to this code (express server with route that calls it)

and then I run remote node.js debug session with port 5858 and go to the specified route that activated the forking.

The break point I set in the process is not hit.

 

What I though of is that the process is finished before getting the 'message event', here's the forked process code:

console.log('here1');
process.on('message', function(x) {
var _ = require('underscore');
console.log('here2');
// calcs
process.send({ value: 'done', x: x});
});

it prints the here1 to the console but I thought maybe it's ending before getting the event.

0
8 comments

Why are you using Remote Node.js run configuration to debug forked processes? Do they run remotely? for now the only solution for debugging child processes remotely is creating separate remote run configuration for each of forked processes, specifying its own port (configuration for master - 5858, configuration for child1 - child1 debug port, etc.). See https://youtrack.jetbrains.com/issue/WEB-16891

0
Avatar
Permanently deleted user

Because I want to debug the main application while debugging the forked processes at the same time. How could I achieve it otherwise ? When I checked if just debugging the main process and placing break points on the remote process code would hit, it wouldn't, so I've read that I need to remote debug them and make multiple debugging sessions for each forked process.

0

You need using the Node.js run configuration to debug forked processes - breakpoints in child app code, as well as in main application, should be hit. Note that since 2016.2 no separate tabs are opened for child processes on debugging; instead, there is a threads dropdown in Frames tab that allows switching between threads.

0
Avatar
Permanently deleted user

So for a breakpoint on the forked process to hit, I need to switch to the correct thread handle in the thread dropdown ?

0

No, threads should be switched automatically once breakpoints is hit. Normally you don't need doing anything special to to get them hit

0
Avatar
Permanently deleted user

I'm trying to debug a child process using Intellij Ulitimate 2017.1 Node version 8.1. When I fork the process as described in a number of your responses I discovered that the only one that works is if I set the fork option execArgv: ["--inspect=<different port>". What I see is the following. Here I set the different port to 50000:

Debugger listening on port 53266.
Debugger attached.
Debugger listening on port 50000.
Warning: This is an experimental feature and could change at any time.
To start debugging, open the following URL in Chrome:
chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:50000/333892d3-053c-4934-8a79-4635b9027330
Debugger attached.

Looks as though the debugger is trumped because if I set a breakpoint in both it gets confused. I can only debug one at a time. So how do you debug a forked node process? Note, I have tried not setting anything, --debug-brk=<port>, --debug=<port>

0
Avatar
Permanently deleted user

I'm trying to debug a child process using Intellij Ulitimate 2017.1 Node version 8.1. When I fork the process as described in a number of your responses I discovered that the only one that works is if I set the fork option execArgv: ["--inspect=<different port>". What I see is the following. Here I set the different port to 50000:

Debugger listening on port 53266.
Debugger attached.
Debugger listening on port 50000.
Warning: This is an experimental feature and could change at any time.
To start debugging, open the following URL in Chrome:
chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:50000/333892d3-053c-4934-8a79-4635b9027330
Debugger attached.

Looks as though the debugger is trumped because if I set a breakpoint in both it gets confused. I can only debug one at a time. So how do you debug a forked node process? Note, I have tried not setting anything, --debug-brk=<port>, --debug=<port>

0

You need using `--inspect-brk` for Node 8. But this doesn't work in 2017.1 (https://youtrack.jetbrains.com/issue/WEB-27312), issue is fixed in 2017.2

0

Please sign in to leave a comment.