Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transferring MessagePort from worker thread does not work #15931

Open
kylecarbs opened this issue Dec 22, 2024 · 3 comments
Open

Transferring MessagePort from worker thread does not work #15931

kylecarbs opened this issue Dec 22, 2024 · 3 comments
Labels
bug Something isn't working needs triage

Comments

@kylecarbs
Copy link

What version of Bun is running?

1.1.42+50eec0025

What platform is your computer?

Linux 6.6.61 aarch64 unknown

What steps can reproduce the bug?

// main.js
const { Worker } = require("node:worker_threads")
const worker = new Worker("./worker.js")
worker.on("message", (message) => {
    console.log(message)
})
// worker.js
const {parentPort} = require("worker_threads")
const { port1 } = new MessageChannel()
parentPort.postMessage({ port1 }, [port1])

Run main.js.

What is the expected behavior?

In NodeJS, output is as-expected.

$ node main.js
{
  port1: MessagePort [EventTarget] {
    active: true,
    refed: false,
    [Symbol(kEvents)]: SafeMap(2) [Map] {
      'newListener' => [Object],
      'removeListener' => [Object]
    },
    [Symbol(events.maxEventTargetListeners)]: 10,
    [Symbol(events.maxEventTargetListenersWarned)]: false,
    [Symbol(kNewListener)]: [Function (anonymous)],
    [Symbol(kRemoveListener)]: [Function (anonymous)],
    [Symbol(nodejs.internal.kCurrentlyReceivingPorts)]: undefined
  }
}

What do you see instead?

$ bun main.js
200 |   #onClose(e) {
201 |     this.#onExitPromise = e.code, this.emit("exit", e.code);
202 |   }
203 |   #onError(event) {
204 |     let error = event?.error;
205 |       error = new Error(event.message, { cause: event });
                    ^
error: 1 | const {parentPort} = require("worker_threads")
2 | const { port1 } = new MessageChannel()
3 | parentPort.postMessage({ port1 }, [port1])
               ^
TypeError: Unable to deserialize data.
      at /tmp/buntran/worker.js:3:12

 cause: {
  "isTrusted": true
}

      at #onError (node:worker_threads:205:15)

Bun v1.1.42 (Linux arm64)

Additional information

No response

@kylecarbs kylecarbs added bug Something isn't working needs triage labels Dec 22, 2024
@kylecarbs
Copy link
Author

kylecarbs commented Dec 22, 2024

Error is coming from:

if (obj->inherits<JSMessagePort>()) {
auto index = m_transferredMessagePorts.find(obj);
if (index != m_transferredMessagePorts.end()) {
write(MessagePortReferenceTag);
write(index->value);
return true;
}
// MessagePort object could not be found in transferred message ports
code = SerializationReturnCode::ValidationError;
return true;
}

@kylecarbs kylecarbs changed the title Transfering MessagePort between worker threads doesn't work Transfering MessagePort from worker thread does not work Dec 22, 2024
@kylecarbs
Copy link
Author

Transfering a MessagePort from main.js to worker.js works, just not the other way around.

@kylecarbs
Copy link
Author

It also works when transfering a MessagePort over another MessagePort, just not when going through postMessage directly from the parentPort.

@kylecarbs kylecarbs changed the title Transfering MessagePort from worker thread does not work Transferring MessagePort from worker thread does not work Dec 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
None yet
Development

No branches or pull requests

1 participant