-
Notifications
You must be signed in to change notification settings - Fork 30k
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
Node.js Cluster Example Fails under VS Code #3201
Comments
@curtw how does you launch config look like? |
My launch.json file is unaltered from that which VS Code created by default:
|
Reproduced for me in debug mode |
vscode default use --debug-brk to enable the debug support which block the child process at beginning. |
+1 |
Yes, the
The 2nd workaround is the preferred way if you actually want to debug any worker and not just the first process that is launched.
and then you can attach to that port with VS Code. |
what could be the workaround if I want to debug all workers in the same time ? |
@weinand When I launch a node js process that is clustered, and try to 'Attach to Process', I only have the master process in the list. How do you manage to have the master process, and all the forks in your list (your screenshot) ? |
I was facing the same issue in my web app, what I did is the following: if (cluster.isMaster && process.env.NODE_ENV !== "development") app.listen(port, function () {
if(cluster.worker)
console.log("Worker %d running! App listening on port %s", cluster.worker.id, port);
else
console.log("App listening on port %s", port);
}); Because I only need this feature in production. 😄 |
Cheers sant123 thats good enough for me to investigate if vscode is good to test with :) |
FYI, starting with the October release VS Code supports multi session debugging. This means you can create multiple launch configurations that attach to different debug ports. But you would have to trigger these launch configs manually... A better way could be to write a small extension that uses a launch config as a template and starts multiple debug sessions out of it (and varies the debug port for each of them). Or this extension could use the 'process picker' code from here to find the process IDs of all node processes and then create debug sessions that attach to those processes. |
you cannot use hack :
with "type": "node2", |
Any updates on this issue @weinand ? |
Something you can try is pm2. From here I just forget the need to create clusters in my code, because this creates them for me. This is useful for development and production environments. At the end, you write your code as a single process but you can turn it to multiple process. |
we had write inspector-proxy to proxy the workers debugPort, so in VSCode we just need to attach to one port without worry abut increate debugPort. |
@sant123 We do use pm2 too, however that doesn't help you if one of your instances is launching a background task which is running as it's own process. That still doesn't allow you to debug the launched process with the VScode debugger. |
Why can't VS Code have the option to detect when a |
Other IDEs like Webstorm have indeed better support for debugging child processes. I hope the VSCode team is going to work on a better support too. |
Any suggestions how to detect the |
seems WebStorm will monitor the main process's child process tree, and auto attach them |
when debug with: {
"type": "node",
"request": "launch",
"name": "Egg Debug",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run",
"debug","--", "--workers=2"
],
"console": "integratedTerminal",
"restart": true,
"protocol": "auto",
"port": 9999
} the process tree is: ➜ showcase git:(master) ✗ pstree -s showcase
-+= 00001 root /sbin/launchd
\-+= 00806 tz /Applications/Visual Studio Code.app/Contents/MacOS/Electron -psn_0_122910
\-+- 01821 tz /Applications/Visual Studio Code.app/Contents/Frameworks/Code Helper.app/Contents/MacOS/Code Helper --type=renderer --js-flags=--nolazy --no-sandbox --primordial-pipe-token=835C6
\-+- 02161 tz /Applications/Visual Studio Code.app/Contents/Frameworks/Code Helper.app/Contents/MacOS/Code Helper /Applications/Visual Studio Code.app/Contents/Resources/app/out/bootstrap --
\-+= 02162 tz /bin/zsh -l
\-+= 02258 tz npm
\-+- 02261 tz node /Users/tz/Workspaces/eggjs/test/showcase/node_modules/.bin/egg-bin debug --workers=2
\-+- 02269 tz /Users/tz/git/nvs/node/8.9.0/x64/bin/node --inspect /Users/tz/Workspaces/eggjs/test/showcase/node_modules/[email protected]@egg-bin/lib/start-cluster {"workers":1,"baseDir
|--- 02273 tz /Users/tz/git/nvs/node/8.9.0/x64/bin/node --inspect --debug-port=5800 /Users/tz/Workspaces/eggjs/test/showcase/node_modules/[email protected]@egg-cluster/lib/agent_
\--- 02274 tz /Users/tz/git/nvs/node/8.9.0/x64/bin/node --inspect --inspect-port=9230 /Users/tz/Workspaces/eggjs/test/showcase/node_modules/[email protected]@egg-cluster/lib/app_ so I guess WebStorm will do this:
|
Oh this looks like a perfect opportunity for an extension! |
yes, @weinand, I had try before, but got some problem: I don't know how to got the main process pid which vscode debugger started ( |
The Node community is currently working on a PR that will add support of debugging fork/child processes via the new Inspector Protocol |
This issue has been closed because it is not within the scope of the core product, but could be addressed by an extension. The VS Code Marketplace shows all existing extensions and you can get started writing your own extension in a few simple steps. See also our issue reporting guidelines. Happy Coding! |
@isidorn Huh? Why is this an extension candidate? Automatically attaching to the correct port for cluster child processes seems well within the remit of VS Code considering it comes with a debugger and multi-process debugging. |
The extension candidate label was already present in this issue, I simply reapplied it. |
@weinand Any update? What's the plan for this? |
For another project I wrote an extension that tracks all child processes of VS Code (which could include a node.js cluster) and allows to pick node.js processes for debugging based on ports or pids. |
I followed my own suggestion: please see some progress in #40123 |
duplicate of #40123. |
Environment:
Steps to Reproduce:
Create a new directory named cluster-test
Create a new file cluster-test\app.js with sample code from https://nodejs.org/dist/latest-v4.x/docs/api/cluster.html#cluster_cluster as follows:
Open cluster-test in VS Code
Switch to the Debug view and click the Start icon, or press F5
Select "Node.js" as the debug environment
Click Start again
Point you browser to http://localhost:8000/
Observe that no one is listening
Exit VS code, open a command prompt and invoke:
node cluster-test\app.js
Point you browser to http://localhost:8000/
Observe that a "hello world" response is received
Conclusion:
Basic node cluster example fails in VS Code
The text was updated successfully, but these errors were encountered: