I see, thanks for the details!<div><br></div><div>This makes sense - you let the worker run unrestricted (ports still send messages, sync API work) until it exits JS code. It is one of two possibilities I though of as well (run unrestricted while in JS or immediate termination).<div>
<br></div><div>BTW, the current Worker spec and WebKit do not have 'onclose'. I'm curious if FF plans to keep it. I've tried to find relevant discussion on exact reasons it was removed but it hides from me...</div>
<div><br></div><div>Dmitry<br><div><br></div><div><div class="gmail_quote">On Wed, Mar 31, 2010 at 10:03 AM, ben turner <span dir="ltr"><<a href="mailto:bent.mozilla@gmail.com">bent.mozilla@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Hi,<br>
<br>
When implementing the close() function for Firefox we chose to set the<br>
closing flag and clear pending events only. As the worker script is<br>
calling close() on itself we figured that the worker should retain<br>
maximum functionality until it has finished execution (otherwise it<br>
could just not call close() and rely on some kind of postMessage() and<br>
terminate() combo). Therefore we do not enforce any timeout for the<br>
currently executing script and we continue to allow postMessage()<br>
calls and synchronous XHR to proceed. Since the closing flag is set in<br>
response to close() the worker is guaranteed to finish as soon as the<br>
currently running script finishes. We always enforce a timeout for any<br>
code that runs in response to the close event that gets fired after<br>
the current script finishes, though.<br>
<br>
If the code that calls close() never returns (like the while(1) { }<br>
example above) then the worker will never finish, as pointed out<br>
above, but that's no different than having a worker script that<br>
consists only of a while(1) { } loop and we don't think it's important<br>
to prevent. If a worker script is written in this way then a<br>
terminate() call is still a valid solution.<br>
<br>
Also, since we try to retain maximum functionality after close() we<br>
also allow errors to propagate as shown above.<br>
<br>
If anyone is curious the basic strategy we use in response to close<br>
functions (like close(), terminate(), and for UA-generated events like<br>
when the main worker object is GC'd) can be found in the following<br>
table:<br>
<br>
<a href="http://mxr.mozilla.org/mozilla-central/source/dom/src/threads/nsDOMWorker.h#202" target="_blank">http://mxr.mozilla.org/mozilla-central/source/dom/src/threads/nsDOMWorker.h#202</a><br>
<font color="#888888"><br>
-Ben<br>
</font><div><div></div><div class="h5"><br>
On Tue, Mar 30, 2010 at 6:38 PM, Dmitry Titov <<a href="mailto:dimich@chromium.org">dimich@chromium.org</a>> wrote:<br>
><br>
> On Tue, Mar 30, 2010 at 5:58 PM, Drew Wilson <<a href="mailto:atwilson@google.com">atwilson@google.com</a>> wrote:<br>
>><br>
>> I'll note that the spec gives the UA an significant amount of latitude<br>
>> about its behavior after close() is called:<br>
>> User agents may invoke the "kill a worker" processing model on a worker at<br>
>> any time, e.g. in response to user requests, in response to CPU quota<br>
>> management, or when a worker stops being an active needed worker if the<br>
>> worker continues executing even after its closing flag was set to true.<br>
>> Essentially, UAs can kill a worker at any time, and since the "kill a<br>
>> worker" algorithm allows UAs to abort the script after a user-agent-defined<br>
>> amount of time (including zero), it seems like almost any behavior<br>
>> post-close is compliant. This seems like a guaranteed source of<br>
>> cross-browser incompatibilities.<br>
><br>
> Yes, and this, after many hours of troubles, may eventually crystallize into<br>
> "don't have any code after close() in your worker code" rule-of-thumb for<br>
> web developers. Which is basically a bad thing...<br>
> This is why it could be better to specify explicitly that either execution<br>
> is immediately terminated or it runs until JS exits with full functionality<br>
> of the worker, not in a special almost-closed mode. Having a timeout<br>
><br>
>><br>
>> I've always operated under the impression that the intent of the spec is<br>
>> to allow pending worker operations to complete, but still give UAs the<br>
>> ability to abort scripts that don't exit in a timely manner (so close()<br>
>> should not immediately abort the script), but I don't see anything in the<br>
>> spec regarding this.<br>
>> For #2 below, I believe that exceptions in worker context should *always*<br>
>> be reported, regardless of closing state. Section 4.6 (Runtime script<br>
>> errors) makes no mention of tying this behavior to the closing flag.<br>
><br>
> This applies as long as the browser still executes the code after close().<br>
> In WebKit V8 case, we terminate execution almost instantly, so we don't even<br>
> run the code that causes an error :-) If we are not to terminate execution<br>
> instantly, then it'd be nice to say the script runs until it ends (or parent<br>
> document closes, which actually terminates the script forcefully), and not<br>
> have a requirement to stop the queue or disconnect ports, as to not create a<br>
> whole new 'mode of worker execution' which needs to have a spec on its own<br>
> since all other features will need to be mentions (like sync APIs).<br>
> It's not clear why a fixed timeout would be useful. It would create some<br>
> randomness in what a worker can still do after calling close(). I think web<br>
> developers would then rather do those things before calling close(), to<br>
> avoid random results.<br>
> But if we say close() lets script run until completion (but prevents further<br>
> messages/events from dispatching), then perhaps we don't need it at all -<br>
> there is nothing then that script in the worker can not do to the same<br>
> effect (unregister onmessage, clear timers etc).<br>
> That means letting worker to call close() on itself only makes additional<br>
> sense if it is specified as immediate termination. It could be useful and it<br>
> can be specified in deterministic manner.<br>
> On a separate note, I agree with giving workers some time before terminating<br>
> them in case the parent page closes - but this is the case when forces<br>
> outside and async to the worker need to close it. I think<br>
> it's different when worker's own script wants to terminate - it could do its<br>
> last wishes before calling close() as well.<br>
><br>
>><br>
>> -atw<br>
>><br>
>><br>
>> On Tue, Mar 30, 2010 at 4:44 PM, Dmitry Titov <<a href="mailto:dimich@chromium.org">dimich@chromium.org</a>> wrote:<br>
>>><br>
>>> Hi!<br>
>>> Trying to fix some bugs for Workers, I've got some questions about<br>
>>> close() method on WorkerGlobalScope.<br>
>>> In particular, the spec seems to imply that after calling close() inside<br>
>>> the worker, the JS does not get terminated right away, but rather continue<br>
>>> to execute, while an internal 'closing' flag is set and a message<br>
>>> queue associated with the worker discards all the tasks, existing and<br>
>>> future. Also, all ports are immediately disentangled.<br>
>>> This seems to leave some questions without explicit answer, with<br>
>>> differences in current implementations:<br>
>>> 1. Does this code in a worker continues looping until the parent page<br>
>>> unloads:<br>
>>>  ...<br>
>>>  close();<br>
>>>  while(true) {}<br>
>>> WebKit V8 terminates, WebKit JCS terminates after a timeout, FF does not<br>
>>> terminate.<br>
>>> 2. Do the errors propagate back to Worker object after close()?<br>
>>> ...<br>
>>> close();<br>
>>> nonExistingFunction();  <<-- throws, if not processed locally, posts<br>
>>> error info to the Worker object.<br>
>>> In WebKit and FF errors propagate, although it does not seem consistent<br>
>>> while worker closed all the ports and is in a dormant state.<br>
>>> 3. Should synchronous operations work after close()? Asynchronous ones<br>
>>> perhaps should not, because of the event loop queue which is stopped...<br>
>>> ...<br>
>>> close();<br>
>>> xhr.open("GET", "<a href="http://foo.com" target="_blank">foo.com</a>", false);<br>
>>> xhr.send();<br>
>>> WebKit: does not work (mostly), FF - does work.<br>
>>> Perhaps it would be simpler to either say nothing is<br>
>>> executed/posted/fired after close() (immediate termination), or to enable<br>
>>> worker run unimpeded (with ports open, etc) until it naturally yields from<br>
>>> JS.<br>
>>> Any opinions?<br>
>>> Thanks,<br>
>>> Dmitry<br>
>>><br>
>><br>
><br>
><br>
</div></div></blockquote></div><br></div></div></div>