Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions tornado/test/websocket_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,11 @@ def test_write_after_close(self):
with self.assertRaises(WebSocketClosedError):
ws.write_message("hello")

@gen_test
def test_reference_self_after_been_closed(self):
ws = yield self.ws_connect("/close_reason")
self.assertIs(ws.connect_future, None)

@gen_test
def test_async_prepare(self):
# Previously, an async prepare method triggered a bug that would
Expand Down
20 changes: 13 additions & 7 deletions tornado/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -1360,7 +1360,9 @@ def __init__(
subprotocols: Optional[List[str]] = None,
resolver: Optional[Resolver] = None,
) -> None:
self.connect_future = Future() # type: Future[WebSocketClientConnection]
self.connect_future = (
Future()
) # type: Union[Future[WebSocketClientConnection], None]
self.read_queue = Queue(1) # type: Queue[Union[None, str, bytes]]
self.key = base64.b64encode(os.urandom(16))
self._on_message_callback = on_message_callback
Expand Down Expand Up @@ -1437,11 +1439,12 @@ def close(self, code: Optional[int] = None, reason: Optional[str] = None) -> Non
self.protocol = None # type: ignore

def on_connection_close(self) -> None:
if not self.connect_future.done():
if self.connect_future and not self.connect_future.done():
self.connect_future.set_exception(StreamClosedError())
self._on_message(None)
self.tcp_client.close()
super().on_connection_close()
self.connect_future = None

def on_ws_connection_close(
self, close_code: Optional[int] = None, close_reason: Optional[str] = None
Expand All @@ -1451,7 +1454,7 @@ def on_ws_connection_close(
self.on_connection_close()

def _on_http_response(self, response: httpclient.HTTPResponse) -> None:
if not self.connect_future.done():
if self.connect_future and not self.connect_future.done():
if response.error:
self.connect_future.set_exception(response.error)
else:
Expand Down Expand Up @@ -1487,7 +1490,8 @@ async def headers_received(
# ability to see exceptions.
self.final_callback = None # type: ignore

future_set_result_unless_cancelled(self.connect_future, self)
if self.connect_future:
future_set_result_unless_cancelled(self.connect_future, self)

def write_message(
self, message: Union[str, bytes, Dict[str, Any]], binary: bool = False
Expand Down Expand Up @@ -1664,6 +1668,8 @@ def websocket_connect(
subprotocols=subprotocols,
resolver=resolver,
)
if callback is not None:
IOLoop.current().add_future(conn.connect_future, callback)
return conn.connect_future
if conn.connect_future:
if callback is not None:
IOLoop.current().add_future(conn.connect_future, callback)
return conn.connect_future
raise WebSocketError("Initialize websocket client")