Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Support dynamic port assignment via --inspect-port=0 #153

Description

It's possible to start Node with node --inspect-port=0 that assignes a dynamic port to the inspector server.

When using it together with a dynamic attach config like:

        {
            "type": "node",
            "request": "attach",
            "name": "Node",
            "processId": "${command:PickProcess}",
            "protocol": "inspector",
            "trace": "verbose"
        }

We end up failing to attach, as we try on the default port

Discovering targets via http://127.0.0.1:9229/json
HTTP GET failed: Error: connect ECONNREFUSED 127.0.0.1:9229

but the Inspector server is running the dynamically assigned port

Find the server at: http://localhost:3001/
Debugger listening on ws://127.0.0.1:51514/64e041f8-0433-4d33-9d11-408359869e84
For help see https://nodejs.org/en/docs/inspector

Idea: When the given process has been selected, attach to the strout, parse the output to find the used port. use that for attach

Activity

  1. roblourens commented on Nov 16, 2017

    @roblourens
    Member

    Didn't even know about that feature, but this is easy to support. cc Andre Weinand (@weinand)

  2. auchenberg commented on Nov 17, 2017

    @auchenberg
    Author

    Me neither, learned about it in nodejs/node#16872 (comment)

  3. weinand commented on Nov 17, 2017

    @weinand
    Contributor

    Hmmm. Since the DA is attaching (and not launching), how can we read the stdout of the debuggee in order to detect the port?

    In addition, attaching by process ID is useful if node has been started without any debug/inspect arguments. So if we have to use node --inspect-port=0 upfront this defeats the purpose because then we could use node --inspect=12345 instead and would not have to use the process ID at all...

    But may be I'm not really understanding this feature. It's too late... :-)

  4. roblourens commented on Nov 17, 2017

    @roblourens
    Member

    I skimmed this and assumed the launch config had the port in it.

    If we have to detect the port from stdout then we would have to use some platform-specific method of getting the stdout of a process that isn't a child process. I have no idea how to do that on windows, I don't even know if it's possible. It also seems like there could be issues with node's debug output getting mixed in with output from the user code.

  5. roblourens commented on Nov 17, 2017

    @roblourens
    Member
  6. auchenberg commented on Nov 17, 2017

    @auchenberg
    Author

    Andre Weinand (@weinand) The scenario here is that --inspect-port=0 could have been passed to the process, and therefore our default SIGUSR1 logic doesn't work. This is relevant to the Node on Azure production debugging case, where there might be multiple Node processes running and dynamic port assignment is needed. See linked Node issue.

  7. weinand commented on Nov 17, 2017

    @weinand
    Contributor

    We already have code in node-debug that detects a port based on a process ID: https://github.com/Microsoft/vscode-node-debug/blob/424c7760fd197e29ad7a8bd20234d9dbbd534163/src/node/extension/protocolDetection.ts#L154

    And please note that the SIGUSR1 mechanism only works locally. So you cannot send signals to a remote process.

  8. june07 commented on May 3, 2019

    @june07

    Because this is somewhat related, I'm linking to the PR: #235

    Would like to get some additional feedback regarding this. Thanks roblourens so far for your assistance as well.

  9. connor4312 commented on Oct 8, 2019

    @connor4312
    Member

    roblourens following up on this and what Andre Weinand (@weinand) mentioned, it seems like (if we still want to do this), the approach would be:

    1. Adding a similar getOpenPortsForPidUnix, then top-level getInspectorPortForPid which grabs the process' ports
    2. If there's more than one, filtering for which one of those is the inspector by calling GET localhost:<port>/json (If the user is building a web server, they will have additional open ports.) This has the potential of causing side-effects on the target server but I'm unsure that there's a better way to identify the correct port.
    3. Calling getInspectorPortForPid in that event that we see --inspect-port=0 in analyseArguments

    Thoughts?

  10. locked and limited conversation to collaborators on Dec 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions