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

Discrepancy in Bun vs Node parentPort.on in async context #15408

Open
rockisch opened this issue Nov 25, 2024 · 0 comments
Open

Discrepancy in Bun vs Node parentPort.on in async context #15408

rockisch opened this issue Nov 25, 2024 · 0 comments
Labels
bug Something isn't working needs triage

Comments

@rockisch
Copy link

What version of Bun is running?

1.1.36

What platform is your computer?

Darwin 22.4.0 arm64 arm

What steps can reproduce the bug?

Create 2 files in the same folder:

a.js

import { Worker } from "node:worker_threads";

const worker = new Worker("./src/b.js", {});
worker.postMessage("");

b.js

import { parentPort } from "node:worker_threads";

parentPort.on("message", () => {
  console.log("received message");
});

while (true) {
  // some sync code...
  await new Promise((r) => setImmediate(r));
}

Run:

bun a.js

What is the expected behavior?

I expect to see the line in the callback being printed. Here's what happens when running this program through Node:

$ node src/a.js
received message

What do you see instead?

The program never prints anything, supposedly because the callback is never called in the first place.

Additional information

If you move the hot loop inside a function, and call it without awaiting it on the top-level code, it seems like the issue 'gets fixed', which leads me to believe Bun is probably treating top-level async/await differently.

As in:

async function a() {
  while (true) {
    // some sync code...
    await new Promise((r) => setImmediate(r));
  }
}
a();
@rockisch rockisch added bug Something isn't working needs triage labels Nov 25, 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