wget calls in web worker causes refernce to worker to be lost preventing emscripten_worker_respond
If I try to do an wget call (emscripten_async_wget2 or emscripten_wget) within a web worker, I am no longer able to call emscripten_worker_respond without getting this error:
InternalError: uncaught exception: not in worker call!
Related Question: When spawning web workers is it better, from a performance standpoint, to have a single long lived worker or several short lived ones. I can chunk the problem either way.
Thanks!
cc @floooh
I suppose what is happening here is that the worker call exits, and then the async wget callback happens later. So it is in fact no longer in a worker call, and can't respond.
You can still use normal message passing, just you can't use the "synchronous" worker_respond call.
You might actually be able to work around this with the emterpreter, and replacing the emscripten_async_wget with a synchronous emscripten_wget, see part 3 of https://hacks.mozilla.org/2015/02/synchronous-execution-and-filesystem-access-in-emscripten/ But that's a big hammer to use here.
Thanks Alon, I have tried emscripten_wget and faced the same problem which is why I filled the issue. I can actually call emscripten_async_call and if it returns before the "main" function call returns it works. This leads me to believe that in general, if the worker call exits then worker_respond will fail. However, this is (shouldn't be) the case with emscripten_wget.
For normal message passing, is there an API for that at the emscripten level or just the JS level.
Thanks, Michael
Just adding that the code runs if I comment out:
emscripten_wget(url.c_str(), _targetFileName.c_str());
Fails if I leave it in.
And you did test the synchronous version with the worker compiled using the emterpreter option? Can you perhaps make a testcase of that, so I can test it locally - we should make that work.
Sorry for the delay, I have had a chance to test the emterpreter option and still no luck. I have create a very simple test which you can find here: https://www.dropbox.com/s/8smjp0ipinv04sd/WebWorkerTestForKripken.zip?dl=0 Details are in a README file.
In it I simply call emscripten_sleep and this breaks the emscripten_worker_respond. Here is the output:
*** Error with call to emscripten_sleep in worker ***
"pre-main prep time: 23 ms" a.html:1245:12
"Web Workers Running: 1" a.html:1233:12
"exit(0) called, but noExitRuntime, so not exiting (you can use emscripten_force_exit, if you want to force a true shutdown)" a.html:1245:12
: Successfully compiled asm.js code (total compilation time 61ms; unable to store in cache due to internal error (consider filing a bug)) worker.js
"PRE CHECK" worker.js:89:3
InternalError: uncaught exception: not in worker call! <unknown>
"POST CHECK" worker.js:89:3
@kripken Were you able to reproduce the issue using the shared code?
Further investigation links the problem to https://github.com/kripken/emscripten/blob/fdf24b478e1b26c0362a1627aca49ef82fd53f6a/src/library_browser.js#L1259
Simply commenting this line out resolves all issues.
Looking at https://github.com/kripken/emscripten/blob/fdf24b478e1b26c0362a1627aca49ef82fd53f6a/src/postamble.js#L296
Given the narrow scope that inWorkerCall appears to be used, I was unable to determine its purpose.
inWorkerCall is used to verify that the respond() call was done synchronously. The emterpreter makes that a little harder to figure out, though. I'll think some more if there is a nice way to solve this, otherwise, perhaps we should just remove the assert.
I removed the assertion and added a test. If I find a better solution I'll do that later.
Thanks.