Repository navigation
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.opencan 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.closeisundefinedin 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
Activity
addaleax commentedon Dec 13, 2021
@ds3p2 I think the best way to address this would be to always open/close the inspector server from the main thread, and then communicate to the worker threads using that?
ds3p2 commentedon Dec 13, 2021
@addaleax
My case is a little special.
I suppose different threads are running tasks belongs to different owners, and there will be more than one developer doing debugging at the same time, each focusing on one specific thread. If I use an inspector from the main thread, then developer may accidentally comes across breakpoint from other thread or the main thread (when some of them do not set proper breakpoint condition).
Although I did not find relative statement in any document, I am quite satisfied with the current behavior that the inspector created in a worker thread is "scoped".
addaleax commentedon Dec 13, 2021
@ds3p2 I guess you could also use the
inspector.SessionAPI and host the TCP server yourself, for each thread?I don’t think this is the intended use case here – if you do want to get into a situation in which there can be more than one built-in inspector TCP server per process, I think that would require some work on Node.js itself.
ds3p2 commentedon Dec 13, 2021
@addaleax
I do not quite understand your suggestion. In the main process I cannot call
inspector.openmultiple times (ErrorInspector is already activated. Close it with inspector.close() before activating it again.). Even if I can create separate session for each thread, how do the developer specify which thread to debug when trying to attach to the same debug port?I am not sure if I understand this correctly. Are you suggesting that
inspector.openshould not be called inside a worker thread? If so, why not disable the API just likeinspector.close?addaleax commentedon Dec 13, 2021
Yes.
I don’t know?
ds3p2 commentedon Dec 13, 2021
@addaleax
OK, it seems I need to look for some other approach.
Is there any official document where usage of
inspector.openin worker thread is discouraged?addaleax commentedon Dec 13, 2021
@ds3p2 No, but I assume there should be one in https://nodejs.org/api/inspector.html#inspectoropenport-host-wait. It’s a bit unfortunate that the inspector API was never really well-documented to begin with 😕
ds3p2 commentedon Dec 13, 2021
@addaleax
It's OK, thanks for your kind help. I am closing this issue.