Cannot use 'autoAttachChildProcesses' to debug forked processes - node.js

I cannot debug forked node processes.
The forked process launches and behaves as expected; however, the debugger does not pause on breakpoints of the forked code.
Hovering over the breakpoint shows Unverified breakpoint tooltip. In addition, the Debug activity bar shows Breakpoint set but not yet bound:
I have 2 javascript files. The first forks the other:
index.js
const child_process = require("child_process");
console.log(process.argv[0]);
process.execArgv.push('--debug=' + (40895));
child_process.fork('./m1.js');
m1.js
console.log(`from fork: ${process.argv[0]}`);
launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "run.vscode",
"program": "${workspaceFolder}/index.js",
"autoAttachChildProcesses": true
}
]
}
I'm using node v10.16.0 and the issue is reproducible in vscode 1.36.0-insider and in 1.34.0; in Windows 10 or in WSL using the Remote - WSL extension.
Is the autoAttachChildProcesses property even supported for child_process.fork() or only for cluster.fork()?

I made it work by passing the --inspect-brk flag through the execArgv option of the fork() method:
child_process.fork('./m1.js', [], {execArgv:['--inspect-brk=40895']});
See more details in a corresponding issue I opened on GitHub.

Related

'autoAttachChildProcesses' option not doing anything

I have a javascript program that launches a script inside it with the following command:
node --inspect file.js.
When it happens, I would like VSCode to auto attach debugger to the child process. This is my config in launch.json:
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Start with Yarn",
"autoAttachChildProcesses": true,
...
}
auto attach is set to 'SMART'.
So I launch the program with the configuration, wait for the script to get called inside the program and... VSCode does not automatically attach a debugger to it. Why is that?

Visual Studio Code debugger attaches but does not stop node js process

I have a Node js application (running on Windows) which occasionally "hangs" - it gives no sign of progress. From Visual Studio Code, I attach the debugger by selecting the process ID. Evidence of successful attachment is given both from within VSC and standard out (console):
"Debugger listening on ws://x.y.z.1:9229/242f50ce-a43c-48dc-9926-23520ed10ed3
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached."
However, the process continues to run and clicking on the "pause" symbol in the debugger control bar un-ghosts momentarily but does not change to the "continue" symbol (other control symbols on the bar remain ghosted).
Launch configuration:
{
"launch": {
"configurations": [
{
"name": "Attach by Process ID",
"processId": "${command:PickProcess}",
"request": "attach",
//"skipFiles": [
// "<node_internals>/**"
//],
"type": "node"
},
{
"name": "Launch Program",
"program": "${workspaceFolder}/index.js",
"request": "launch",
"skipFiles": [
"<node_internals>/**"
],
"type": "node", "args": ["set", "31100-1", "subsets" ]
}
]
},
"workbench.colorTheme": "Default High Contrast"
}
On occasion when I attach and then detach ("Stop Debugging") the process seems to continue and behave normally (no longer hung), but I am not able to discover where and/or why it is hanging.
I should add that while this Node js program runs from both a windows power shell and a Cygwin tcsh, it is when running from the Cygwin that I am seeing this behavior (insufficient testing from power shell to say whether or not this ever occurs)
I can understand that the source might not be visible from VSC or that the callstack would be abbreviated/incomplete, but I would expect the process to stop. Perhaps it has and I am misinterpreting the VSC presentation.
Any discussion or suggestions are welcomed. I am both a Node js and VSC novice.

VSCode NodeJs: Debugger not stopping at breakpoint (WSL2/Ubuntu18)

Using WSL2/Ubuntu18 I've not been able to make the VSCode NodeJs Debugger to stop on the breakpoints of any NodeJs app. When I start the debugger, it runs (I can see the output on the integrated terminal) but breakpoints are simply ignored.
The simple.js file, with a breakpoint on line 3:
The launch.json is set to:
{
"version": "0.2.0",
"configurations": [
{
"name": "NodeJs: Launch Program",
"program": "${file}",
"request": "launch",
"skipFiles": [
"<node_internals>/**"
],
"type": "pwa-node",
"console": "integratedTerminal"
}
]
}
When I press F5 or click on the "Start Debugging" button on VS Code, the app runs and following appears on the integrated Terminal:
/usr/bin/env 'NODE_OPTIONS=--require /home/myuser/.vscode-server/bin/ea3859d4ba2f3e577a159bc91e3074c5d85c0523/extensions/ms-vscode.js-debug/src/bootloader.bundle.js --inspect-publish-uid=http' 'VSCODE_INSPECTOR_OPTIONS={"inspectorIpc":"/tmp/node-cdp.19338-1.sock","deferredMode":false,"waitForDebugger":"","execPath":"/home/myuser/.nvm/versions/node/v14.15.1/bin/node","onlyEntrypoint":false,"autoAttachMode":"always","fileCallback":"/tmp/node-debug-callback-ff32d873905abafa"}' /home/myuser/.nvm/versions/node/v14.15.1/bin/node ./simple.js
Debugger attached.
0
1
2
3
4
Waiting for the debugger to disconnect...
I've already upgraded from Node10 to Node14, but the problem persists.
On another computer using WSL1, and using the same launch.json the debugger stops at the given breakpoints. Do I need to set something additionally on WSL2? For the record, this is what appears on the integrated terminal on the WSL1 computer before it stops at line 3:
/usr/bin/env 'NODE_OPTIONS=--require /home/myuser/.vscode-server/bin/ea3859d4ba2f3e577a159bc91e3074c5d85c0523/extensions/ms-vscode.js-debug/src/bootloader.bundle.js --inspect-publish-uid=http' 'VSCODE_INSPECTOR_OPTIONS={"inspectorIpc":"/tmp/node-cdp.787-3.sock","deferredMode":false,"waitForDebugger":"","execPath":"/home/myuser/.nvm/versions/node/v14.15.1/bin/node","onlyEntrypoint":false,"autoAttachMode":"always","fileCallback":"/tmp/node-debug-callback-b901b6d6e3e9799b"}' /home/myuser/.nvm/versions/node/v14.15.1/bin/node ./simple.js
Debugger attached.
<Breakpoint hit and stop...>
Additional info, debugging Python3 files work correctly on both machines.
Both computers have the same VS Code Version installed.
Update:
You can follow the issue on GitHub: https://github.com/microsoft/vscode/issues/113283
The problem is that the NodeJs App is being run from a symlinked address - so the debugger can not handle it.
Answer from one of the developers of VSCode/NodeJS on github:
It looks like you have your script symlinked into /bin/nhosko/simple.js, but its actual location is /mnt/c/Users//bin-nhosko/simple.js. In this case you need to specify some flags so that Node will report the linked location that vscode sees and told the debugger about: https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_can-i-debug-if-im-using-symlinks. I want to make the debugger smart enough to fix this automatically in microsoft/vscode-js-debug#776.
https://github.com/microsoft/vscode/issues/113283#issuecomment-750371948

How to debug "built" production NodeJS

I am using Visual Studio code to debug a node application in production environment
The Node process runs inside docker,
I port-forwarded and signaled USR1 to enable attaching debugger from VS code to that node process
My VS Code configuration is like this
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Debug: service",
"sourceMaps": true,
"smartStep": true,
"remoteRoot": "/src/",
"localRoot": "/home/my-username/work/orders/src/",
"protocol": "inspector",
"port": 9229,
"restart": true,
"address": "0.0.0.0",
"skipFiles": [
"<node_internals>/**",
"<node_modules>/**"
]
}
]
}
From VS code, I can hook into the application and the application can break on exception
However there is no source-mapping which cause all my breakpoint in my source-code to be "unbound breakpoint"
The loaded script list in VS code show that
The VS code debugger is able to see the node_modules and the built version of my source code inside dist. One other notable point is that the source code that is used to build the /dist is also available directly in the production server, in the upper folder.
How can I debug the built production process, using my unbuilt source code in this case?
I added Chrome behaviour as separate question
NodeJs: Chrome inspector can map source but unable to debug on original source
I don't know whether it will be helpful to you or not. But I think you have to use node-inspector. It can be used from any browser supporting WebSocket. It is really good.
Cool stuff
Node Inspector uses WebSockets, so no polling for breaks.
Remote debugging and debugging remote machine.
Live edit of running code, optionally persisting changes back to the file-system.
Set breakpoints in files that are not loaded into V8 yet - useful for debugging module loading/initialization.
Embeddable in other applications - see Embedding HOWTO for more details.
Node already ships with an integrated debugger. However, it doesn’t have a GUI, so you need to use the command line version.
You can launch this debugger using node debug.
$node debug test.js
< Debugger listening on port 5858
debug> . ok
break in test.js:1
> 1 var a= 5;
2 a = a*a
3 a += 2;
debug>
It shows you where it’s paused and then lets you control execution with commands like next and cont.
debug> next
break in test.js:2
1 var a= 5;
> 2 a = a*a
3 a += 2;
4
The repl and watch commands allow you to see the values of local variables.

VS Code Node.js debugger immediately exits if nothing anywhere in the code "forces" the main event loop to be active

I have an issue with VS Code Node.js built-in debugger: it exits immediately (without executing anything) unless i add some kind of delay (such as setTimeout) anywhere in the code.
The basic code being tested:
var x = 5;
console.log('test');
Debug console output:
/usr/bin/node /home/dev/Desktop/Test/test.js
Debugger listening on ws://127.0.0.1:46297/de11d65b-015a-45eb-9c0b-6a0501513777
For help see https://nodejs.org/en/docs/inspector
Process exited with code 0
Modified code:
var x = 5;
console.log('test')
setTimeout(function() {}, 1000);
Gives this debug console output:
/usr/bin/node /home/dev/Desktop/Test/test.js
Debugger listening on ws://127.0.0.1:43463/51555bf6-94f4-4fcc-a766-442e0adc6fb3
For help see https://nodejs.org/en/docs/inspector
Debugger attached.
test
Waiting for the debugger to disconnect...
Process exited with code 0
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/test.js"
}
]
}
No breakpoints fire in either situation, unless i add them to some code inside the timeout callback. What's going on here? I've tried to close and reopen the workspace folder, tried re-creating the launch.json. I also tried to follow some VS Code Node.js debugging tutorials but everything looks to be in order. Launching the script using "node" from the terminal directly works as expected in both cases.

Resources