emscripten icon indicating copy to clipboard operation
emscripten copied to clipboard

wget calls in web worker causes refernce to worker to be lost preventing emscripten_worker_respond

Open MichaelBalazs opened this issue 11 years ago • 10 comments

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!

MichaelBalazs avatar Feb 25 '15 17:02 MichaelBalazs

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.

kripken avatar Feb 25 '15 19:02 kripken

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

MichaelBalazs avatar Feb 25 '15 20:02 MichaelBalazs

Just adding that the code runs if I comment out:

emscripten_wget(url.c_str(), _targetFileName.c_str());

Fails if I leave it in.

MichaelBalazs avatar Feb 25 '15 21:02 MichaelBalazs

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.

kripken avatar Feb 25 '15 23:02 kripken

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

MichaelBalazs avatar Mar 14 '15 14:03 MichaelBalazs

@kripken Were you able to reproduce the issue using the shared code?

MichaelBalazs avatar Mar 18 '15 13:03 MichaelBalazs

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.

MichaelBalazs avatar Mar 19 '15 17:03 MichaelBalazs

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.

kripken avatar Mar 20 '15 00:03 kripken

I removed the assertion and added a test. If I find a better solution I'll do that later.

kripken avatar Mar 20 '15 01:03 kripken

Thanks.

MichaelBalazs avatar Mar 20 '15 12:03 MichaelBalazs