Skip to content

Node.js Cluster Example Fails under VS Code #3201

Closed

Description

@curtw

Environment:

  • VS Code Version 0.10.9
  • Mac OS 10.10.5
  • Node.js v4.3.1

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:

const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('exit', (worker, code, signal) => {
    console.log(`worker ${worker.process.pid} died`);
  });
} else {
  // Workers can share any TCP connection
  // In this case it is an HTTP server
  http.createServer((req, res) => {
    res.writeHead(200);
    res.end('hello world\n');
  }).listen(8000);
}

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

Activity

  1. weinand commented on Feb 21, 2016

    @weinand
    Contributor

    Curt Williams (@curtw) how does you launch config look like?

  2. curtw commented on Feb 21, 2016

    @curtw
    Author

    My launch.json file is unaltered from that which VS Code created by default:

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Launch",
                "type": "node",
                "request": "launch",
                "program": "${workspaceRoot}/app.js",
                "stopOnEntry": false,
                "args": [],
                "cwd": "${workspaceRoot}",
                "runtimeExecutable": null,
                "runtimeArgs": [
                    "--nolazy"
                ],
                "env": {
                    "NODE_ENV": "development"
                },
                "externalConsole": false,
                "sourceMaps": false,
                "outDir": null
            },
            {
                "name": "Attach",
                "type": "node",
                "request": "attach",
                "port": 5858,
                "sourceMaps": false,
                "outDir": null,
                "localRoot": "${workspaceRoot}",
                "remoteRoot": null
            }
        ]
    }
    
  3. Benzeliden commented on May 1, 2016

    @Benzeliden

    Reproduced for me in debug mode
    But this example works when launching without debugging (ctrl + F5\ cmd + F5)

  4. sunjunzero commented on May 20, 2016

    @sunjunzero

    vscode default use --debug-brk to enable the debug support which block the child process at beginning.

  5. enif-lee commented on Jun 10, 2016

    @enif-lee

    +1
    In my case, not works when using sticky-cluster module, but using ctrl+F5 normally works ..T_T

  6. added
    debug
    bug
    on Jun 10, 2016
  7. weinand commented on Jun 10, 2016

    @weinand
    Contributor

    Yes, the --debug-brk option is the problem. Currently there is no way to prevent VS Code from using this option, but there are two workarounds:

    1. change the argument by adding this statement as the first line to app.js:

      process.execArgv[0] = process.execArgv[0].replace('-brk', '');
      
    2. or launch node from a terminal and attach to it with the VS Code debugger.

      Run in terminal:

      node --debug app.js
      

      Then select the default 'attach' launch config and attach to it.

    The 2nd workaround is the preferred way if you actually want to debug any worker and not just the first process that is launched.
    For the example from above you could add the debug port to the hello world message:

    res.end(`hello world ${process.debugPort}\n`);
    

    and then you can attach to that port with VS Code.

  8. weinand commented on Jun 10, 2016

    @weinand
    Contributor

    BTW, in the latest VS Code Insider you can use the new process picker support to select the cluster worker that you want to debug. After starting the cluster app from a terminal:

    node app.js
    

    run the 'Attach to Process' launch config and select a process:

    2016-06-10_12-42-43

  9. added
    feature-request
    and removed
    bug
    on Jul 6, 2016
  10. sylvainlap commented on Jul 7, 2016

    @sylvainlap

    The 2nd workaround is the preferred way if you actually want to debug any worker and not just the first process that is launched.

    what could be the workaround if I want to debug all workers in the same time ?

  11. sylvainlap commented on Jul 8, 2016

    @sylvainlap

    Andre Weinand (@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) ?

  12. 23 remaining items

  13. jez9999 commented on Nov 17, 2017

    @jez9999

    Isidor Nikolic (@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.

  14. isidorn commented on Nov 17, 2017

    @isidorn

    The extension candidate label was already present in this issue, I simply reapplied it.
    I see the discussion seems lively so I can reopen the issue and Andre Weinand (@weinand) can decide on how to proceed

  15. weeco commented on Dec 7, 2017

    @weeco

    Andre Weinand (@weinand) Any update? What's the plan for this?

  16. weinand commented on Dec 7, 2017

    @weinand
    Contributor

    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.
    This could be an alternative way of supporting a node.js cluster (alternative to the new Inspector Protocol target domain mentioned above).

  17. modified the milestones: On Deck, on Dec 12, 2017
  18. weinand commented on Dec 12, 2017

    @weinand
    Contributor

    I followed my own suggestion: please see some progress in #40123

  19. weinand commented on Dec 21, 2017

    @weinand
    Contributor

    duplicate of #40123.

  20. added
    *duplicate
    and removed
    *extension-candidate
    on Dec 21, 2017
  21. locked and limited conversation to collaborators on Feb 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    *duplicatedebugfeature-request

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions