Skip to content

DOMException [DataCloneError]: Object that needs transfer was found in message but not listed in transferList #9927

@vitonsky

Description

@vitonsky

Describe the bug

The code in package web-worker breaks the MessagePort transfer into the worker, so any packages that relies on that behaviour like a comlink - does not work.

The code

const transferWithoutPorts = transfer?.filter( // `ports` must be excluded from the `transfer` option passed to `structuredClone` to keep the MessagePort objects working correctly in the same thread.
t => !(t instanceof MessagePort),
)

The code were added in #9118

User do not see any warning or error. I have spend a few hours to debug my code before I've go to the web-worker package code.

When i enable the debug logs i see the error message

create message event, using native structured clone
failed to clone message, dispatch "messageerror" event: DOMException [DataCloneError]: Object that needs transfer was found in message but not listed in transferList

Reproduction

The worker code

self.addEventListener('message', (evt) => {
	self.postMessage("received");
});

The test code

import MyWorker from './MyWorker?worker';

test('Worker can receive a MessagePort', async () => {
	const worker: Worker = new MyWorker();

	const { port1 } = new MessageChannel();

	const onMessage = vi.fn();
	worker.addEventListener('message', onMessage);

	worker.postMessage({ data: port1 }, [port1]);

	await vi.waitUntil(() => onMessage.mock.calls.length);
	expect(onMessage).toHaveBeenCalled();
}, 500);

System Info

System:
    OS: Linux 6.19 Fedora Linux 43 (KDE Plasma Desktop Edition)
    CPU: (16) x64 AMD Ryzen 7 7745HX with Radeon Graphics
    Memory: 12.23 GB / 38.84 GB
    Container: Yes
    Shell: 5.3.0 - /bin/bash
  Binaries:
    Node: 22.21.1 - /home/username/.asdf/installs/nodejs/lts/bin/node
    npm: 10.9.4 - /home/username/.asdf/plugins/nodejs/shims/npm
  Browsers:
    Firefox: 148.0.2
    Firefox Developer Edition: 148.0.2
  npmPackages:
    @vitest/web-worker: ^4.1.0 => 4.1.0 
    vitest: ^4.0.18 => 4.1.0

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions