You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fwiw, I recently wrote @eugeneo an email about this. We implemented something similar to the Target domain that Chrome uses, but under a different name. I don’t know if the plan is for Devtools to use the Node.js-specific domain, or for us to eventually use the Target domain as well.
@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.
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 ndb you press step into on post message, debugger will automatically go to the worker and pause there when worker is about to process message.
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 --inspect-brk is possible with some hacky patching. Passing it as execArgv won't work but using node:inspector inside the worker works.
This does not work:
import{isMainThread,Worker}from"node:worker_threads";import{fileURLToPath}from"node:url";constfilename=fileURLToPath(import.meta.url);if(isMainThread){newWorker(filename,{execArgv: ["--inspect-brk"]});}else{debugger;// No stop here}
The node:inspector works. Chrome DevTools stop at the debugger.
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 process.execArgv checks in userland seems like unnecessary work - maybe Node could do this for us?
Reacted by peelz, Dean Xu, Nathaniel, Mattias Buelens, Tamir Duberstein, bobOnGitHub and Max Coplan
Debugging workers with --inspect-brk is possible with some hacky patching. Passing it as execArgv won't work but using node:inspector inside the worker works.
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 inspector.open();inspector.waitForDebugger(); in it, it seems like it just freezes
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 ( [[FunctionLocation]]) and also the "break on caught exceptions" doesn't seem to work within a worker but I got further, thanks :)
I'd like to be able to pass inspect as a positional argument to the worker causing it to start the CLI debugger just like we would do with node inspect script.js.
As things are, I am unable to start the internal (CLI) debugger for a worker thread on Node v22.9.0 (Linux) with :
node inspect-cli.js
const{ Worker, isMainThread }=require('node:worker_threads');if(isMainThread){constworker=newWorker(__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:
Activity
addaleax commentedon Mar 12, 2019
Fwiw, I recently wrote @eugeneo an email about this. We implemented something similar to the
Targetdomain that Chrome uses, but under a different name. I don’t know if the plan is for Devtools to use the Node.js-specific domain, or for us to eventually use theTargetdomain as well./cc @nodejs/v8-inspector
ofrobots commentedon Mar 13, 2019
/cc @pavelfeldman
pavelfeldman commentedon Mar 13, 2019
@aslushnikov , @a1ph, do you have cycles to look at it?
pavelfeldman commentedon Mar 13, 2019
@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.
gauravmahto commentedon Jul 14, 2019
NDB has added support for worker_threads. Do we expect something similar in future?
GoogleChromeLabs/ndb#281
addaleax commentedon Jul 14, 2019
@gauravmahto Unfortunately, DevTools does not support this yet, https://bugs.chromium.org/p/chromium/issues/detail?id=968472 is their open issue for this.
alexkozy commentedon Jul 14, 2019
There are two separate things:
addaleax commentedon Jul 14, 2019
@ak239 What do we need to do for the second part?
gauravmahto commentedon Jul 15, 2019
@ak239 Maybe this helps solve the second part?
alexkozy commentedon Jul 15, 2019
@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
ndbyou press step into on post message, debugger will automatically go to the worker and pause there when worker is about to process message.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.
connor4312 commentedon Dec 10, 2020
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.
AriPerkkio commentedon Mar 10, 2023
Debugging workers with
--inspect-brkis possible with some hacky patching. Passing it asexecArgvwon't work but usingnode:inspectorinside the worker works.This does not work:
The
node:inspectorworks. Chrome DevTools stop at thedebugger.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
process.execArgvchecks in userland seems like unnecessary work - maybe Node could do this for us?danieltroger commentedon Nov 7, 2023
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
inspector.open();inspector.waitForDebugger();in it, it seems like it just freezesAriPerkkio commentedon Nov 7, 2023
Yes, just save the code above into file and run with
node. Here's picture from Chrome dev tools:danieltroger commentedon Nov 7, 2023
Hmm, @AriPerkkio do you know if it works with require too? Just adding
const inspector = require("node:inspector");at the top of the worker I'm trying to debug, https://github.com/nodejs/node/blob/55d2eb53d78040472371eedd0e9e7922748dfb0e/lib/internal/modules/esm/worker.js makes the worker freeze for me (become unresponsive at 0% CPU usage) 😢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
connor4312 commentedon Nov 7, 2023
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.
danieltroger commentedon Nov 7, 2023
@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 (
[[FunctionLocation]]) and also the "break on caught exceptions" doesn't seem to work within a worker but I got further, thanks :)thoughtsunificator commentedon Oct 26, 2024
But what about the CLI debugger?
I'd like to be able to pass
inspectas a positional argument to the worker causing it to start the CLI debugger just like we would do withnode inspect script.js.As things are, I am unable to start the internal (CLI) debugger for a worker thread on Node v22.9.0 (Linux) with :
node inspect-cli.jsWhat happens is, the process hangs, and here are the NODE_DEBUG and strace logs:
node_debug.txt
strace.txt
Here's what the debugger yields when running the
scriptscommand after attaching a debugger client usingnode inspect -p PID:Spoiler
Finally, I think it would only make sense that nodejs start the CLI debugger:
debuggercyans-nya commentedon Jan 14, 2025
Can I use the Session from the
node:inspectormodule and connect to a worker?eugeneo commentedon Jan 14, 2025