Skip to content

How to close inspector in worker thread? #3651

Closed
@ds3p2

Description

@ds3p2

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 is undefined 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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions