Node: Debug worker_threads

Created on 12 Mar 2019  路  10Comments  路  Source: nodejs/node

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

const worker = new Worker('./worker.js', {});

debugger statements also don't work inside thread

debugger inspector worker

Most helpful comment

@gauravmahto Unfortunately, DevTools does not support this yet, https://bugs.chromium.org/p/chromium/issues/detail?id=968472 is their open issue for this.

All 10 comments

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鈥檛 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.

/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?
https://github.com/GoogleChromeLabs/ndb/pull/281

@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:

  • DevTools folks can adopt ndb code to support workers,
  • additional instrumentation should be added in node to support stepping between main and worker threads.

@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 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.

Was this page helpful?
0 / 5 - 0 ratings