Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Node: debug into Worker Threads (node 11)

Can VS Code's Javascript debugger be made to debug node 11's new "Worker Threads"? Worker threads are modelled after web workers with a small number of extra capabilities on top and are available from the new worker_threads package (see https://nodejs.org/api/worker_threads.html). Other than with node's sub processes, one can share memory with worker threads in the form of SharedArrayBuffers.

My VS Code launch configuration looks like that:

   {
        "type": "node",
        "request": "launch",
        "name": "Mocha Tests",
        "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
        "runtimeArgs": [
            "--experimental-wasm-threads",
            "--experimental-worker"
        ],
        "args": [
            "-u", "tdd",
            "--timeout", "100000",
            "--colors", "${workspaceFolder}/test"
        ],
        "internalConsoleOptions": "openOnSessionStart",
        "autoAttachChildProcesses": true
   }

I can debug into the main node script, but the "autoAttachChildProcesses" flag is not effective in attaching to the spawned off worker threads and neither is adding a "debugger" statement within the code that runs inside the worker thread.

They mention that the "inspector" package is not yet supported for worker threads in their reference documentation, so I guess this may explain why that is.

Against all these odds, has anyone still succeeded in debugging into worker threads inside VS Code?

like image 372
Soeren Avatar asked Jan 18 '19 10:01

Soeren


People also ask

How do I use the node debugger in Visual Studio Code?

Use a launch config to start your program, or attach to a process launched outside of VS Code. If the Auto Attach feature is enabled, the Node debugger automatically attaches to certain Node.js processes that have been launched from VS Code's Integrated Terminal.

How do I view threads in the Visual Studio debugger?

View threads in the Visual Studio debugger by using the Threads window (C#, Visual Basic, C++) In the Threads window, you can examine and work with threads in the application that you're debugging.

What runtimes are supported by the VS Code Node JS debugger?

Since the VS Code Node.js debugger communicates to the Node.js runtimes through wire protocols, the set of supported runtimes is determined by all runtimes supporting the wire protocols. Today two wire protocols exist: legacy: the original V8 Debugger Protocol which is currently supported by older runtimes.

How to debug a JavaScript program in Visual Studio Code?

1 Use auto attach to debug processes you run in VS Code's integrated terminal, 2 Use the JavaScript debug terminal, in a similar way, or. 3 Use a launch config to start your program, or attach to a process launched outside of VS Code.


2 Answers

In the latest release of vscode-js-debug debugging of workers is now supported when combined with a recent version of nodejs.

like image 93
Steven Avatar answered Oct 11 '22 11:10

Steven


UPDATE Feb 2021 It is now possible to debug worker threads in VSC using vscode-js-debug as pointed out in the other answer. I have tested it and it works quite well. Webstorm remains a great alternative, with perhaps a bit more features built-in, but bear in mind that it is a paid product.

Disclaimer: The answer below describes my experience debugging worker threads, but not using VS Code, which was the original question. I thought it would be useful to know alternative options anyway, since it seems that as of today the only option to debug worker threads is Webstorm.

Webstorm option

It is possible to debug worker threads using Webstorm: https://blog.jetbrains.com/webstorm/2018/10/webstorm-2018-3-eap-6/ I have tried it and it works really well (as documented in the link above).

Chrome tools

I have tried debugging worker threads using chrome dev tools, following the same approach in which you would debug web workers, but it did not work. When debugging web workers, they show up like this in chrome tools

enter image description here

Unfortunately, worker threads do not. When a worker thread runs, the debugger does not show it, and does not let you put breakpoints nor step through the code. I suspect that this may be because of this: https://github.com/nodejs/node/issues/26609

VS Code

VSC does not have a feature to debug worker threads. Interestingly, VCS also does not have a feature to debug Web Workers. This is an explicit decision on their part: https://github.com/Microsoft/vscode-chrome-debug/issues/675

like image 4
edoDev Avatar answered Oct 11 '22 13:10

edoDev