-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
Debug worker_threads #26609
Comments
Fwiw, I recently wrote @eugeneo an email about this. We implemented something similar to the /cc @nodejs/v8-inspector |
/cc @pavelfeldman |
@aslushnikov , @a1ph, do you have cycles to look at it? |
@ak239 , anyone? :) Now that we have the flattened session support settled in chrome land, we can probably name this domain Target and introduce the flattened sessionId dance in Node. |
NDB has added support for worker_threads. Do we expect something similar in future? |
@gauravmahto Unfortunately, DevTools does not support this yet, https://bugs.chromium.org/p/chromium/issues/detail?id=968472 is their open issue for this. |
There are two separate things:
|
@ak239 What do we need to do for the second part? |
@ak239 Maybe this helps solve the second part? |
@addaleax there are three methods available on V8Inspector: storeCurrentStackTrace,externalAsyncTaskStarted,externalAsyncTaskFinished. To support step into between threads we need to call storeCurrentStackTrace before postMessage, pass V8StackTraceId to another thread and call externalAsyncTaskStarted / finished on another thread when we are processing this message. V8StackTraceId is actually pair of ints. As soon as it is done, when in Second idea that is implemented in Chrome, it is doing the same for Worker constructor: store at constructor call, and call started/finished for main worker script. When this one is done, user can click step into worker constructor and go to worker script. |
In case any debugger implementors come across there -- Node has an extension to the debugger protocol which allows debugging worker threads -- with minimal work given your debugger is already multi-target aware, like VS Code's. However this is not documented anywhere so I have no idea how stable it may be, YMMV. |
Debugging workers with This does not work: import { isMainThread, Worker } from "node:worker_threads";
import { fileURLToPath } from "node:url";
const filename = fileURLToPath(import.meta.url);
if (isMainThread) {
new Worker(filename, { execArgv: ["--inspect-brk"] });
} else {
debugger; // No stop here
} The import { isMainThread, Worker } from "node:worker_threads";
import { fileURLToPath } from "node:url";
+import inspector from "node:inspector";
const filename = fileURLToPath(import.meta.url);
if (isMainThread) {
new Worker(filename, { execArgv: ["--inspect-brk"] });
} else {
+ if (process.execArgv.includes("--inspect-brk")) {
+ inspector.open();
+ inspector.waitForDebugger();
+ }
debugger; // Stops in worker thread!
} Adding the |
Could you please elaborate on this? Does this enable debugging a worker with chrome devtools? Because I'm not having any success - the worker doesn't show up in chrome after executing |
Hmm, @AriPerkkio do you know if it works with require too? Just adding And nothing shows up in chrome I'm trying to get a debugger to where this error is thrown, see reproduction steps there: swc-project/swc-node#736 (comment) and I'm following these steps to build node 20.9.0 https://www.devdungeon.com/content/build-nodejs-source |
Fyi, VS Code's JavaScript debugger automatically debugs node_workers using the NodeWorker domain (#26609 (comment)). So if you just need to debug a worker, easiest way is to run "Create JavaScript Debug Terminal" in VS Code; everything run in that terminal will be debugged. |
@connor4312 TYSM, that got me a debugger inside of the worker! Unfortunately it doesn't seem like the vscode debugger can go to function definitions on all functions ( |
But what about the CLI debugger? I'd like to be able to pass As things are, I am unable to start the internal (CLI) debugger for a worker thread on Node v22.9.0 (Linux) with :
const { Worker, isMainThread } = require('node:worker_threads');
if(isMainThread) {
const worker = new Worker(__filename, { execArgv: ["--inspect-brk"] })
worker.on('error', console.error)
worker.on('exit', (code) => console.log(`Worker stopped with exit code ${code}`))
} else {
debugger
} What happens is, the process hangs, and here are the NODE_DEBUG and strace logs: Here's what the debugger yields when running the Spoiler[user@pc node-playground]$ node inspect -p 123 connecting to 127.0.0.1:9229 ... ok debug> scripts 22: node:events 24: node:buffer 31: node:async_hooks 32: node:timers 34: node:path 37: node:querystring 42: node:fs 47: node:util 62: node:url 66: node:diagnostics_channel 75: inspect-cli.js 76: node:worker_threads 80: node:stream 92: node:string_decoder 95: node:stream/promises 101: node:inspector 102: node:tty 103: node:net Finally, I think it would only make sense that nodejs start the CLI debugger:
|
There is no possible to debug worker threads, only through event messages to main thread
You can't step into this worker in the chrome dev tools
debugger
statements also don't work inside threadThe text was updated successfully, but these errors were encountered: