nvim-dap-vscode-js icon indicating copy to clipboard operation
nvim-dap-vscode-js copied to clipboard

Unable to debug worker threads

Open jZhangTk opened this issue 3 years ago • 3 comments

I'm trying to debug with worker threads. When it hits a breakpoint (DapStopped sign shows up), I can't continue, step over, inspect a variable, etc. It acts as it doesn't know it hit a breakpoint. When I do :lua require'dap'.continue() to continue, it gives me

Session active, but not stopped at breakpoint

And when I do :lua require'dap.ui.widgets'.hover() to inspect a variable, it gives me

Cannot evaluate "foo"!

Below is my config for javascript. It works perfectly without worker threads. And it also works in VS Code with worker threads (with "type": "node").

{
  type = 'pwa-node',
  request = 'launch',
  name = 'Debug',
  program = '${file}',
  cwd = '${fileDirname}',
  sourceMaps = true,
  skipFiles = { '<node_internals>/**' },
  outputCapture = 'std',
  resolveSourceMapLocations = {
    '**',
    '!**/node_modules/**',
  },
},

Is this a bug or misconfiguration?

jZhangTk avatar Aug 21 '22 03:08 jZhangTk

Hi, sorry for the late reply!

Can you send an MRE? I just tested a simple node.js project with worker threads using your configuration, and I was unable to replicate the issue.

mxsdev avatar Sep 10 '22 19:09 mxsdev

@mxsdev I'm sorry, what do you mean by MRE?

As I did a quick test with simple worker threads, it indeed worked. There must be something special I'm doing in my project caused the behaviour. Let me dig a little further and get back to you.

jZhangTk avatar Sep 13 '22 14:09 jZhangTk

Actually, it is not worker_threads but threads causing the issue.

Steps to reproduce:

  1. put the below files in the same folder
  2. npm i threads in that folder
  3. set a breakpoint at any line between new Worker() (excluded) and Thread.terminate() (included), or inside of the work() function
  4. debug index.js

Once it hits the breakpoint, you should see the behaviour described earlier (VS Code works just fine though). In addition, once the thread is terminated (e.g. console.log('done')), everything is back to normal. It seems like something is acting up with the threads created by new Worker().

// index.js
const { spawn, Thread, Worker } = require('threads');
const main = async function () {
    const worker = new Worker('./worker.js');
    const instance = await spawn(worker);
    await instance.work();
    await Thread.terminate(instance);
    console.log('done');
};
main();
// worker.js
const { expose } = require('threads');
expose({
    work() {
        console.log('hello world');
    },
});

jZhangTk avatar Sep 15 '22 01:09 jZhangTk