Skip to content

Commit d72b682

Browse files
eugeneotargos
authored andcommitted
inspector: report all workers
Main thread (the one that WS endpoint connects to) should be able to report all workers. PR-URL: #28872 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent b6e174b commit d72b682

File tree

4 files changed

+108
-9
lines changed

4 files changed

+108
-9
lines changed

src/inspector/worker_inspector.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ class ParentInspectorHandle {
5555
std::shared_ptr<MainThreadHandle> parent_thread,
5656
bool wait_for_connect);
5757
~ParentInspectorHandle();
58+
std::unique_ptr<ParentInspectorHandle> NewParentInspectorHandle(
59+
int thread_id, const std::string& url) {
60+
return std::make_unique<ParentInspectorHandle>(thread_id,
61+
url,
62+
parent_thread_,
63+
wait_);
64+
}
5865
void WorkerStarted(std::shared_ptr<MainThreadHandle> worker_thread,
5966
bool waiting);
6067
bool WaitForConnect() {

src/inspector_agent.cc

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,17 +237,21 @@ class ChannelImpl final : public v8_inspector::V8Inspector::Channel,
237237
tracing_agent_ =
238238
std::make_unique<protocol::TracingAgent>(env, main_thread_);
239239
tracing_agent_->Wire(node_dispatcher_.get());
240-
worker_agent_ = std::make_unique<protocol::WorkerAgent>(worker_manager);
241-
worker_agent_->Wire(node_dispatcher_.get());
240+
if (worker_manager) {
241+
worker_agent_ = std::make_unique<protocol::WorkerAgent>(worker_manager);
242+
worker_agent_->Wire(node_dispatcher_.get());
243+
}
242244
runtime_agent_ = std::make_unique<protocol::RuntimeAgent>();
243245
runtime_agent_->Wire(node_dispatcher_.get());
244246
}
245247

246248
~ChannelImpl() override {
247249
tracing_agent_->disable();
248250
tracing_agent_.reset(); // Dispose before the dispatchers
249-
worker_agent_->disable();
250-
worker_agent_.reset(); // Dispose before the dispatchers
251+
if (worker_agent_) {
252+
worker_agent_->disable();
253+
worker_agent_.reset(); // Dispose before the dispatchers
254+
}
251255
runtime_agent_->disable();
252256
runtime_agent_.reset(); // Dispose before the dispatchers
253257
}
@@ -670,6 +674,9 @@ class NodeInspectorClient : public V8InspectorClient {
670674
}
671675

672676
std::shared_ptr<WorkerManager> getWorkerManager() {
677+
if (!is_main_) {
678+
return nullptr;
679+
}
673680
if (worker_manager_ == nullptr) {
674681
worker_manager_ =
675682
std::make_shared<WorkerManager>(getThreadHandle());
@@ -968,7 +975,11 @@ void Agent::SetParentHandle(
968975

969976
std::unique_ptr<ParentInspectorHandle> Agent::GetParentHandle(
970977
int thread_id, const std::string& url) {
971-
return client_->getWorkerManager()->NewParentHandle(thread_id, url);
978+
if (!parent_handle_) {
979+
return client_->getWorkerManager()->NewParentHandle(thread_id, url);
980+
} else {
981+
return parent_handle_->NewParentInspectorHandle(thread_id, url);
982+
}
972983
}
973984

974985
void Agent::WaitForConnect() {

test/parallel/test-inspector-async-hook-after-done.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const common = require('../common');
44

55
common.skipIfInspectorDisabled();
6+
common.skipIfWorker();
67

78
const assert = require('assert');
89
const { Worker } = require('worker_threads');
@@ -12,9 +13,7 @@ const session = new Session();
1213

1314
let done = false;
1415

15-
session.connect();
16-
17-
session.on('NodeWorker.attachedToWorker', ({ params: { sessionId } }) => {
16+
function onAttachToWorker({ params: { sessionId } }) {
1817
let id = 1;
1918
function postToWorkerInspector(method, params) {
2019
session.post('NodeWorker.sendMessageToWorker', {
@@ -47,7 +46,11 @@ session.on('NodeWorker.attachedToWorker', ({ params: { sessionId } }) => {
4746
{ enabled: true });
4847
// start worker
4948
postToWorkerInspector('Runtime.runIfWaitingForDebugger');
50-
});
49+
}
50+
51+
session.connect();
52+
53+
session.on('NodeWorker.attachedToWorker', common.mustCall(onAttachToWorker));
5154

5255
session.post('NodeWorker.enable', { waitForDebuggerOnStart: true }, () => {
5356
new Worker('console.log("Worker is done")', { eval: true })
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
common.skipIfInspectorDisabled();
5+
6+
const { Worker, isMainThread, parentPort, workerData } =
7+
require('worker_threads');
8+
9+
if (isMainThread || workerData !== 'launched by test') {
10+
common.skipIfWorker();
11+
}
12+
13+
const { Session } = require('inspector');
14+
15+
const MAX_DEPTH = 3;
16+
17+
let rootWorker = null;
18+
19+
const runTest = common.mustCall(function() {
20+
let reportedWorkersCount = 0;
21+
const session = new Session();
22+
session.connect();
23+
session.on('NodeWorker.attachedToWorker', common.mustCall(
24+
({ params: { workerInfo } }) => {
25+
console.log(`Worker ${workerInfo.title} was reported`);
26+
if (++reportedWorkersCount === MAX_DEPTH) {
27+
rootWorker.postMessage({ done: true });
28+
}
29+
}, MAX_DEPTH));
30+
session.post('NodeWorker.enable', { waitForDebuggerOnStart: false });
31+
});
32+
33+
function processMessage({ child }) {
34+
console.log(`Worker ${child} is running`);
35+
if (child === MAX_DEPTH) {
36+
runTest();
37+
}
38+
}
39+
40+
function workerCallback(message) {
41+
parentPort.postMessage(message);
42+
}
43+
44+
function startWorker(depth, messageCallback) {
45+
const worker = new Worker(__filename, { workerData: 'launched by test' });
46+
worker.on('message', messageCallback);
47+
worker.postMessage({ depth });
48+
return worker;
49+
}
50+
51+
function runMainThread() {
52+
rootWorker = startWorker(1, processMessage);
53+
}
54+
55+
function runChildWorkerThread() {
56+
let worker = null;
57+
parentPort.on('message', ({ child, depth, done }) => {
58+
if (done) {
59+
if (worker) {
60+
worker.postMessage({ done: true });
61+
}
62+
parentPort.close();
63+
} else if (depth) {
64+
parentPort.postMessage({ child: depth });
65+
if (depth < MAX_DEPTH) {
66+
worker = startWorker(depth + 1, workerCallback);
67+
}
68+
} else if (child) {
69+
parentPort.postMessage({ child });
70+
}
71+
});
72+
}
73+
74+
if (isMainThread) {
75+
runMainThread();
76+
} else {
77+
runChildWorkerThread();
78+
}

0 commit comments

Comments
 (0)