Hi!<br><br>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.<br>
<br>I know that there is a message queue [1], but you can easily make up an example where a message is not enqueued:<br><br>Main Script:<br><br><blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote">
var worker = new Worker("webworker.js");<br>worker.onmessage = function(event) {<br> console.log('onmessage ' + event.data) <br>};<br>worker.postMessage("start");<br></blockquote><br><br>
'webworker.js':<br><br><blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote">setTimeout(<br> function() {<br> onmessage = function(event) {<br>
postMessage("message received"); <br> };<br> postMessage("done");<br> }, 1000);<br></blockquote><br><br>The output is (in Chromium 6.0.475.0 and Firefox 4.01b, Opera 10.70 also outputs "onmessage message received"):<br>
<br><blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote">onmessage done<br></blockquote><br>So the "onmessage" handler of the web worker is never called.<br>
<br><br>Is this the behavior the specification requests, or is it a bug in Chrome/Chromium and Firefox?<br><br>Tobias<br><br><br><br>[1]: <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/comms.html#port-message-queue">http://www.whatwg.org/specs/web-apps/current-work/multipage/comms.html#port-message-queue</a><br>