Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
AriPerkkio opened this issue Apr 11, 2024 · 0 comments · Fixed by #52527
Closed

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

AriPerkkio opened this issue Apr 11, 2024 · 0 comments · Fixed by #52527
Labels
inspector Issues and PRs related to the V8 inspector protocol worker Issues and PRs related to Worker support.

Comments

@AriPerkkio
Copy link

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
@VoltrexKeyva VoltrexKeyva added inspector Issues and PRs related to the V8 inspector protocol worker Issues and PRs related to Worker support. labels Apr 11, 2024
nodejs-github-bot pushed a commit that referenced this issue May 13, 2024
Signed-off-by: Daeyeon Jeong <[email protected]>
PR-URL: #52527
Fixes: #52467
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Kohei Ueno <[email protected]>
targos pushed a commit that referenced this issue May 13, 2024
Signed-off-by: Daeyeon Jeong <[email protected]>
PR-URL: #52527
Fixes: #52467
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Kohei Ueno <[email protected]>
marco-ippolito pushed a commit that referenced this issue Jun 17, 2024
Signed-off-by: Daeyeon Jeong <[email protected]>
PR-URL: #52527
Fixes: #52467
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Kohei Ueno <[email protected]>
marco-ippolito pushed a commit that referenced this issue Jun 17, 2024
Signed-off-by: Daeyeon Jeong <[email protected]>
PR-URL: #52527
Fixes: #52467
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Kohei Ueno <[email protected]>
marco-ippolito pushed a commit that referenced this issue Jun 17, 2024
Signed-off-by: Daeyeon Jeong <[email protected]>
PR-URL: #52527
Fixes: #52467
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Kohei Ueno <[email protected]>
EliphazBouye pushed a commit to EliphazBouye/node that referenced this issue Jun 20, 2024
Signed-off-by: Daeyeon Jeong <[email protected]>
PR-URL: nodejs#52527
Fixes: nodejs#52467
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Kohei Ueno <[email protected]>
bmeck pushed a commit to bmeck/node that referenced this issue Jun 22, 2024
Signed-off-by: Daeyeon Jeong <[email protected]>
PR-URL: nodejs#52527
Fixes: nodejs#52467
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Kohei Ueno <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
inspector Issues and PRs related to the V8 inspector protocol worker Issues and PRs related to Worker support.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants