Skip to content
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

debugger: bind to random port with --debug-port=0 #5025

Closed
wants to merge 1 commit into from

Conversation

bnoordhuis
Copy link
Member

Allow binding to a randomly assigned port number with --debug-port=0.

Refs: #4419

R=@indutny

@ChALkeR
Copy link
Member

ChALkeR commented Feb 1, 2016

This looks like a semver-minor to me.

src/node.cc Outdated
fprintf(stderr, "Starting debugger on port %d failed\n", debug_port);
fflush(stderr);
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this is necessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the opposite of necessary, the return statement isn't necessary. :-)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duh :D In my defense I was Reviewing in mobile.

@develar
Copy link

develar commented Feb 1, 2016

I think, new CLI parameter is not required. Old --debug and --debug-brk could be modified to use 0 as any free port.

Also, debuggers use --debug-brk (run and stop on first line — wait debugger connection). --debug used only to debug already running nodejs process.

update: it seems, issue title is not correct — new logic is applied to all 3 flags and not bound only to --debug-port.

@develar
Copy link

develar commented Feb 1, 2016

We (JetBrains) like this solution.

#4419 is too magic. Node is an engine. Strictly speaking, it should be handled on an application level.

Node feature --debug-brk=0 (this issue) means that JetBrains debugger (or node-inspector) must be modified also — IDE should run main node process with --debug-brk=0 (forked child will be also run with this parameter (default nodejs behaviour)).

But as result, tools like AVA will be debuggable without any efforts from tool author.

@bnoordhuis
Copy link
Member Author

I think, new CLI parameter is not required.

It's not new, it's an existing command line switch.

update: it seems, issue title is not correct — new logic is applied to all 3 flags and not bound only to --debug-port.

Correct, but that doesn't fit in 50 columns. :-)

@bnoordhuis bnoordhuis added the semver-minor PRs that contain new features and should be released in the next minor version. label Feb 1, 2016
@thefourtheye
Copy link
Contributor

LGTM

1 similar comment
@jasnell
Copy link
Member

jasnell commented Feb 1, 2016

LGTM

@bnoordhuis
Copy link
Member Author

@@ -21,7 +21,7 @@ exports.start = function start() {
agent.listen(process._debugAPI.port, function() {
var addr = this.address();
process._rawDebug('Debugger listening on port %d', addr.port);
process._debugAPI.notifyListen();
process._debugAPI.notifyListen(addr.port);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so could it be possible that an error was passed to the callback from listen and this.address().port is undefined? If so, that would cause an abort in debug-agent right? Should we guard for that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

net.Server#listen() doesn't work that way, it emits an 'error' event when it can't bind to a port.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good point. I was incorrectly assuming that the connection listener would still be called, just with an error. Carry on

@evanlucas
Copy link
Contributor

LGTM

@bnoordhuis
Copy link
Member Author

New CI attempt: https://ci.nodejs.org/job/node-test-pull-request/1504/ - previous one was hit by a network outage, it looks like.

@cjihrig
Copy link
Contributor

cjihrig commented Feb 2, 2016

LGTM

@bnoordhuis
Copy link
Member Author

Grr, more RemotingSystemExceptions and IOExceptions. One more try: https://ci.nodejs.org/job/node-test-pull-request/1506/

@bnoordhuis
Copy link
Member Author

A seemingly genuine failure on one of the Windows buildbots:

not ok 42 test-debug-port-zero.js
# 
# assert.js:89
#   throw new assert.AssertionError({
#   ^
# AssertionError: 3221226505 === 0
#     at ChildProcess.proc.on.common.mustCall (c:\workspace\node-test-binary-windows\RUN_SUBSET\1\VS_VERSION\vs2015\label\win10\test\parallel\test-debug-port-zero.js:10:10)
#     at ChildProcess.<anonymous> (c:\workspace\node-test-binary-windows\RUN_SUBSET\1\VS_VERSION\vs2015\label\win10\test\common.js:395:15)
#     at emitTwo (events.js:101:13)
#     at ChildProcess.emit (events.js:186:7)
#     at Process.ChildProcess._handle.onexit (internal/child_process.js:200:12)

https://ci.nodejs.org/job/node-test-binary-windows/922/RUN_SUBSET=1,VS_VERSION=vs2015,label=win10/console

Exit code 3221226505 is C0000409 is "Stack buffer overflow."

@bnoordhuis
Copy link
Member Author

I suspect the failure is unrelated and only exposed, not caused, by this change. Added another test to validate that. CI: https://ci.nodejs.org/job/node-test-pull-request/1543/

EDIT: To be clear, the second commit is not supposed to land as-is.

@bnoordhuis
Copy link
Member Author

Interestingly, the new test passes on all platforms. The old test passes too except on win10+vs2015; win2012r2+vs2015 and win20082r2+vs2013 are fine. I'm inclined to scrap the previous test and go with the new one.

@3y3
Copy link

3y3 commented Feb 5, 2016

How we can receive information about debug port of each child from cluster master?
How we can start all cluster on random ports?

@bnoordhuis
Copy link
Member Author

Can I get one more round of review? I worked over the test and added two more. The gist of the PR hasn't changed but I had to add some machinery to make testing reliable.

CI: https://ci.nodejs.org/job/node-test-pull-request/1563/

@bnoordhuis
Copy link
Member Author

@3y3 Take a look at the cluster test I just added. If you start the master with --debug=0 or --debug-port=0, it gets propagated to the workers. If you want to know the port a worker is listening on, either parse its stderr or ask it for its process.debugPort property (that's what the test does.)

worker.send('debugPort');
}
process.on('exit', () => {
assert(ports.every((port) => port > 0));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe port > 1024?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that would be more correct.

@jasnell
Copy link
Member

jasnell commented Mar 22, 2016

@bnoordhuis ... what's the status on this?

Copy link
Member Author

@bnoordhuis bnoordhuis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, @sam-github PTAL. CI: https://ci.nodejs.org/job/node-test-pull-request/8332/

Question: should the call to env->inspector_agent()->Start() call debug_options.set_port()?

@sam-github
Copy link
Contributor

I think the options should mirror what was set on the CLI (or the default), IMO., anything else is more confusing. When the listener is stopped and started the port will change.

@bnoordhuis
Copy link
Member Author

@sam-github Can you (re)review? It's not clear to me if you were asking me to change anything.

@sam-github
Copy link
Contributor

Ben:

Question: should the call to env->inspector_agent()->Start() call debug_options.set_port()?

Sam:
Answer: I think the options should mirror what was set on the CLI (or the default), IMO., anything else is more confusing. When the listener is stopped and started the port will change. So --> No, to not call set_port() with the actual ephemeral port

Copy link
Contributor

@cjihrig cjihrig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still LGTM

@bnoordhuis bnoordhuis added inspector Issues and PRs related to the V8 inspector protocol and removed stalled Issues and PRs that are stalled. debugger labels May 30, 2017
@bnoordhuis bnoordhuis closed this May 30, 2017
bnoordhuis added a commit to bnoordhuis/io.js that referenced this pull request May 30, 2017
Allow binding to a randomly assigned port number with `--inspect=0`
or `--inspect-brk=0`.

PR-URL: nodejs#5025
Refs: nodejs#4419
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: Sam Roberts <[email protected]>
@bjrmatos
Copy link

sorry to comment on a closed issue, just one question, is this change present in latest 8.0? i'm trying to use it but it fails with Debug port must be in range 1024 to 65535.

@bnoordhuis
Copy link
Member Author

I just replied to nodejs/help#649 but to answer your question, I expect it will be released in v8.1.0.

@bjrmatos
Copy link

thnks!

jasnell pushed a commit that referenced this pull request Jun 5, 2017
Allow binding to a randomly assigned port number with `--inspect=0`
or `--inspect-brk=0`.

PR-URL: #5025
Refs: #4419
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: Sam Roberts <[email protected]>
@jasnell jasnell mentioned this pull request Jun 5, 2017
jasnell added a commit to jasnell/node that referenced this pull request Jun 7, 2017
* **Async Hooks**
  * When one `Promise` leads to the creation of a new `Promise`, the parent
    `Promise` will be identified as the trigger
    [[`135f4e6643`](nodejs@135f4e6643)]
    [nodejs#13367](nodejs#13367).
* **Dependencies**
  * libuv has been updated to 1.12.0
    [[`968596ec77`](nodejs@968596ec77)]
    [nodejs#13306](nodejs#13306).
  * npm has been updated to 5.0.3
    [[`ffa7debd7a`](nodejs@ffa7debd7a)]
    [nodejs#13487](nodejs#13487).
* **File system**
  * The `fs.exists()` function now works correctly with `util.promisify()`
    [[`6e0eccd7a1`](nodejs@6e0eccd7a1)]
    [nodejs#13316](nodejs#13316).
  * fs.Stats times are now also available as numbers
    [[`c756efb25a`](nodejs@c756efb25a)]
    [nodejs#13173](nodejs#13173).
* **Inspector**
  * It is now possible to bind to a random port using `--inspect=0`
    [[`cc6ec2fb27`](nodejs@cc6ec2fb27)]
    [nodejs#5025](nodejs#5025).
* **Zlib**
  * A regression in the Zlib module that made it impossible to properly
    subclasses `zlib.Deflate` and other Zlib classes has been fixed.
    [[`6aeb555cc4`](nodejs@6aeb555cc4)]
    [nodejs#13374](nodejs#13374).
rvagg pushed a commit that referenced this pull request Jun 8, 2017
* **Async Hooks**
  * When one `Promise` leads to the creation of a new `Promise`, the parent
    `Promise` will be identified as the trigger
    [[`135f4e6643`](135f4e6643)]
    [#13367](#13367).
* **Dependencies**
  * libuv has been updated to 1.12.0
    [[`968596ec77`](968596ec77)]
    [#13306](#13306).
  * npm has been updated to 5.0.3
    [[`ffa7debd7a`](ffa7debd7a)]
    [#13487](#13487).
* **File system**
  * The `fs.exists()` function now works correctly with `util.promisify()`
    [[`6e0eccd7a1`](6e0eccd7a1)]
    [#13316](#13316).
  * fs.Stats times are now also available as numbers
    [[`c756efb25a`](c756efb25a)]
    [#13173](#13173).
* **Inspector**
  * It is now possible to bind to a random port using `--inspect=0`
    [[`cc6ec2fb27`](cc6ec2fb27)]
    [#5025](#5025).
* **Zlib**
  * A regression in the Zlib module that made it impossible to properly
    subclasses `zlib.Deflate` and other Zlib classes has been fixed.
    [[`6aeb555cc4`](6aeb555cc4)]
    [#13374](#13374).
rvagg pushed a commit that referenced this pull request Jun 8, 2017
* **Async Hooks**
  * When one `Promise` leads to the creation of a new `Promise`, the parent
    `Promise` will be identified as the trigger
    [[`135f4e6643`](135f4e6643)]
    [#13367](#13367).
* **Dependencies**
  * libuv has been updated to 1.12.0
    [[`968596ec77`](968596ec77)]
    [#13306](#13306).
  * npm has been updated to 5.0.3
    [[`ffa7debd7a`](ffa7debd7a)]
    [#13487](#13487).
* **File system**
  * The `fs.exists()` function now works correctly with `util.promisify()`
    [[`6e0eccd7a1`](6e0eccd7a1)]
    [#13316](#13316).
  * fs.Stats times are now also available as numbers
    [[`c756efb25a`](c756efb25a)]
    [#13173](#13173).
* **Inspector**
  * It is now possible to bind to a random port using `--inspect=0`
    [[`cc6ec2fb27`](cc6ec2fb27)]
    [#5025](#5025).
* **Zlib**
  * A regression in the Zlib module that made it impossible to properly
    subclasses `zlib.Deflate` and other Zlib classes has been fixed.
    [[`6aeb555cc4`](6aeb555cc4)]
    [#13374](#13374).
@gibfahn
Copy link
Member

gibfahn commented Jan 15, 2018

Release team decided not to backport to v6.x, as the inspector is substantially different in that version, let us know if you think it should be backported.

@james-rabbit
Copy link

Is this feature documented somewhere? I wasn't able to find much information about it.
Is there any chance of multiple forked children trying to use the same port for debugging?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
inspector Issues and PRs related to the V8 inspector protocol semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.