Skip to content

Commit 4e49d5d

Browse files
legendecasjuanarbol
authored andcommitted
inspector: expose inspector.close on workers
Workers can open their own inspector agent with `inspector.open`. They should be able to close their own inspector agent too with `inspector.close`. PR-URL: #44489 Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 31f809a commit 4e49d5d

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

doc/api/inspector.md

-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ const inspector = require('node:inspector');
1919

2020
Deactivate the inspector. Blocks until there are no active connections.
2121

22-
This function is not available in [worker threads][].
23-
2422
## `inspector.console`
2523

2624
* {Object} An object to send messages to the remote inspector console.
@@ -256,4 +254,3 @@ session.post('HeapProfiler.takeHeapSnapshot', null, (err, r) => {
256254
[`'Debugger.paused'`]: https://chromedevtools.github.io/devtools-protocol/v8/Debugger#event-paused
257255
[`session.connect()`]: #sessionconnect
258256
[security warning]: cli.md#warning-binding-inspector-to-a-public-ipport-combination-is-insecure
259-
[worker threads]: worker_threads.md

lib/inspector.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const {
3232
validateString,
3333
} = require('internal/validators');
3434
const { isMainThread } = require('worker_threads');
35+
const { _debugEnd } = internalBinding('process_methods');
3536

3637
const {
3738
Connection,
@@ -194,7 +195,7 @@ function inspectorWaitForDebugger() {
194195

195196
module.exports = {
196197
open: inspectorOpen,
197-
close: process._debugEnd,
198+
close: _debugEnd,
198199
url,
199200
waitForDebugger: inspectorWaitForDebugger,
200201
// This is dynamically added during bootstrap,

src/node_process_methods.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,6 @@ static void Initialize(Local<Object> target,
563563
// define various internal methods
564564
if (env->owns_process_state()) {
565565
env->SetMethod(target, "_debugProcess", DebugProcess);
566-
env->SetMethod(target, "_debugEnd", DebugEnd);
567566
env->SetMethod(target, "abort", Abort);
568567
env->SetMethod(target, "causeSegfault", CauseSegfault);
569568
env->SetMethod(target, "chdir", Chdir);
@@ -576,6 +575,7 @@ static void Initialize(Local<Object> target,
576575
env->SetMethod(target, "cpuUsage", CPUUsage);
577576
env->SetMethod(target, "resourceUsage", ResourceUsage);
578577

578+
env->SetMethod(target, "_debugEnd", DebugEnd);
579579
env->SetMethod(target, "_getActiveRequests", GetActiveRequests);
580580
env->SetMethod(target, "_getActiveRequestsInfo", GetActiveRequestsInfo);
581581
env->SetMethod(target, "_getActiveHandles", GetActiveHandles);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
common.skipIfInspectorDisabled();
5+
const { isMainThread, Worker } = require('worker_threads');
6+
const assert = require('assert');
7+
const inspector = require('inspector');
8+
9+
if (!isMainThread) {
10+
// Verify the inspector api on the worker thread.
11+
assert.strictEqual(inspector.url(), undefined);
12+
13+
inspector.open(0, undefined, false);
14+
const wsUrl = inspector.url();
15+
assert(wsUrl.startsWith('ws://'));
16+
inspector.close();
17+
assert.strictEqual(inspector.url(), undefined);
18+
return;
19+
}
20+
21+
// Open inspector on the main thread first.
22+
inspector.open(0, undefined, false);
23+
const wsUrl = inspector.url();
24+
assert(wsUrl.startsWith('ws://'));
25+
26+
const worker = new Worker(__filename);
27+
worker.on('exit', common.mustCall((code) => {
28+
assert.strictEqual(code, 0);
29+
30+
// Verify inspector on the main thread is still active.
31+
assert.strictEqual(inspector.url(), wsUrl);
32+
inspector.close();
33+
assert.strictEqual(inspector.url(), undefined);
34+
}));

0 commit comments

Comments
 (0)