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

MessageChannel not working between Workers in Bun #17039

Open
luckyyyyy opened this issue Feb 4, 2025 · 0 comments
Open

MessageChannel not working between Workers in Bun #17039

luckyyyyy opened this issue Feb 4, 2025 · 0 comments
Labels
bug Something isn't working needs triage

Comments

@luckyyyyy
Copy link

luckyyyyy commented Feb 4, 2025

What version of Bun is running?

1.2.2

What steps can reproduce the bug?

When trying to establish communication between two Workers using MessageChannel in Bun, the messages are not being transmitted between the Workers, while the same code works correctly in Node.js.

Reproduction

import { Worker, isMainThread, parentPort, MessageChannel, threadId } from 'worker_threads';
import { fileURLToPath } from 'url';

if (isMainThread) {
  const worker1 = new Worker(fileURLToPath(import.meta.url));
  const worker2 = new Worker(fileURLToPath(import.meta.url));
  const { port1, port2 } = new MessageChannel();

  worker1.postMessage({ port: port1, workerId: 1 }, [port1]);
  worker2.postMessage({ port: port2, workerId: 2 }, [port2]);

  worker1.on('message', (msg) => console.log('Main thread received from worker1:', msg));
  worker2.on('message', (msg) => console.log('Main thread received from worker2:', msg));
} else {
  let workerPort;
  let myWorkerId;

  parentPort.on('message', ({ port, workerId }) => {
    workerPort = port;
    myWorkerId = workerId;

    workerPort.on('message', (msg) => {
      console.log(`Worker ${myWorkerId} received:`, msg);
      workerPort.postMessage(`Reply from worker ${myWorkerId}`);
    });

    workerPort.postMessage(`Hello from worker ${myWorkerId}`);
  });

  parentPort.postMessage(`Worker ${threadId} started`);
}

Expected Behavior

  • Workers should be able to communicate with each other through MessageChannel ports
  • Messages sent through MessageChannel ports should be received by the other Worker
  • Similar behavior to Node.js implementation

Actual Behavior

  • Messages are not being transmitted between Workers
  • No errors are thrown
  • Communication does not work as expected

Environment

  • Bun Version: 1.2.2
  • Operating System: macOS

Additional Context

  • The same code works correctly in Node.js
  • This affects the ability to implement direct Worker-to-Worker communication in Bun applications
@luckyyyyy luckyyyyy added bug Something isn't working needs triage labels Feb 4, 2025
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