Debugging node child processes

Hi
I have a nodejs server .
From the server I Fork a new child process (node also that points to a js file.

If I run my server in debug mode console.log and breakpoints do not work in the forked process.
If I run (not in debug) then the console.log does work on the forked process.

I want to debug my forked process and put breakpoints  how can I do it.
Thanks.

0


Still waiting for a answer
Thanks.

0

Hi,

Looks like you need to set "execArgv" parameter to empty array. When a node process is debugged it's started with --debug-brk=<port> argument. So all forked node processes inherit this argument, but it isn't desired behavior, because the port is already bound by the parent node process.

Here is an example that shows how that could be accomplished:

main.js

var child_process = require('child_process')   , path = require('path'); child_process.fork(   path.join(__dirname, 'child.js'),   [],   {     execArgv: []   } ); setInterval(function () {   console.log('Hello from parent'); }, 2000);


child.js

console.log('Child process started'); setInterval(function () {   console.log("Hello from child process"); }, 1000);


Hope that will help.

Sergey

0

Hi Sergey
Thanks for your answer.
your suggestion only fixes the console output. I now can see the console output of the child process in debug mode however breakpoints in the child process still do not work.
Thanks.

0

Forgot about breakpoints. Sorry.

If you'd like to debug child process, you need to pass "--debug-brk=<any free port>" as "execArgv" parameter to it.
With "execArgv: []" child process is obviously started in a run mode.

1

Hi Sergey
I tried it and the breakpoint on the child node doesnt get hit. (BTW this also causes the console.log not to work again)
see below console output , I set the child node to listen on port 8083 and I can see that it does from the console , but the breakpoint does get called. How exaclty is it supposed to work when I want to debug both node server and the child process that it forks. ?

debugger listening on port 8083


See all the console output . the last line is the child process being called.

"C:\Program Files\nodejs\node.exe" --debug-brk=56650 serverController.js
debugger listening on port 56650
connect.multipart() will be removed in connect 3.0
Express server listening on port 3000
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
connect.limit() will be removed in connect 3.0
connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
connect.limit() will be removed in connect 3.0
connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
connect.limit() will be removed in connect 3.0
connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
connect.limit() will be removed in connect 3.0
connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
connect.limit() will be removed in connect 3.0
connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
connect.limit() will be removed in connect 3.0
connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
connect.limit() will be removed in connect 3.0
debugger listening on port 8083

0

It stopped :-) thanks sergey . let me try and just clarify to see what I understand

1. create a Node.js remote debug
2. run this configuration
3. run the parent node js (BUT NOT IN DEBUG )

the breakpoint stops in child (but not breakpoints in the parent obviously)

so my questions now are

1. Is there any way to debug both parent and child in the same session ?
2. is this the only way , it looks like some kind of workaround   var childProc= fork('./public/LG/lg.js',{execArgv: ['--debug-brk=5858']});  ..... When I finish dev I must remove the exectArgv no ? . This doesnt seem like code to keep in the production . I couldnt find the --debug-brk in node js documentation)  ?
3. I see that once I do this , there are no console outputs from the child process ?



Thanks.

0

BTW Sergey
I just noticed that the debugging isnt smooth , it stopped on a breakpoint but then when doing F8 it suddenly went crazy and entered other modules.

0

1. Is there any way to debug both parent and child in the same session ?


Yes. Just make sure "--debug-brk=<free port>" is passed to a forked child node process.
Also make sure you're using WebStorm 7.0.3. (WebStorm 7.0.2 doesn't debug child node processes correctly).
Click "Debug" button (that starts parent node process debugging) and it should work in the following way:
* A console associated with the parent node process will shown up.
* As soon as a child node process is forked, there will be automatically created "Node.js Remote Debug" run configuration for the child node process. And it will be shown near parent's console.

2. is this the only way , it looks like some kind of workaround   var childProc= fork('./public/LG/lg.js',{execArgv: ['--debug-brk=5858']});  ..... When I finish dev I must remove the exectArgv no ? . This doesnt seem like code to keep in the production . I couldnt find the --debug-brk in node js documentation)  ?


Oh, it could be simplified in the following way (forgot to mention it in the first place):

var childProc= fork('./public/LG/lg.js',{execArgv: ['--debug-brk']})

In that case node automatically finds a free port.

But yes, you need to keep some logic whether a child process should start in a debug mode or not.
For example, possible logic could be like this: start a child process in a debug mode if a parent process has been started in a debug mode.
To verify that a node process is started in a debug mode, you can analyze "process.argv" array.
FYI, "cluster" node core modules works in this way.

Can't find any links about "--debug-brk" in node documentation either.

3. I see that once I do this , there are no console outputs from the child process ?

Is child process actually running?
If a node process is started with "--debug-brk", it's suspended on the first line.
It continues running after starting "Node.js Remote Debug" run configuration with debug port of this process.
So make sure you start "Node.js Remote Debug" run configuration.


You can take a sample code from my first comment, replace

execArgv: []

with

execArgv: ["--debug-brk"]

and hit "Debug" main.js.
After that you'll see output of both processes in console associated with the parent process and breakpoints in child.js should work also.

Please let me know whether it's working for you.

0

Hi Sergey
I see what was missing.
I didnt realize that the child process was suspended and that I must run a remote debug configuration after the child was created.

Its working now
BTW , I see that I can run the Child Process Remote Configuration BEFORE starting the main server , and then run the Main Server Configuration in Debug
(For some reason yesterday I though it didnt work)

BTW I am using IDEA 12.1.4.   Are there any known debugging issues ?

The only thing now is that the debugger doesnt work properly , It doesnt seem to be in sync with the code, it suddelny enters other modules although I am doing F8

Thanks for the great help.

0

Great!

BTW I am using IDEA 12.1.4.   Are there any known debugging issues ?

Well, a lot of debugging issues have been fixed since IDEA 12.1.4: http://youtrack.jetbrains.com/issues/WEB?q=%23Fixed+Subsystem%3A+Debugger+Fix+versions%3A+7* 
But I'm not sure whether your issues is fixed or not...
I'd recommend you to try the latest IDEA 13 release and reproduce the issue there.
Since no new IDEA 12.1 releases are planned, only issues that are reproducible with IDEA 13 will be addressed.
Thanks.

0

This article also helps me but I have one more question.
What is the difference btween --debug-brk and --debug.
With --debug-brk I have my breakpoints enabled. I am interested in if do I still need also --debug for some special cases if I passed --debug-brk?
Regards,
Wojciech

0

Option --debug-brk is basically the same as --debug, except that execution breaks on the first line of the entry point of the current application. Thus, usually, both --debug and --debug-brk are not passed.

0

Hi guys i hope you can help me with this debug stuff... I have been using webstorm from a while a in the project I was able to debug nice and clean with the child forks and so on.

But not, i not sure if is the version WebStorm 2016.3.2 Build #WS-163.9166.30, built on December 12, 2016, but now i not able to debug the childs.

 

ryan h said:

I see what was missing.
I didnt realize that the child process was suspended and that I must run a remote debug configuration after the child was created.

 

But for what I remember, I never need that, I only have to create a normal node configuration, them add the parameter --debug-brk=port and click the debug button. On the debug panel appears a number of tabs, as many as child forks, and i have to do run on each one to be able to debug. 

But with this version I don't see those tabs, so i not able to debug the project.

I read the https://www.jetbrains.com/help/webstorm/2016.3/running-and-debugging-node-js.html#node_multiprocess_debugging topic, and i realize that the frames don't have any dropdown at all.

Do I missing any thing? 

Thanks in advance

 

0

Hi Nuno,

What's your node version and OS? Can it be reproduced with node 6 and node 7? Possibly related issue https://youtrack.jetbrains.com/issue/WEB-24629

0

Hi Sergey Simonchik, yes, was related with that.

I have the same issues on two different machines:

Mac Os with node 7.1

Windows 10 with node 7.3

On the Mac Os already downgrade node to version 6.9.X and is working ok.

Will try on the Windows, if I don't say nothing is because is working ;)

 

Thanks guys :)

0

请先登录再写评论。