Skip to content

Commit 42df2ba

Browse files
joyeecheungcodebytere
authored andcommitted
inspector: throw error when activating an already active inspector
When the user tries to activate the inspector that is already active on a different port and host, we previously just silently reset the port and host stored in the Environment without actually doing anything for that to be effective. After this patch, we throw an error telling the user to close the active inspector before invoking `inspector.open()` again. PR-URL: #33015 Fixes: #33012 Reviewed-By: Yongsheng Zhang <[email protected]> Reviewed-By: Eugene Ostroukhov <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 8457033 commit 42df2ba

File tree

5 files changed

+53
-4
lines changed

5 files changed

+53
-4
lines changed

doc/api/errors.md

+7
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,13 @@ time.
12241224
The `--input-type` flag was used to attempt to execute a file. This flag can
12251225
only be used with input via `--eval`, `--print` or `STDIN`.
12261226

1227+
<a id="ERR_INSPECTOR_ALREADY_ACTIVATED"></a>
1228+
### `ERR_INSPECTOR_ALREADY_ACTIVATED`
1229+
1230+
While using the `inspector` module, an attempt was made to activate the
1231+
inspector when it already started to listen on a port. Use `inspector.close()`
1232+
before activating it on a different address.
1233+
12271234
<a id="ERR_INSPECTOR_ALREADY_CONNECTED"></a>
12281235
### `ERR_INSPECTOR_ALREADY_CONNECTED`
12291236

lib/inspector.js

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
} = primordials;
99

1010
const {
11+
ERR_INSPECTOR_ALREADY_ACTIVATED,
1112
ERR_INSPECTOR_ALREADY_CONNECTED,
1213
ERR_INSPECTOR_CLOSED,
1314
ERR_INSPECTOR_COMMAND,
@@ -33,6 +34,7 @@ const {
3334
MainThreadConnection,
3435
open,
3536
url,
37+
isEnabled,
3638
waitForDebugger
3739
} = internalBinding('inspector');
3840

@@ -131,6 +133,9 @@ class Session extends EventEmitter {
131133
}
132134

133135
function inspectorOpen(port, host, wait) {
136+
if (isEnabled()) {
137+
throw new ERR_INSPECTOR_ALREADY_ACTIVATED();
138+
}
134139
open(port, host);
135140
if (wait)
136141
waitForDebugger();

lib/internal/errors.js

+4
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,10 @@ E('ERR_INCOMPATIBLE_OPTION_PAIR',
941941
'Option "%s" cannot be used in combination with option "%s"', TypeError);
942942
E('ERR_INPUT_TYPE_NOT_ALLOWED', '--input-type can only be used with string ' +
943943
'input via --eval, --print, or STDIN', Error);
944+
E('ERR_INSPECTOR_ALREADY_ACTIVATED',
945+
'Inspector is already activated. Close it with inspector.close() ' +
946+
'before activating it again.',
947+
Error);
944948
E('ERR_INSPECTOR_ALREADY_CONNECTED', '%s is already connected', Error);
945949
E('ERR_INSPECTOR_CLOSED', 'Session was closed', Error);
946950
E('ERR_INSPECTOR_COMMAND', 'Inspector error %d: %s', Error);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Flags: --inspect=0
2+
'use strict';
3+
4+
const common = require('../common');
5+
common.skipIfInspectorDisabled();
6+
common.skipIfWorker();
7+
8+
const assert = require('assert');
9+
const inspector = require('inspector');
10+
const wsUrl = inspector.url();
11+
assert(wsUrl.startsWith('ws://'));
12+
assert.throws(() => {
13+
inspector.open(0, undefined, false);
14+
}, {
15+
code: 'ERR_INSPECTOR_ALREADY_ACTIVATED'
16+
});
17+
assert.strictEqual(inspector.url(), wsUrl);
18+
inspector.close();
19+
assert.strictEqual(inspector.url(), undefined);

test/sequential/test-inspector-open.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ const fork = require('child_process').fork;
1010
const net = require('net');
1111
const url = require('url');
1212

13+
const kFirstOpen = 0;
14+
const kOpenWhileOpen = 1;
15+
const kReOpen = 2;
16+
1317
if (process.env.BE_CHILD)
1418
return beChild();
1519

@@ -19,7 +23,7 @@ const child = fork(__filename,
1923
child.once('message', common.mustCall((msg) => {
2024
assert.strictEqual(msg.cmd, 'started');
2125

22-
child.send({ cmd: 'open', args: [0] });
26+
child.send({ cmd: 'open', args: [kFirstOpen] });
2327
child.once('message', common.mustCall(firstOpen));
2428
}));
2529

@@ -31,7 +35,7 @@ function firstOpen(msg) {
3135
ping(port, (err) => {
3236
assert.ifError(err);
3337
// Inspector is already open, and won't be reopened, so args don't matter.
34-
child.send({ cmd: 'open', args: [] });
38+
child.send({ cmd: 'open', args: [kOpenWhileOpen] });
3539
child.once('message', common.mustCall(tryToOpenWhenOpen));
3640
firstPort = port;
3741
});
@@ -62,7 +66,7 @@ function closeWhenOpen(msg) {
6266
function tryToCloseWhenClosed(msg) {
6367
assert.strictEqual(msg.cmd, 'url');
6468
assert.strictEqual(msg.url, undefined);
65-
child.send({ cmd: 'open', args: [] });
69+
child.send({ cmd: 'open', args: [kReOpen] });
6670
child.once('message', common.mustCall(reopenAfterClose));
6771
}
6872

@@ -93,7 +97,17 @@ function beChild() {
9397

9498
process.on('message', (msg) => {
9599
if (msg.cmd === 'open') {
96-
inspector.open(...msg.args);
100+
if (msg.args[0] === kFirstOpen) {
101+
inspector.open(0, false, undefined);
102+
} else if (msg.args[0] === kOpenWhileOpen) {
103+
assert.throws(() => {
104+
inspector.open(0, false, undefined);
105+
}, {
106+
code: 'ERR_INSPECTOR_ALREADY_ACTIVATED'
107+
});
108+
} else if (msg.args[0] === kReOpen) {
109+
inspector.open(0, false, undefined);
110+
}
97111
}
98112
if (msg.cmd === 'close') {
99113
inspector.close();

0 commit comments

Comments
 (0)