Re: [hybi] Races in websocket API?

Jamie Lokier <jamie@shareable.org> Fri, 20 November 2009 15:16 UTC

Return-Path: <jamie@shareable.org>
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 A7D743A68CC for <hybi@core3.amsl.com>; Fri, 20 Nov 2009 07:16:59 -0800 (PST)
X-Virus-Scanned: amavisd-new at amsl.com
X-Spam-Flag: NO
X-Spam-Score: -4.299
X-Spam-Level:
X-Spam-Status: No, score=-4.299 tagged_above=-999 required=5 tests=[AWL=-2.300, BAYES_00=-2.599, J_CHICKENPOX_26=0.6]
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 8-V-qDtsuQMz for <hybi@core3.amsl.com>; Fri, 20 Nov 2009 07:16:59 -0800 (PST)
Received: from mail2.shareable.org (mail2.shareable.org [80.68.89.115]) by core3.amsl.com (Postfix) with ESMTP id D2C283A680C for <hybi@ietf.org>; Fri, 20 Nov 2009 07:16:58 -0800 (PST)
Received: from jamie by mail2.shareable.org with local (Exim 4.63) (envelope-from <jamie@shareable.org>) id 1NBVE2-0006Gd-LH; Fri, 20 Nov 2009 15:16:42 +0000
Date: Fri, 20 Nov 2009 15:16:42 +0000
From: Jamie Lokier <jamie@shareable.org>
To: John Fallows <john.fallows@kaazing.com>
Message-ID: <20091120151642.GB20634@shareable.org>
References: <4B04C2D6.8010908@webtide.com> <c5b3a7130911192133p1c896e0cmcc0713f0a06b4f7c@mail.gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
Content-Disposition: inline
In-Reply-To: <c5b3a7130911192133p1c896e0cmcc0713f0a06b4f7c@mail.gmail.com>
User-Agent: Mutt/1.5.13 (2006-08-11)
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: Fri, 20 Nov 2009 15:16:59 -0000

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?

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