ERR_WORKER_UNSERIALIZABLE_ERROR node worker_threads ai_generated true

Error [ERR_WORKER_UNSERIALIZABLE_ERROR]: Serializing an uncaught exception failed

ID: node/node-worker-thread-crash

Also available as: JSON · Markdown
85%Fix Rate
88%Confidence
50Evidence
2023-06-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
20 active

Root Cause

A worker thread crashed with an error that could not be serialized and sent back to the parent thread via the structured clone algorithm.

generic

Workarounds

  1. 90% success Add proper error handling in the worker and serialize errors before throwing
    In the worker file, wrap logic in try-catch and use parentPort.postMessage({error: err.message}) instead of letting exceptions propagate

    Sources: https://nodejs.org/api/worker_threads.html

  2. 88% success Listen for 'error' and 'exit' events on the Worker instance in the parent
    worker.on('error', (err) => { /* handle */ }); worker.on('exit', (code) => { if (code !== 0) /* restart or log */ });

    Sources: https://nodejs.org/api/worker_threads.html#event-error

Dead Ends

Common approaches that don't work:

  1. Wrapping the entire worker in a try-catch without handling the error event on the parent 80% fail

    Uncaught exceptions in worker threads are not caught by try-catch in the parent; you must listen for the 'error' event on the Worker instance

  2. Restarting the worker in an infinite loop without addressing the root cause 75% fail

    Creates a crash loop that consumes resources and masks the underlying bug

Error Chain

Leads to:
Preceded by:
Frequently confused with: