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

Bug: Web workers appear to reuse same module cache #16503

Closed
dev-nicolaos opened this issue Nov 1, 2022 · 6 comments
Closed

Bug: Web workers appear to reuse same module cache #16503

dev-nicolaos opened this issue Nov 1, 2022 · 6 comments

Comments

@dev-nicolaos
Copy link
Contributor

dev-nicolaos commented Nov 1, 2022

This appears to be a regression (was previously reported in #6116). My understanding is that each web worker is supposed to its own separate module cache that is separate from the one used for the main thread and those used for other workers.

Web Worker References

Environment

OS: Windows 11 Home Version 22H2

Observed in Deno v1.27.0

Steps to reproduce

I initially observed this in a more complicated example and provide that context if desired, but for simplicity sake I reusing the steps from #6116 as I believe they illustrate the same issue and is much simpler.

  1. Create a file script.ts with the content console.log('hello').

  2. Create another file main.ts with the following content:

while(true) {
  const worker = new Worker(new URL("./script.ts", import.meta.url).href, { type: "module"});
  await new Promise(resolve => setTimeout(resolve, 1000))
  worker.terminate()
}
  1. Run this command: deno run --allow-read main.ts.

  2. Modify the content of script.ts to console.log('bye') and save it.

Expected behavior

The subsequent log of deno run --allow-read main.ts should be bye.

Observed behavior

After step 4, the subsequent log of deno run --allow-read main.ts is still hello.

Notes

Testing with JS files yielded the same result. I also tried using self.close in the worker rather than worker.terminate and again got the same result.

@dev-nicolaos dev-nicolaos changed the title Bug: Web workers appear to reuse main thread module cache Bug: Web workers appear to reuse same module cache Nov 1, 2022
@bartlomieju
Copy link
Member

I believe this behavior matches what browsers do - once you fetch the source once it won't be fetched again in the same session (or until cache expires). If you really want to reload the source you can add a query param that will cache-bust, something like: import.meta.resolve(./script.ts?cache=${new Date().getTime()})

@bartlomieju
Copy link
Member

I'm gonna close this issue for now, I suggest to use the above solution if you really need to reload sources in-process.

@NyanHelsing
Copy link

NyanHelsing commented Feb 13, 2023

I dont think that adding a date time is a valid solution because in trying this it still loads the cached module. the only way I can seem to bust the cache is by starting a new process or by doing this:

Deno[Deno.internal].core.ops.op_create_worker({ hasSourceCode: true, name: "hello", sourceCode: await Deno.readTextFile("./my-script.ts"), permissions: {}, workerType: "module", specifier: "file:///dev/null"})

...which is a nightmare because I can't easily post messages to it or anything because i haven't bothered to read how passed messages get serialized and also probably shouldn't be possible because type === "module" && hasSourceCode === true???

@NyanHelsing
Copy link

@bartlomieju Looking deeper I seem to have found the culprit is the way Deno is calculating module specifiers.

see my patch here: https://github.com/denoland/deno/pull/17755/files

@NyanHelsing
Copy link

@bartlomieju Looking deeper I seem to have found the culprit is the way Deno is calculating module specifiers.

see my patch here: https://github.com/denoland/deno/pull/17755/files

mmmm no that doesn't seem right..... 😮‍💨
I think for me workaround by reading file into a blob url gets me pretty much what I want. gotta figure out how to make that work with ts now tho, probably emit()

@vfssantos
Copy link

@bartlomieju , does this means that Deno's web worker will keep the module in memory even after it's terminated, and won't free up the memory until the main session is ended?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants