[html5] Web Workers: Race-Condition setting onmessage handler?

Tobias Sauerwein tobias.sauerwein at camptocamp.com
Tue Jul 27 00:46:56 PDT 2010


I am wondering what prevents a web worker from running into race-conditions
when setting the onmessage handlers. I am worried about that a web worker
posts a message before the main script has set up the onmessage handler, or
the other way around, that the web worker posts a message before the main
script has set up its onmessage handler.

I know that there is a message queue [1], but you can easily make up an
example where a message is not enqueued:

Main Script:

var worker = new Worker("webworker.js");
> worker.onmessage = function(event) {
>     console.log('onmessage ' + event.data)
> };
> worker.postMessage("start");


>     function() {
>         onmessage = function(event) {
>             postMessage("message received");
>         };
>         postMessage("done");
>     }, 1000);

The output is (in Chromium 6.0.475.0 and Firefox 4.01b, Opera 10.70 also
outputs "onmessage message received"):

onmessage done

So the "onmessage" handler of the web worker is never called.

Is this the behavior the specification requests, or is it a bug in
Chrome/Chromium and Firefox?


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.whatwg.org/pipermail/help-whatwg.org/attachments/20100727/62fb7004/attachment-0002.htm>

More information about the Help mailing list