Doubt on worker termination

hello all!

have a doubt about Workers. lets consider this snipper:

// client code
var w = new Worker("myWor.js");

// in the worker
onmessage = function() {
   // blablabla


w.terminate() // in client code

the same as

self.close() // in worker ciode


I made some tests and it does look like both the instructions terminate the worker which in turn stops answering to messages but I thought to double check with you guys.


w.terminate() and self.close() can be compared when powering down a computer.
w.terminate() is like pulling the plug, and self.close() is like performing a safe shutdown.

w.terminate() doesn’t give the worker any opportunity to complete its operations or to cleanup properly, so it’s better to use self.close() as much as you can.

because self.close() triggers the onclose event where important pre-shutdown routines can be executed. is that correct?

It’s because you can script the worker to properly complete its work before the self.close() command is invoked.

thats also true.

by the way have u managed to have the onclose event to fire at all? it is in the spec and it should be fired when calling close() but it looks like its not working. Or maybe am doing something wrong :wink:

While I haven’t made use of web workers myself, I’m not aware of any bugs relating to the firing of a websocket’s onclose event.

Could it be that you’re assuming that the onclose event fires when one of the web workers closes itself? I don’t think that is the case.

I am indeed assuming exactly that! when else should that event be triggered if not when a worker calls close()?


The web socket’s onclose event fires when the web socket is closed. Not one of the workers, but the complete socket itself.

The worker itself doesn’t currently have an onclose event yet on anything but gecko-based web browsers.

ok so to summarize: close() and terminate() do exactly the same thing. Only if u use close() it means u r IN the thread and can therefore do some operations before shutting down which u cant do from OUTSIDE (where u can instead use terminate).

but technically they do the same thing.


by the way I have an ex colleague from christchurch. howz it going downunder? :wink:

No, but you get on to it further here.

Landing an aircraft is very different from having jet fighters bomb it out of the sky. Sure, technically the aircraft ends up on the ground in both situations, but one (self.close()) is very much the preferred option over the other (worker.terminate())

We’re still shaken. It’s likely to take a whole generation for the place to return to mostly it was. Not in 5 years, some in 10, many parts in 15, most in 20.

shame man! oh well better than the poor japan I suppose … good luck anyway kiwiland! :tup: