-
-
Notifications
You must be signed in to change notification settings - Fork 19
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
new Database()
in Web Worker freezing
#125
Comments
I think this might be because this module loads asynchronously, and by the time you postMessage to that Worker, module hasn't really evaluated by then. This is because the dlopen we use from |
tks, I think I got it, closing this issue
|
Note that ES module evaluation is asynchronous. Normally it just resolves quickly, but in cases where we use top-level await, it will not be asynchronous. That is why we have to await the dynamic imports. While it may not look asynchronous here, import { Database } from "jsr:@db/sqlite@0.11"; But it is actually awaiting the module evaluation of
|
thanks for your detailed explanation. I think the way that put listener on Here I have draw a chart showing my concerning By chartAs what's showing in this chart,
As they being executed in separated thread in parallel, time Flawed Code ExampleHere is a flawed code example, I use sleep for simulating main thread's slowness. main.ts const sleep = (ms: number) => new Promise(r => setTimeout(r, ms));
const worker = new Worker(import.meta.resolve("./worker.ts"), {
type: "module"
});
// simulate main thread slowness
await sleep(200);
const worker_ready = () => new Promise<void>((resolve, _reject) => {
const handleEvnet = (e: MessageEvent) => {
const { msg } = e.data;
console.log(`main.ts received msg: ${msg}`);
if (msg === "ready") {
resolve();
worker.removeEventListener("message", handleEvnet);
}
}
worker.addEventListener("message", handleEvnet);
});
// make sure worker ready
await worker_ready();
// then post some msg to worker
worker.postMessage({ msg: "main.ts msg"}); worker.ts const thisWorker = self as unknown as Worker;
thisWorker.onmessage = e => {
const { msg } = e.data;
console.log(`worker.ts received msg: ${msg}`);
thisWorker.close();
}
thisWorker.postMessage({ msg: "ready"}); ConclusionAs what's showing above, the effectiveness of awaiting worker's There should be a way that we can register initial ready listener for EDIT: Learnt from The Basics of Web Workers, worker only starts by calling the |
What happend
adding
const db = new Database('test.db')
in worker.ts makes process freezingEnvironment info
sqlite version
3.42.0
Macbook Pro 2020
Minimal example
Here is the minimal reproducible example
main.ts
worker.ts
run code with the following command
then only
msg posted
is shown on the screen, and process just hang, expecting shownreceived msg1
and exiting as usual.The text was updated successfully, but these errors were encountered: