diff --git a/doc/api/inspector.md b/doc/api/inspector.md index 10458d9ac51c82..dd5fac313d020b 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. @@ -256,4 +254,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 dafc4ef4932ff1..4212ebb34f8d3c 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, @@ -194,7 +195,7 @@ function inspectorWaitForDebugger() { module.exports = { open: inspectorOpen, - close: process._debugEnd, + close: _debugEnd, url, waitForDebugger: inspectorWaitForDebugger, // This is dynamically added during bootstrap, diff --git a/src/node_process_methods.cc b/src/node_process_methods.cc index 6f8b42be01063a..f38f601d779534 100644 --- a/src/node_process_methods.cc +++ b/src/node_process_methods.cc @@ -563,7 +563,6 @@ static void Initialize(Local target, // define various internal methods if (env->owns_process_state()) { env->SetMethod(target, "_debugProcess", DebugProcess); - env->SetMethod(target, "_debugEnd", DebugEnd); env->SetMethod(target, "abort", Abort); env->SetMethod(target, "causeSegfault", CauseSegfault); env->SetMethod(target, "chdir", Chdir); @@ -576,6 +575,7 @@ static void Initialize(Local target, env->SetMethod(target, "cpuUsage", CPUUsage); env->SetMethod(target, "resourceUsage", ResourceUsage); + env->SetMethod(target, "_debugEnd", DebugEnd); env->SetMethod(target, "_getActiveRequests", GetActiveRequests); env->SetMethod(target, "_getActiveRequestsInfo", GetActiveRequestsInfo); env->SetMethod(target, "_getActiveHandles", GetActiveHandles); 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); +}));