Skip to content

Worker bug: parentPort.postMessage in worker_threads #46350

@cyriltil

Description

@cyriltil

Version

v16.14.0

Platform

Microsoft Windows NT 10.0.22000.0 x64

Subsystem

No response

What steps will reproduce the bug?

Worker Thread mixes up large json object passed from Worker back to the parentPort (both imported from worker_threads). During the way the object (usually ~1MB large) is accidently changed (some sub-branches)).

Workaround: If the object is stringified in the Worker and parsed in the parent, then it works fine.

Problem:

//workerCode.mjs
import {parentPort } from 'worker_threads';
console.log('sending', someJsonObject)
parentPort.postMessage(someJsonObject);

//main.mjs
import { Worker } from 'worker_threads';
const worker = new Worker('./workerCode.mjs', { });
worker.on('message', jsonObject => console.log('receiving', someJsonObject) )

sending and receiving object should be the same, but it is not (for some large objects with nesting subobjects, some branches are randomly replaced by completely different objects.

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

Happens only for specific objects, not for any json object. But for the certain object it happens always.

What is the expected behavior?

It is expected that the sender and receiver does not affect the object that is being sent.

What do you see instead?

received object is randomly accidentally corrupted (some branches are replaced by unrelated data)

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    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