Description
Making asynchronous calls from a worker procedure will lead to incorrect and duplicate responses being sent to the main thread.
For example:
// worker
void workerProc(int8_t* data, int size) {
myAsyncLoadFromIndexDB( [](void* result, int resultSize) {
emscripten_worker_respond_provisionally(result, resultSize);
}, data);
}
// main
emscripten_call_worker(myWorker, "workerProc", "name", 5, nameCallback, null);
emscripten_call_worker(myWorker, "workerProc", "addr", 5, addrCallback, null);
...when run, the callback "addrCallback" will be called for both responses.
The problem is that an emscripten worker uses a single global variable, workerCallbackId, to map incoming to outgoing messages. If you post two consecutive messages to the worker, workerCallbackId will get overwritten by the second call before either of the worker's async calls have finished, so they will both end up using the second callback function and user argument.
We fixed this in our own code by modifying the worker prototype and emscripten_worker_respond* functions to take an additional parameter, 'int callbackId', and it's the responsibility of the worker invocation to pass that index through to its response.
This isn't a backwards compatible fix of course -- any suggestion for a better approach that I could implement and submit?