From: Ben Darnell Date: Sun, 18 Aug 2013 23:24:14 +0000 (-0400) Subject: Add a distinct exception for writing a WebSocket message on a closed connection. X-Git-Tag: v3.2.0b1~91 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6ec711cded7be36079ee28047eb5ebedb5771955;p=thirdparty%2Ftornado.git Add a distinct exception for writing a WebSocket message on a closed connection. This replaces an AttributeError on NoneType. Closes #879. --- diff --git a/tornado/websocket.py b/tornado/websocket.py index 6c3b875d2..676d21bf8 100644 --- a/tornado/websocket.py +++ b/tornado/websocket.py @@ -51,6 +51,10 @@ class WebSocketError(Exception): pass +class WebSocketClosedError(WebSocketError): + pass + + class WebSocketHandler(tornado.web.RequestHandler): """Subclass this class to create a basic WebSocket handler. @@ -160,6 +164,8 @@ class WebSocketHandler(tornado.web.RequestHandler): message will be sent as utf8; in binary mode any byte string is allowed. """ + if self.ws_connection is None: + raise WebSocketClosedError() if isinstance(message, dict): message = tornado.escape.json_encode(message) self.ws_connection.write_message(message, binary=binary) @@ -195,6 +201,8 @@ class WebSocketHandler(tornado.web.RequestHandler): def ping(self, data): """Send ping frame to the remote end.""" + if self.ws_connection is None: + raise WebSocketClosedError() self.ws_connection.write_ping(data) def on_pong(self, data): @@ -210,8 +218,9 @@ class WebSocketHandler(tornado.web.RequestHandler): Once the close handshake is successful the socket will be closed. """ - self.ws_connection.close() - self.ws_connection = None + if self.ws_connection: + self.ws_connection.close() + self.ws_connection = None def allow_draft76(self): """Override to enable support for the older "draft76" protocol.