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');