mxsdev / nvim-dap-vscode-js

nvim-dap adapter for vscode-js-debug
278 stars 30 forks source link

Unable to debug worker threads #6

Open jZhangTk opened 2 years ago

jZhangTk commented 2 years ago

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?

mxsdev commented 2 years ago

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.

jZhangTk commented 2 years ago

@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 commented 2 years ago

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');
    },
});