Skip to content

[worker_threads]: Main thread receives Error object when the worker throws a primitive value #35506

@suin

Description

@suin
  • Version: v14.7.0
  • Platform: Darwin suin 18.7.0 Darwin Kernel Version 18.7.0: Mon Feb 10 21:08:45 PST 2020; root:xnu-4903.278.28~1/RELEASE_X86_64 x86_64
  • Subsystem: N/A

What steps will reproduce the bug?

Summary: Since Node v14.7.0, when primitive values are thrown in a worker, the main thread receives it as Error objects.

I'm not sure this is actually a bug. But I couldn't find out the information about this breaking change. So I have created this issue. If this is an expected change, please close this issue.

// main.js
const { Worker } = require('worker_threads')
const worker = new Worker('./worker.js')
console.log(process.versions.node)
worker.on('error', err => {
  console.error(err)
})
// worker.js
throw 'Error was thrown in worker'

in Node v14.6.0:

$ node main.js
14.6.0
Error was thrown in worker

in Node v14.7.0:

$ node main.js
14.7.0
Error [ERR_UNHANDLED_ERROR]: Unhandled error. ('Error was thrown in worker')
    at process.emit (events.js:303:17)
    at emitUnhandledRejectionOrErr (internal/event_target.js:541:11)
    at MessagePort.[nodejs.internal.kHybridDispatch] (internal/event_target.js:356:9)
    at MessagePort.exports.emitMessage (internal/per_context/messageport.js:18:26) {
  code: 'ERR_UNHANDLED_ERROR',
  context: 'Error was thrown in worker'
}

Other primitives:

Undefined

// worker.js
throw undefined
$ node main.js
14.6.0
undefined

$ node main.js
14.7.0
Error [ERR_UNHANDLED_ERROR]: Unhandled error. (undefined)
    at process.emit (events.js:303:17)
    at emitUnhandledRejectionOrErr (internal/event_target.js:541:11)
    at MessagePort.[nodejs.internal.kHybridDispatch] (internal/event_target.js:356:9)
    at MessagePort.exports.emitMessage (internal/per_context/messageport.js:18:26) {
  code: 'ERR_UNHANDLED_ERROR',
  context: undefined
}

Number

// worker.js
throw 0
$ node main.js
14.6.0
0

$ node main.js
14.7.0
Error [ERR_UNHANDLED_ERROR]: Unhandled error. (0)
    at process.emit (events.js:303:17)
    at emitUnhandledRejectionOrErr (internal/event_target.js:541:11)
    at MessagePort.[nodejs.internal.kHybridDispatch] (internal/event_target.js:356:9)
    at MessagePort.exports.emitMessage (internal/per_context/messageport.js:18:26) {
  code: 'ERR_UNHANDLED_ERROR',
  context: 0
}

Symbol

// worker.js
throw Symbol('this is a symbol')
$ node main.js
14.6.0
Symbol(this is a symbol)

$ node main.js
14.7.0
Error [ERR_UNHANDLED_ERROR]: Unhandled error. (Symbol(this is a symbol))
    at process.emit (events.js:303:17)
    at emitUnhandledRejectionOrErr (internal/event_target.js:541:11)
    at MessagePort.[nodejs.internal.kHybridDispatch] (internal/event_target.js:356:9)
    at MessagePort.exports.emitMessage (internal/per_context/messageport.js:18:26)

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

There is no condition.

What is the expected behavior?

I'm not sure which is valid behavior, but at point of view of backward compatibilities, it would be the expected behavior that the main thread receives the unhandled primitive error as the primitive value instead of the Error object like the Node v14.6.0 behavior.

What do you see instead?

The main thread gets the Error object.

Additional information

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues with confirmed bugs.eventtargetIssues and PRs related to the EventTarget implementation.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