Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inspector: expose inspector.close on workers #44489

Merged
merged 1 commit into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions doc/api/inspector.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
3 changes: 2 additions & 1 deletion lib/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const {
validateString,
} = require('internal/validators');
const { isMainThread } = require('worker_threads');
const { _debugEnd } = internalBinding('process_methods');

const {
Connection,
Expand Down Expand Up @@ -195,7 +196,7 @@ function inspectorWaitForDebugger() {

module.exports = {
open: inspectorOpen,
close: process._debugEnd,
close: _debugEnd,
url,
waitForDebugger: inspectorWaitForDebugger,
console,
Expand Down
4 changes: 2 additions & 2 deletions src/node_process_methods.cc
Original file line number Diff line number Diff line change
Expand Up @@ -566,24 +566,24 @@ static void Initialize(Local<Object> 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);
Expand Down
34 changes: 34 additions & 0 deletions test/parallel/test-inspector-close-worker.js
Original file line number Diff line number Diff line change
@@ -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();
MoLow marked this conversation as resolved.
Show resolved Hide resolved
assert.strictEqual(inspector.url(), undefined);
}));