Skip to content

Commit caa9609

Browse files
committed
Fix WebSocket race condition
1 parent 90401f1 commit caa9609

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

jupyter_server_proxy/handlers.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ async def proxy_open(self, host, port, proxied_path=''):
231231

232232
client_uri = self.get_client_uri('ws', host, port, proxied_path)
233233
headers = self.request.headers
234+
current_loop = ioloop.IOLoop.current()
235+
ws_connected = current_loop.asyncio_loop.create_future()
234236

235237
def message_cb(message):
236238
"""
@@ -261,10 +263,15 @@ async def start_websocket_connection():
261263
request = httpclient.HTTPRequest(url=client_uri, headers=headers)
262264
self.ws = await pingable_ws_connect(request=request,
263265
on_message_callback=message_cb, on_ping_callback=ping_cb)
266+
ws_connected.set_result(True)
264267
self._record_activity()
265268
self.log.info('Websocket connection established to {}'.format(client_uri))
266269

267-
ioloop.IOLoop.current().add_callback(start_websocket_connection)
270+
current_loop.add_callback(start_websocket_connection)
271+
# Wait for the WebSocket to be connected before resolving.
272+
# Otherwise, messages sent by the client before the
273+
# WebSocket successful connection would be dropped.
274+
await ws_connected
268275

269276

270277
def proxy_request_headers(self):

0 commit comments

Comments
 (0)