Skip to content

Conversation

@whitphx
Copy link
Contributor

@whitphx whitphx commented Nov 22, 2025

Description

MessagePort objects can be transferred to another process such as a worker and can be accessed via events.ports as below, but it's not working now. This PR fixes it.

// Main thread
const worker = new MyWorker()
const channel = new MessageChannel()
worker.postMessage('some-data', [channel.port2]) // <- `channel.port2` is of type `MessagePort` and transferred to the worker.
// Worker thread
self.onmessage = (e) => {
  const port = e.ports[0] // <- The transferred `MessagePort` object should be accessed as `e.ports[i]`, however, it's `undefined` now.
  if (port) {
    port.postMessage("data from worker")
  }
}

Refs:

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.
  • Please check Allow edits by maintainers to make review process faster. Note that this option is not available for repositories that are owned by Github organizations.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

MessagePort objects can be transferred to another process such as a worker and can be accessed via events.ports
as written in https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent/ports
> The ports read-only property of the MessageEvent interface is an array of MessagePort objects containing all MessagePort objects sent with the message, in order.
Refs: https://developer.mozilla.org/en-US/docs/Web/API/MessageChannel, https://github.com/mdn/dom-examples/tree/9a8acd0da2417372655b22d5a0ced768e76deb1c/channel-messaging-basic
@whitphx whitphx marked this pull request as ready for review November 22, 2025 13:55
@whitphx whitphx changed the title fix(web-worker): transfer MessagePort objects to worker as event.ports fix(web-worker): MessagePort objects passed to Worker.postMessage are available as event.ports in the worker thread Nov 22, 2025
@AriPerkkio AriPerkkio merged commit 9d1763d into vitest-dev:main Nov 24, 2025
13 of 16 checks passed
@whitphx whitphx deleted the web-worker-pass-message-port branch November 24, 2025 07:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants