Closed
Description
Details
I am trying to enable some debugging in the worker thread programmatically. I read the Inspector API and trying to implement it in the worker thread model.
Currently I am not sure which is the proper way to use the inspector
module in worker thread.
The sample code shows how I am using inspector
.
- I found
inspector.open
can be called in the worker thread, and the created inspector only works in the current thread. Breakpoints in other thread / main thread will be just ignored. - However, I found
inspector.close
isundefined
in worker thread. According to fix(worker): allow inspector.close() inside Worker node#30680, this is the expected behavior. But the question is how do I close the debugger port created in the worker thread?
Node.js version
14.15.5
Example code
const {
isMainThread,
parentPort,
Worker,
workerData,
} = require("worker_threads");
const inspector = require("inspector");
if (isMainThread) {
console.log("in main thread", inspector.close);
// inspector.open(9231, "127.0.0.1", true); // Uncomment this line will get error "Inspector is already activated"
const worker1 = new Worker(__filename, { workerData: { id: 1 } });
const worker2 = new Worker(__filename, {
workerData: { id: 2 },
});
let cnt = 0;
worker1.on("message", () => {
cnt++;
});
worker2.on("message", () => {
cnt++;
});
} else {
if (workerData.id === 1) {
console.log("in worker thread", inspector.close);
inspector.open(9229, "127.0.0.1", true);
const content = "I am worker: " + workerData.id;
setInterval(() => {
console.log(content);
parentPort.postMessage("done");
}, 1000);
} else {
console.log("in worker thread", inspector.close);
inspector.open(9230, "127.0.0.1", true);
const content = "I am worker: " + workerData.id;
setInterval(() => {
console.log(content);
parentPort.postMessage("done");
}, 1000);
}
}
Operating system
Mac OS 12.0.1
Scope
code, runtime
Module and version
Not applicable.
Metadata
Metadata
Assignees
Labels
No labels