threads.js icon indicating copy to clipboard operation
threads.js copied to clipboard

Debugging with ts-node and VS code

Open tuturis opened this issue 4 years ago • 4 comments

There seems to be an issue with running threads.js with node -r ts-node/register. Description of how the issue appears: Launching application through VS Code debugger that has threads.js method call that spawns a new worker. After the first call to such method, everything works fine - worker returns. Subsequent calls throws

Error: Timeout: Did not receive an init message from worker after 10000ms. Make sure the worker calls expose().
    at Timeout._onTimeout (E:\something\node_modules\threads\dist\master\spawn.js:34:53)
    at listOnTimeout (internal/timers.js:531:17)
    at processTimers (internal/timers.js:475:7)

Details: tsconfig.json

{
  "compilerOptions": {
    "skipLibCheck": true,
    "lib": [
      "dom",
      "es5",
      "es6",
      "es7",
      "es2015",
      "es2016",
      "es2017",
      "es2018",
      "esnext"
    ],
    "module": "commonjs",
    "moduleResolution": "node",
    "pretty": true,
    "sourceMap": false,
    "target": "es6",
    "outDir": "./dist",
    "baseUrl": "./",
    "declaration": true,
    },
    "forceConsistentCasingInFileNames": false,
    "noImplicitReturns": true,
    "noImplicitAny": true,
    "strict": true,
    "strictNullChecks": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "resolveJsonModule": true,
    "noUnusedLocals": false,
    "esModuleInterop": true,
    "typeRoots": [
      "./node_modules/@types",
      "./src/@types"
    ]
  },
  "include": [
    "./src/*.ts",
    "./src/**/*.ts",
    "./src/**/*.d.ts"
  ],
  "exclude": [
    "node_modules"
  ],
  "typeAcquisition": {
    "enable": true
  }
}

vs code debuggers launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "API",
      "type": "node",
      "request": "launch",
      "args": [
        "${workspaceFolder}/src/Server.ts"
      ],
      "runtimeArgs": [
        "-r",
        "ts-node/register"
      ],
      "cwd": "${workspaceRoot}",
      "protocol": "inspector",
      "internalConsoleOptions": "openOnSessionStart",
    }
  ]
}

Sample code Controller with the worker method

export default class Controller {
    worker_pool: WorkerPool.Pool<WorkerPool.ModuleThread<Worker>>
    constructor() {
        this.story_worker_pool = Pool(() => spawn<Worker>(new Worker("./worker.worker")))
    }
   async method() {
        return this.worker_pool.queue(async worker => {
            processed_items = await worker.processItems(items)
        })
   }
}

worker

export class Worker implements WorkerModule<any>  {
    [x: string]: WorkerFunction;

    public processItems(items): any[] {
        items.map((story, index) => {
            // do math
        });
        return items
    }
}
expose(Worker as any)

How this issue could be resolved? Everything works fine with the sample code after building the source code and running it with node

tuturis avatar Jun 30 '20 12:06 tuturis

Hey @tuturis.

Can't really tell as I tried to use the VS code debugger like three times in my life without much success, so I just didn't try again. What you can do, though, is setting this environment variable to enable debug logging: DEBUG=threads*

Apart from that… maybe someone else has an idea?

andywer avatar Jun 30 '20 15:06 andywer

Hi @andywer , the problem is that VS code debugger gets stuck in a breakpoint for each worker at the start of the file which exposes the function (in my case at least). You have to manually resume each thread within the timeout or it will throw the error commented.

And if it helps it isn't only with ts-node, i'm running ts code compiled into js

Dobloq avatar May 11 '21 14:05 Dobloq

Thanks, @Dobloq!

I see… I am not surprised that it fails with that spawn timeout error (if the debugger halts the new thread immediately, of course it cannot tell the main thread that it has initialized), I am rather surprised that it works once at all.

@tuturis Maybe you continued / skipped that breakpoint in the worker quickly enough when it was first hit and then on subsequent calls the debugger just kept the thread on hold?

andywer avatar May 11 '21 15:05 andywer

Related: https://github.com/microsoft/vscode/issues/125451

Seems like it's been closed as well! After testing my project with the nightly version of the debugger extension in VSCode it seems like I am finally able to use the debugger with workers. Looks like it wasn't just isolated to Threads.js. Try the nightly!

eliellis avatar Jul 08 '21 20:07 eliellis