From 1b160517f57cc960e3974efb8699e044abc7d082 Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Tue, 13 Sep 2022 17:04:24 +0800 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/44489 Reviewed-By: Moshe Atlow Reviewed-By: Anna Henningsen --- doc/api/inspector.md | 3 -- lib/inspector.js | 3 +- src/node_process_methods.cc | 4 +-- test/parallel/test-inspector-close-worker.js | 34 ++++++++++++++++++++ 4 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 test/parallel/test-inspector-close-worker.js diff --git a/doc/api/inspector.md b/doc/api/inspector.md index c0ab1ed1ce08f1..90bc90e2d12226 100644 --- a/doc/api/inspector.md +++ b/doc/api/inspector.md @@ -19,8 +19,6 @@ const inspector = require('node:inspector'); Deactivate the inspector. Blocks until there are no active connections. -This function is not available in [worker threads][]. - ## `inspector.console` * {Object} An object to send messages to the remote inspector console. @@ -262,4 +260,3 @@ session.post('HeapProfiler.takeHeapSnapshot', null, (err, r) => { [`'Debugger.paused'`]: https://chromedevtools.github.io/devtools-protocol/v8/Debugger#event-paused [`session.connect()`]: #sessionconnect [security warning]: cli.md#warning-binding-inspector-to-a-public-ipport-combination-is-insecure -[worker threads]: worker_threads.md diff --git a/lib/inspector.js b/lib/inspector.js index b0ab2b8517451c..8c2f649411284b 100644 --- a/lib/inspector.js +++ b/lib/inspector.js @@ -32,6 +32,7 @@ const { validateString, } = require('internal/validators'); const { isMainThread } = require('worker_threads'); +const { _debugEnd } = internalBinding('process_methods'); const { Connection, @@ -195,7 +196,7 @@ function inspectorWaitForDebugger() { module.exports = { open: inspectorOpen, - close: process._debugEnd, + close: _debugEnd, url, waitForDebugger: inspectorWaitForDebugger, console, diff --git a/src/node_process_methods.cc b/src/node_process_methods.cc index 9d4a5abb0163b6..d7de7959254463 100644 --- a/src/node_process_methods.cc +++ b/src/node_process_methods.cc @@ -562,24 +562,24 @@ static void Initialize(Local target, // define various internal methods if (env->owns_process_state()) { SetMethod(context, target, "_debugProcess", DebugProcess); - SetMethod(context, target, "_debugEnd", DebugEnd); SetMethod(context, target, "abort", Abort); SetMethod(context, target, "causeSegfault", CauseSegfault); SetMethod(context, target, "chdir", Chdir); } SetMethod(context, target, "umask", Umask); - SetMethod(context, target, "_rawDebug", RawDebug); SetMethod(context, target, "memoryUsage", MemoryUsage); SetMethod(context, target, "rss", Rss); SetMethod(context, target, "cpuUsage", CPUUsage); SetMethod(context, target, "resourceUsage", ResourceUsage); + SetMethod(context, target, "_debugEnd", DebugEnd); SetMethod(context, target, "_getActiveRequestsInfo", GetActiveRequestsInfo); SetMethod(context, target, "_getActiveRequests", GetActiveRequests); SetMethod(context, target, "_getActiveHandles", GetActiveHandles); SetMethod(context, target, "_getActiveHandlesInfo", GetActiveHandlesInfo); SetMethod(context, target, "_kill", Kill); + SetMethod(context, target, "_rawDebug", RawDebug); SetMethodNoSideEffect(context, target, "cwd", Cwd); SetMethod(context, target, "dlopen", binding::DLOpen); diff --git a/test/parallel/test-inspector-close-worker.js b/test/parallel/test-inspector-close-worker.js new file mode 100644 index 00000000000000..9d41468e3b267a --- /dev/null +++ b/test/parallel/test-inspector-close-worker.js @@ -0,0 +1,34 @@ +'use strict'; + +const common = require('../common'); +common.skipIfInspectorDisabled(); +const { isMainThread, Worker } = require('worker_threads'); +const assert = require('assert'); +const inspector = require('inspector'); + +if (!isMainThread) { + // Verify the inspector api on the worker thread. + assert.strictEqual(inspector.url(), undefined); + + inspector.open(0, undefined, false); + const wsUrl = inspector.url(); + assert(wsUrl.startsWith('ws://')); + inspector.close(); + assert.strictEqual(inspector.url(), undefined); + return; +} + +// Open inspector on the main thread first. +inspector.open(0, undefined, false); +const wsUrl = inspector.url(); +assert(wsUrl.startsWith('ws://')); + +const worker = new Worker(__filename); +worker.on('exit', common.mustCall((code) => { + assert.strictEqual(code, 0); + + // Verify inspector on the main thread is still active. + assert.strictEqual(inspector.url(), wsUrl); + inspector.close(); + assert.strictEqual(inspector.url(), undefined); +}));