-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Description
Version
20.4.0
Platform
Darwin user 22.5.0 Darwin Kernel Version 22.5.0: Thu Jun 8 22:22:20 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T6000 arm64 arm Darwin
Subsystem
worker_threads
What steps will reproduce the bug?
import {Worker} from 'node:worker_threads'
import {once} from 'node:events'
import types from 'node:util/types'
const worker = new Worker('setImmediate(() => { throw new Error("error"); })', {eval: true})
const [error] = await once(worker, 'message')
console.log(error instanceof Error) // true
console.log(types.isNativeError(error)) // false
How often does it reproduce? Is there a required condition?
Always, as long as the error is emitted by the error
event on the worker. Any errors sent via postMessage()
are identified correctly.
What is the expected behavior? Why is that the expected behavior?
types.isNativeError()
returns true
for the emitted error.
What do you see instead?
types.isNativeError()
returns false
for the emitted error.
Additional information
The problem seems to be in the error deserialization:
node/lib/internal/error_serdes.js
Line 172 in eece8d7
return ObjectCreate(ctor.prototype, properties); |
Reading the code, I believe it's roughly equivalent to:
const error = Object.create(Error.prototype, {message:{value:'foo'}})
According to the spec, merely extending the error prototype does not create a native error.
Of course, because it does extend the Error prototype, error instanceof Error
returns true
.
I don't have the context for why the serialization is so complex, but FWIW the built-in v8
serde does result in proper native error objects.
Activity
novemberborn commentedon Jul 9, 2023
One more thing, this logic here sets a string tag:
node/lib/internal/error_serdes.js
Lines 164 to 168 in eece8d7
Regular errors don't have string tags:
nivb52 commentedon Jul 10, 2023
@atlowChemi
Hi, seems interesting,
Can I work on it ?
Thanks,
Niv
atlowChemi commentedon Jul 10, 2023
@nivb52 Sure, PRs are welcomed 🙂
Track worker errors
Only treat native errors as errors