Re: [hybi] Races in websocket API?

Maciej Stachowiak <mjs@apple.com> Mon, 23 November 2009 03:50 UTC

Return-Path: <mjs@apple.com>
X-Original-To: hybi@core3.amsl.com
Delivered-To: hybi@core3.amsl.com
Received: from localhost (localhost [127.0.0.1]) by core3.amsl.com (Postfix) with ESMTP id 72F423A6992 for <hybi@core3.amsl.com>; Sun, 22 Nov 2009 19:50:51 -0800 (PST)
X-Virus-Scanned: amavisd-new at amsl.com
X-Spam-Flag: NO
X-Spam-Score: -105.999
X-Spam-Level:
X-Spam-Status: No, score=-105.999 tagged_above=-999 required=5 tests=[BAYES_00=-2.599, J_CHICKENPOX_26=0.6, RCVD_IN_DNSWL_MED=-4, USER_IN_WHITELIST=-100]
Received: from mail.ietf.org ([64.170.98.32]) by localhost (core3.amsl.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id hx4ggxV-yTcQ for <hybi@core3.amsl.com>; Sun, 22 Nov 2009 19:50:50 -0800 (PST)
Received: from mail-out3.apple.com (mail-out3.apple.com [17.254.13.22]) by core3.amsl.com (Postfix) with ESMTP id 460EB3A6808 for <hybi@ietf.org>; Sun, 22 Nov 2009 19:50:50 -0800 (PST)
Received: from relay13.apple.com (relay13.apple.com [17.128.113.29]) by mail-out3.apple.com (Postfix) with ESMTP id E32517A7F070 for <hybi@ietf.org>; Sun, 22 Nov 2009 19:50:46 -0800 (PST)
X-AuditID: 1180711d-b7b18ae000001001-e0-4b0a0696633e
Received: from et.apple.com (et.apple.com [17.151.62.12]) by relay13.apple.com (Apple SCV relay) with SMTP id 36.BC.04097.6960A0B4; Sun, 22 Nov 2009 19:50:46 -0800 (PST)
MIME-version: 1.0
Content-transfer-encoding: 7bit
Content-type: text/plain; charset="US-ASCII"; format="flowed"; delsp="yes"
Received: from [10.0.1.5] (c-69-181-42-237.hsd1.ca.comcast.net [69.181.42.237]) by et.apple.com (Sun Java(tm) System Messaging Server 6.3-7.04 (built Sep 26 2008; 32bit)) with ESMTPSA id <0KTJ003VIMOMN070@et.apple.com> for hybi@ietf.org; Sun, 22 Nov 2009 19:50:46 -0800 (PST)
Message-id: <CC64D0CA-043C-4B38-ADF8-69219D308BF8@apple.com>
From: Maciej Stachowiak <mjs@apple.com>
To: Jamie Lokier <jamie@shareable.org>
In-reply-to: <20091120151642.GB20634@shareable.org>
Date: Sun, 22 Nov 2009 19:50:46 -0800
References: <4B04C2D6.8010908@webtide.com> <c5b3a7130911192133p1c896e0cmcc0713f0a06b4f7c@mail.gmail.com> <20091120151642.GB20634@shareable.org>
X-Mailer: Apple Mail (2.936)
X-Brightmail-Tracker: AAAAAQAAAZE=
Cc: whatwg@lists.whatwg.org, "hybi@ietf.org" <hybi@ietf.org>
Subject: Re: [hybi] Races in websocket API?
X-BeenThere: hybi@ietf.org
X-Mailman-Version: 2.1.9
Precedence: list
List-Id: Server-Initiated HTTP <hybi.ietf.org>
List-Unsubscribe: <https://www.ietf.org/mailman/listinfo/hybi>, <mailto:hybi-request@ietf.org?subject=unsubscribe>
List-Archive: <http://www.ietf.org/mail-archive/web/hybi>
List-Post: <mailto:hybi@ietf.org>
List-Help: <mailto:hybi-request@ietf.org?subject=help>
List-Subscribe: <https://www.ietf.org/mailman/listinfo/hybi>, <mailto:hybi-request@ietf.org?subject=subscribe>
X-List-Received-Date: Mon, 23 Nov 2009 03:50:51 -0000

On Nov 20, 2009, at 7:16 AM, Jamie Lokier wrote:

> John Fallows wrote:
>> Greg,
>>
>> We raised this as a potential issue during the API design for
>> WebSocket (back when it was called TCPConnection), indicating that a
>> separate method might be desirable that could be called after the
>> WebSocket was constructed and event handlers attached.
>>
>> But the decision was made that a single-threaded execution  
>> environment
>> made a separate method unnecessary, because the execution of the
>> onopen handler (for example) could not preempt the execution of the
>> following 2 lines of JavaScript code:
>>
>> var ws = new WebSocket("ws://kaazing.net/echo");
>> ws.onopen = function() { console.log("OPEN"); }
>>
>> Therefore, by the time the open event is delivered, the onopen  
>> handler
>> would have already been attached.
>
> Unfortunately the single-threaded assumption is not correct.
>
> First, it's already been mentioned that when running Javascript under
> a debugger under at least one browser, events can be sent and lost
> before the second line executes.
>
> Second, all recent browsers have multi-threaded Javascript, under
> either Google's WorkerPool API, or WHATWG's Web Workers.
>
> Web Workers have been suggested as a way to use WebSocket effectively,
> but this single-threaded assumption might make that combination
> difficult to use.  What happens when you create a new WebSocket object
> in a Web Worker?  Can that be done safely?

Short version: yes, WebSocket can work fine in a Web Worker.

Slightly longer version: "Single-threaded" is perhaps a misleading way  
to characterize the required condition. Here's a slightly more precise  
description: all WebSocket events are delivered on the logical  
"thread" of JavaScript execution that spawned them (either a UI thread  
or a Worker or what have you). The event delivery is not re-entrant -  
the event will only be sent the next time that thread of execution  
drops back into the event loop. And you can't pass a WebSocket from  
one thread to another. This means that any time you do a sequence like  
this:

   var ws = new WebSocket("ws://kaazing.net/echo");
   ws.onopen = function() { console.log("OPEN"); }

You can be guaranteed that no "open" event will fire before the  
"onopen" attribute is set - the WebSocket object cannot deliver events  
until the event loop on the thread that created it is free.

  -  Maciej

>
> I'm surprised at that, given WHATWG is involved with both WebSocket
> and Web Workers.
>
> An obvious solution is a connect() method, similar to XmlHttpRequest's
> send() method.
>
> -- Jamie
> _______________________________________________
> hybi mailing list
> hybi@ietf.org
> https://www.ietf.org/mailman/listinfo/hybi