Skip to content

worker_thread can not capture promise error in worker #33834

@lanjia

Description

@lanjia
  • Version 12.18:
  • Platform ox 10.15.5:
  • Subsystem:

What steps will reproduce the bug?

const { Worker, isMainThread } = require('worker_threads')

if (isMainThread) {
  const fn = () => {
    return new Promise((resolve, reject) => {
      const worker = new Worker(__filename)
      worker.on('message', resolve)
      worker.on('error', reject)
      worker.on('exit', (code) => {
        if (code !== 0)
          reject(new Error(`Worker stopped with exit code ${code}`))
      })
    })
  }
  (async () => {
    try {
      await fn()
    } catch (err) {
      console.log('main thread err:', err.message)
    }
  })()
} else {
  (async () => {
    throw new Error('worker thread err')
  })()
}

How often does it reproduce? Is there a required condition?

What is the expected behavior?

[Running] node "/code/test/workthread.js"
(node:58339) UnhandledPromiseRejectionWarning: Error: worker thread err
    at /code/test/workthread.js:24:11
    at Object.<anonymous> (/code/test/workthread.js:25:5)
    at Module._compile (internal/modules/cjs/loader.js:1200:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at MessagePort.<anonymous> (internal/main/worker_thread.js:167:24)
    at MessagePort.emit (events.js:315:20)
    at MessagePort.onmessage (internal/worker/io.js:78:8)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:58339) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:58339) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

[Done] exited with code=0 in 0.081 seconds

What do you see instead?

Additional information

Metadata

Metadata

Assignees

No one assigned

    Labels

    promisesIssues and PRs related to ECMAScript promises.workerIssues and PRs related to Worker support.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions