Skip to content

Commit 9123420

Browse files
committed
catch key error on disconnection
1 parent e0f1ec9 commit 9123420

File tree

1 file changed

+50
-56
lines changed
  • services/web/server/src/simcore_service_webserver/socketio

1 file changed

+50
-56
lines changed

services/web/server/src/simcore_service_webserver/socketio/_handlers.py

Lines changed: 50 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
SEE http://python-socketio.readthedocs.io/en/latest/
55
"""
66

7-
import contextlib
87
import logging
98
from typing import Any
109

@@ -165,63 +164,58 @@ async def connect(
165164
@register_socketio_handler
166165
async def disconnect(socket_id: SocketID, app: web.Application) -> None:
167166
"""socketio reserved handler for when the socket.io connection is disconnected."""
168-
async with contextlib.AsyncExitStack() as stack:
169-
# retrieve the socket session
170-
try:
171-
socketio_session = await stack.enter_async_context(
172-
get_socket_server(app).session(socket_id)
173-
)
174-
175-
except KeyError as err:
176-
_logger.warning(
177-
**create_troubleshootting_log_kwargs(
178-
f"Socket session {socket_id} not found during disconnect, already cleaned up",
179-
error=err,
180-
error_context={"socket_id": socket_id},
181-
)
182-
)
183-
return
184-
185-
# session is wel formed, we can access its data
186-
try:
187-
user_id = socketio_session["user_id"]
188-
client_session_id = socketio_session["client_session_id"]
189-
product_name = socketio_session["product_name"]
190-
191-
except KeyError as err:
192-
_logger.exception(
193-
**create_troubleshootting_log_kwargs(
194-
f"Socket session {socket_id} does not have user_id or client_session_id during disconnect",
195-
error=err,
196-
error_context={
197-
"socket_id": socket_id,
198-
"socketio_session": socketio_session,
199-
},
200-
tip="Check if session is corrupted",
167+
try:
168+
async with get_socket_server(app).session(socket_id) as socketio_session:
169+
# if session is well formed, we can access its data
170+
try:
171+
user_id = socketio_session["user_id"]
172+
client_session_id = socketio_session["client_session_id"]
173+
product_name = socketio_session["product_name"]
174+
175+
except KeyError as err:
176+
_logger.exception(
177+
**create_troubleshootting_log_kwargs(
178+
f"Socket session {socket_id} does not have user_id or client_session_id during disconnect",
179+
error=err,
180+
error_context={
181+
"socket_id": socket_id,
182+
"socketio_session": socketio_session,
183+
},
184+
tip="Check if session is corrupted",
185+
)
201186
)
187+
return
188+
189+
except KeyError as err:
190+
_logger.warning(
191+
**create_troubleshootting_log_kwargs(
192+
f"Socket session {socket_id} not found during disconnect, already cleaned up",
193+
error=err,
194+
error_context={"socket_id": socket_id},
202195
)
203-
return
204-
205-
# Disconnecting
206-
with log_context(
207-
_logger,
208-
logging.INFO,
209-
"disconnection of %s with %s",
210-
f"{user_id=}",
211-
f"{client_session_id=}",
212-
):
213-
with managed_resource(user_id, client_session_id, app) as user_session:
214-
await user_session.remove_socket_id()
215-
216-
# signal same user other clients if available
217-
await emit(
218-
app,
219-
"SIGNAL_USER_DISCONNECTED",
220-
user_id,
221-
client_session_id,
222-
app,
223-
product_name,
224-
)
196+
)
197+
return
198+
199+
# Notify disconnection to all replicas/plugins
200+
with log_context(
201+
_logger,
202+
logging.INFO,
203+
"disconnection of %s with %s",
204+
f"{user_id=}",
205+
f"{client_session_id=}",
206+
):
207+
with managed_resource(user_id, client_session_id, app) as user_session:
208+
await user_session.remove_socket_id()
209+
210+
# signal same user other clients if available
211+
await emit(
212+
app,
213+
"SIGNAL_USER_DISCONNECTED",
214+
user_id,
215+
client_session_id,
216+
app,
217+
product_name,
218+
)
225219

226220

227221
@register_socketio_handler

0 commit comments

Comments
 (0)