This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 12340 - It is not clear which worker to use when using nameless SharedWorker
Summary: It is not clear which worker to use when using nameless SharedWorker
Status: RESOLVED FIXED
Alias: None
Product: WebAppsWG
Classification: Unclassified
Component: Web Workers (editor: Ian Hickson) (show other bugs)
Version: unspecified
Hardware: PC Linux
: P2 normal
Target Milestone: ---
Assignee: Ian 'Hixie' Hickson
QA Contact: public-webapps-bugzilla
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-03-19 14:10 UTC by Olli Pettay
Modified: 2011-06-21 21:24 UTC (History)
5 users (show)

See Also:


Attachments

Description Olli Pettay 2011-03-19 14:10:01 UTC
http://www.w3.org/TR/2011/WD-workers-20110310/#shared-workers-and-the-sharedworker-interface

Step 7.5 is
"If name is not the empty string and there exists a SharedWorkerGlobalScope object whose closing flag is false, whose name attribute is exactly equal to name, and whose location attribute represents an absolute URL with the same origin as scriptURL, then let worker global scope be that SharedWorkerGlobalScope object."

7.6. is 
"Otherwise, if name is the empty string and there exists a SharedWorkerGlobalScope object whose closing flag is false, and whose location attribute represents an absolute URL that is exactly equal to scriptURL, then let worker global scope be that SharedWorkerGlobalScope object."

7.8 is
"Create a new SharedWorkerGlobalScope object. Let worker global scope be this new object."


Example web page is
<script>
  var sw1 = new SharedWorker("worker.js", "1");
  sw1.port.onmessage = function(e) { alert("sw1: " + e.data); }
  var sw2 = new SharedWorker("worker.js", "2");
  sw2.port.onmessage = function(e) { alert("sw2: " + e.data); }
  var sw3 = new SharedWorker("worker.js");
  sw3.port.onmessage = function(e) { alert("sw3: " + e.data); }
</script>

and worker 
onconnect = function(e) {
  var port = e.ports[0];
  port.postMessage("worker name: " + name);
}

Since sw1 and sw2 have different names, they get separate workers per 7.5 and 7.8.

Because sw3 doesn't have name, 7.6 applies. Both sw1 and sw2 fulfill the criteria of a worker
whose closing flag is false and location attribute is the same as sw3's location, so sw3 is
connected randomly to either one of those workers.
That is very strange.


Opera seems to implement what the spec says, Chrome creates a new worker for sw3.
Comment 1 Drew Wilson 2011-04-28 18:26:18 UTC
I think that the current spec language doesn't really capture the intent properly. The start of the discussion around the optional name parameter can be found here:

http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2009-August/022050.html

The idea was that if the name is omitted, the URL itself becomes the identifier of the worker, and only matches other instances whose name is empty.

In WebKit, an existing SharedWorker resource is reused if the name matches, or if both names are omitted/empty and the URLs match.

That said, the goal of allowing names to be omitted is to provide a way for developers to avoid collisions from different pages trying to use shared workers under the same domain, where it would otherwise be difficult to coordinate the use of names. I think that either implementation (WebKit or Opera) would satisfy this requirement.

That said, I think the current WebKit implementation makes more sense. It doesn't make sense to me that:

new SharedWorker(url, "name")

and

new SharedWorker(url, "name2")

would yield distinct shared workers, but somehow the special case of:

new SharedWorker(url, "")

would be treated differently ("pick a random instance, ignoring names"). The WebKit implementation has the advantage of having deterministic behavior.
Comment 2 Ian 'Hixie' Hickson 2011-06-21 21:24:27 UTC
Yeah the intent was to still match on name, but also match on URL. Fixed. Behaviour should be deterministic where possible!
Comment 3 contributor 2011-06-21 21:24:39 UTC
Checked in as WHATWG revision r6264.
Check-in comment: Oops, checking the name got dropped somehow in the name='' case.
http://html5.org/tools/web-apps-tracker?from=6263&to=6264