Skip to content

Commit 1b16051

Browse files
legendecasRafaelGSS
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 <moshe@atlow.co.il> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 7db2974 commit 1b16051

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
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.
@@ -262,4 +260,3 @@ session.post('HeapProfiler.takeHeapSnapshot', null, (err, r) => {
262260
[`'Debugger.paused'`]: https://chromedevtools.github.io/devtools-protocol/v8/Debugger#event-paused
263261
[`session.connect()`]: #sessionconnect
264262
[security warning]: cli.md#warning-binding-inspector-to-a-public-ipport-combination-is-insecure
265-
[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,
@@ -195,7 +196,7 @@ function inspectorWaitForDebugger() {
195196

196197
module.exports = {
197198
open: inspectorOpen,
198-
close: process._debugEnd,
199+
close: _debugEnd,
199200
url,
200201
waitForDebugger: inspectorWaitForDebugger,
201202
console,

src/node_process_methods.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -562,24 +562,24 @@ static void Initialize(Local<Object> target,
562562
// define various internal methods
563563
if (env->owns_process_state()) {
564564
SetMethod(context, target, "_debugProcess", DebugProcess);
565-
SetMethod(context, target, "_debugEnd", DebugEnd);
566565
SetMethod(context, target, "abort", Abort);
567566
SetMethod(context, target, "causeSegfault", CauseSegfault);
568567
SetMethod(context, target, "chdir", Chdir);
569568
}
570569

571570
SetMethod(context, target, "umask", Umask);
572-
SetMethod(context, target, "_rawDebug", RawDebug);
573571
SetMethod(context, target, "memoryUsage", MemoryUsage);
574572
SetMethod(context, target, "rss", Rss);
575573
SetMethod(context, target, "cpuUsage", CPUUsage);
576574
SetMethod(context, target, "resourceUsage", ResourceUsage);
577575

576+
SetMethod(context, target, "_debugEnd", DebugEnd);
578577
SetMethod(context, target, "_getActiveRequestsInfo", GetActiveRequestsInfo);
579578
SetMethod(context, target, "_getActiveRequests", GetActiveRequests);
580579
SetMethod(context, target, "_getActiveHandles", GetActiveHandles);
581580
SetMethod(context, target, "_getActiveHandlesInfo", GetActiveHandlesInfo);
582581
SetMethod(context, target, "_kill", Kill);
582+
SetMethod(context, target, "_rawDebug", RawDebug);
583583

584584
SetMethodNoSideEffect(context, target, "cwd", Cwd);
585585
SetMethod(context, target, "dlopen", binding::DLOpen);
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)