-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
Closed
Labels
inspectorIssues and PRs related to the V8 inspector protocolIssues and PRs related to the V8 inspector protocolworkerIssues and PRs related to Worker support.Issues and PRs related to Worker support.
Description
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 correctlyHow 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 pendinginspector.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
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 correctlysheremet-va
Metadata
Metadata
Assignees
Labels
inspectorIssues and PRs related to the V8 inspector protocolIssues and PRs related to the V8 inspector protocolworkerIssues and PRs related to Worker support.Issues and PRs related to Worker support.