comlink icon indicating copy to clipboard operation
comlink copied to clipboard

Comlink hangs indefinitely when loading in an async worker context that it hasn't loaded

Open onzag opened this issue 5 years ago • 2 comments

During a transpilation event using webpack, a file named the injector is created, because my code is splitted, and my worker code shares with the app code, the injector loads asynchronously the content of these files.

eg in my app code.

const fancyWorker = wrap<MyFancyWorker>(new Worker("/resources/fancyworker.injector.js"))

The injector then loads asynchronously

resources/commons.js and resources/fancyworker.js where commons contains code in common and fancyworker contains the whatever is specific to the worker.

if I then do before all the others are loaded:

await fancyWorker.myMethod()

It will just hang forever.

This doesn't occur when using sync importScripts, but only when loading async using fetch; this is a very hard to debug error as, unless you call a function at once and not by user input, everything will work out normal, until your network is slow and at the same time the cache was invalidated.

While this as of now is an edge case, I expect that as webpack keeps developing forwards the use of async loading in workers becomes more commonplace.

I'd consider an option to pass an alternate worker eg...

const fancyWorker = wrap<MyFancyWorker>(new ComlinkManuallyActivatedWorker(new Worker("/resources/fancyworker.injector.js")))

And in my fancy injector once I know that it's ready and accepting.

comlink.ready()

onzag avatar May 22 '20 13:05 onzag

I am getting this issue as well, and cannot find a workaround.

charlieforward9 avatar Oct 12 '23 16:10 charlieforward9

I somehow found a workaround for this last year and regretfully did not document how I did it. I have to refactor the code now and I am running into this problem again.

charlieforward9 avatar Apr 30 '24 19:04 charlieforward9