-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Compatibility with comlink
for workers
#3669
Comments
comlink
comlink
for workers
Workers have a lifetime similar to Node rather than Web Try calling .ref() on the Worker. Or pass |
Also see https://bun.sh/docs/api/workers |
Hmm, interesting. Normally, to get If I ref the worker, I see the output "Counter: 0" but the program exits (with exit code 0) before EDIT: actually, it seems to be a race condition whether "Counter: 0" prints or whether the program exits before that line as well. |
More specifically, the following program exits without waiting 10 seconds and without printing an error. Somehow the execution of the main thread is being halted due to the presence of import { wrap } from "comlink";
async function init() {
try {
const worker = new Worker(await import.meta.resolve("./worker.js"), {"type": "module", bun: { ref: true }});
worker.ref()
// WebWorkers use `postMessage` and therefore work with Comlink.
const obj = wrap(worker);
console.log(`Counter: ${await obj.counter}`);
await obj.inc();
} catch (e) {
console.log("Error!", e);
} finally {
await new Promise((resolve) => setTimeout(resolve, 10000)); // sleep 10 seconds
}
}
init(); |
normally yes, but init isn’t awaited. |
Ah, I'm sorry. Good catch — I was trying to stay close to the sample code but I'm not normally used to testing code using a named |
Okay, I dug into this for an hour and unfortunately all I could determine is that the The worker remains alive and has not removed the |
there's probably a bug in bun here, due to lack of test coverage. sorry for taking up your time on this |
After 777ee4e, back and forth messages work as expected |
There is a GC bug, I suggest waiting a couple more days or until v0.7 is released |
Thanks for the fix, I'm glad to see it! 😻 I can confirm that the latest canary prints out all the expected counters. However, it now seems to have the opposite problem, where the sample program in the first comment doesn't exit, and I'm unable to fix that by trying to explicity This isn't necessarily a big issue, but I thought I'd note it. |
For example: `bun run src/bin/scramble.ts -- 333` Thanks to oven-sh/bun#3645 and oven-sh/bun#3669, we can use `bun` directly on our source code (except the WASM parts). This requires a few changes: - Move around the source code to account for the fact that `esbuild` does not have understand relative `new URL(…, import.meta.url)` or `import.meta.resolve(…)` references yet: evanw/esbuild#312 (comment) - This has the unfortunate side effect that some files have to move to the source code root. This isn't *bad* per se, but it breaks some assumptions while still relying on some other assumptions. I hope we can move the code back some time soon. - Avoid using the trampoline workaround when we seem to be in a browser environment. - Avoid assuming that the output of `await import.meta.resolve(…)` can be passed to `new URL(…)` (`bun` returns a bath without the `file:` protocol).
I did some work on |
What is the problem this feature would solve?
I'm excited to see worker support land in #3645 — the tradeoffs look pretty great!
However, I've so far been unable to use the
bun
v0.6.15 canary withcomlink
. Adapting thecomlink
README:This prints "Worker initialized." but not the counters. In fact, there is no error output, so I'm a bit stuck trying to debug.
What is the feature you are proposing to solve the problem?
Due to the lack of an error, I'm not 100% sure what would be needed to be compatible with
comlink
.comlink
has hardcoded assumptions about browsers andnode
may be causing unexpected behaviour.MessagePort
, but I'm not sure (since somepostMessage(…)
functionality is already supported).Given that
comlink
is the de facto library for exposing an async API over a worker boundary, I think it would be pretty valuable to be compatible out of the box.What alternatives have you considered?
I'm not aware of any viable alternatives to
comlink
(which has ≈10K stars on GitHub). Ifbun
were not compatible, I'd probably have to figure out how to implement custom workarounds instead of consolidating around ecosystem standards and conventions. I have a lot of experience with this, but it doesn't scale to other projects.It's also possible that small fixes to
comlink
would make it work withbun
— I don't have enough information to draft a potential PR, though.The text was updated successfully, but these errors were encountered: