Skip to content

Worker with inspector.waitForDebugger() cannot be terminated #52467

Description

@AriPerkkio

Version

v20.12.2, v21.7.3

Platform

Darwin Aris-MacBook-Pro.local 23.4.0 Darwin Kernel Version 23.4.0: Fri Mar 15 00:12:49 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6020 arm64

Subsystem

No response

What steps will reproduce the bug?

import { isMainThread, Worker } from "node:worker_threads";
import { fileURLToPath } from "node:url";
import inspector from "node:inspector";

if (isMainThread) {
  const worker = new Worker(fileURLToPath(import.meta.url));
  await new Promise((r) => setTimeout(r, 1_000));

  const timeout = setTimeout(() => {
    console.log("Stuck here, calling process.exit()");
    process.exit();
  }, 2_000).unref();

  console.log("Terminating...");
  await worker.terminate();

  clearTimeout(timeout);
  console.log("Done");
} else {
  inspector.open();
  inspector.waitForDebugger();
}
$ node workers.mjs 
Debugger listening on ws://127.0.0.1:9229/0b636b79-612e-49a4-a344-17359caef664
For help, see: https://nodejs.org/en/docs/inspector
Terminating...
Stuck here, calling process.exit()

# Stuck forever, CTRL+c kills process correctly

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior? Why is that the expected behavior?

  • worker.terminate() should terminate the worker and the pending inspector.waitForDebugger()
  • process.exit() should kill the main thread's process

CTRL+c is able to do both requirements.

What do you see instead?

Worker does not terminate. Main thread cannot be exited.

Additional information

vitest-dev/vitest#5511

As (quite complex) work-around we can use web socket connection from main thread and connect to the inspector session. As soon as connection is made, the worker.terminate() activates and code after inspector.waitForDebugger() is not executed.

import { isMainThread, parentPort, Worker } from "node:worker_threads";
import { fileURLToPath } from "node:url";
import inspector from "node:inspector";
import WebSocket from "ws";

if (isMainThread) {
  const worker = new Worker(fileURLToPath(import.meta.url));
  const url = await new Promise((resolve) => worker.on("message", resolve));

  const timeout = setTimeout(async () => {
    console.log("Timeout");

    const ws = new WebSocket(url);
    await new Promise((resolve) => ws.on("open", resolve));

    ws.send(
      JSON.stringify({ method: "Runtime.runIfWaitingForDebugger", id: 2 })
    );
    ws.close();
  }, 2_000).unref();

  await worker.terminate();
  clearTimeout(timeout);
  console.log("Done");
} else {
  inspector.open();
  parentPort.postMessage(inspector.url());
  inspector.waitForDebugger();

  console.log("Running"); // Never executed
}
$ node workers.mjs 
Debugger listening on ws://127.0.0.1:9229/9fc3b32f-23cf-4597-b79e-30d4a61f32cd
For help, see: https://nodejs.org/en/docs/inspector
Timeout
Debugger attached.
Debugger ending on ws://127.0.0.1:9229/9fc3b32f-23cf-4597-b79e-30d4a61f32cd
For help, see: https://nodejs.org/en/docs/inspector
Done

# process exits correctly

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    inspectorIssues and PRs related to the V8 inspector protocol.workerIssues and PRs related to the worker_threads module and Worker API.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions