Here is my best guess about what is happening. By publishing a message from a web worker every 1 ms, you require that the main thread process each sent message for 1 ms.
If the main thread cannot process the message within 1 ms, you still send it a new message, even if it has not finished processing the last message. I would suggest that this puts it in a queue of messages awaiting processing.
Now that you are sending messages from a web worker faster than they can be processed, this queue of unprocessed messages will become more and more. At some point, Chrome is going to crack its hands and say: โThere are too many messages in the queue,โ and instead of queuing up new messages for processing, he omits them.
That is why if you use a reasonable number in a timeout such as 100 ms, the message has enough time to process before sending the next message, and there are no problems with the raw messages.
I created jsFiddle where the worker sends the message to the main thread and the main thread sends the message back to the worker. If this process does not occur before the next message is sent, the counters in both threads will be incompatible, and the web worker will exit.
http://jsfiddle.net/meovfpv3/3/
You can see that with a reasonable setTimeout of 100 ms, all messages have enough time to process until the next message appears.
When you reduce the setTimeout value to 1 ms, the message chain does not have time to finish before the next message is sent, and the counters in each thread will eventually be canceled, disabling the if clause and shutting down the web worker.
One way to fix this problem is instead of a blind message message every 1 ms, if the latter was processed or not, only after a new message after receiving the message back from the main stream. This means that you send messages only quickly, as the main thread can handle them.
For completeness, here's a copy of the JSFiddle code:
Working:
var counter2 = 0; var rcvd = true; function hi() { counter2++; console.log("") console.log("postMessage", counter2) postMessage(counter2); if (!rcvd) { self.close(); console.log("No message received"); } rcvd = false; setTimeout(hi, 1); } hi(); onmessage = function(e) { rcvd = true; console.log("secondMessage", e.data); }
Main:
var ww = document.querySelector('script[type="text/ww"]'), code = ww.textContent, blob = new Blob([code], {type: 'text/javascript'}), blobUrl = URL.createObjectURL(blob), worker = new Worker(blobUrl), counter = 0; worker.onmessage = function(e) { counter++; console.log("onmessage:", counter); worker.postMessage(e.data); }