From c4a61b3ee55988b59888e96fae63dba60662bb98 Mon Sep 17 00:00:00 2001 From: Sam Roberts Date: Tue, 6 Jun 2017 11:21:22 -0700 Subject: [PATCH] src: check whether inspector is doing io Inspector start means that it exists, but doesn't mean it is listening on a port, that only happens if it is doing I/O (i.e. has an io object). PR-URL: https://github.com/nodejs/node/pull/13504 Fixes: https://github.com/nodejs/node/issues/13499 Reviewed-By: Refael Ackermann Reviewed-By: Colin Ihrig --- src/node.cc | 4 ++-- test/inspector/test-inspector-port-zero.js | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/node.cc b/src/node.cc index 68f134ffe9..bbce10220f 100644 --- a/src/node.cc +++ b/src/node.cc @@ -3059,8 +3059,8 @@ static void DebugPortGetter(Local property, #if HAVE_INSPECTOR if (port == 0) { Environment* env = Environment::GetCurrent(info); - if (env->inspector_agent()->IsStarted()) - port = env->inspector_agent()->io()->port(); + if (auto io = env->inspector_agent()->io()) + port = io->port(); } #endif // HAVE_INSPECTOR info.GetReturnValue().Set(port); diff --git a/test/inspector/test-inspector-port-zero.js b/test/inspector/test-inspector-port-zero.js index 0776c82265..a3eb08e5fb 100644 --- a/test/inspector/test-inspector-port-zero.js +++ b/test/inspector/test-inspector-port-zero.js @@ -7,7 +7,7 @@ const assert = require('assert'); const { URL } = require('url'); const { spawn } = require('child_process'); -function test(arg) { +function test(arg, port = '') { const args = [arg, '-p', 'process.debugPort']; const proc = spawn(process.execPath, args); proc.stdout.setEncoding('utf8'); @@ -18,7 +18,6 @@ function test(arg) { proc.stderr.on('data', (data) => stderr += data); proc.stdout.on('close', assert.ifError); proc.stderr.on('close', assert.ifError); - let port = ''; proc.stderr.on('data', () => { if (!stderr.includes('\n')) return; assert(/Debugger listening on (.+)/.test(stderr)); @@ -46,3 +45,9 @@ test('--inspect=localhost:0'); test('--inspect-brk=0'); test('--inspect-brk=127.0.0.1:0'); test('--inspect-brk=localhost:0'); + +// In these cases, the inspector doesn't listen, so an ephemeral port is not +// allocated and the expected value of `process.debugPort` is `0`. +test('--inspect-port=0', '0'); +test('--inspect-port=127.0.0.1:0', '0'); +test('--inspect-port=localhost:0', '0');