|
4 | 4 | SEE http://python-socketio.readthedocs.io/en/latest/ |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import contextlib |
7 | 8 | import logging |
8 | 9 | from typing import Any |
9 | 10 |
|
|
14 | 15 | from models_library.socketio import SocketMessageDict |
15 | 16 | from models_library.users import UserID |
16 | 17 | from servicelib.aiohttp.observer import emit |
| 18 | +from servicelib.logging_errors import create_troubleshootting_log_kwargs |
17 | 19 | from servicelib.logging_utils import get_log_record_extra, log_context |
18 | 20 | from servicelib.request_keys import RQT_USERID_KEY |
19 | 21 |
|
@@ -162,37 +164,64 @@ async def connect( |
162 | 164 | @register_socketio_handler |
163 | 165 | async def disconnect(socket_id: SocketID, app: web.Application) -> None: |
164 | 166 | """socketio reserved handler for when the socket.io connection is disconnected.""" |
165 | | - sio = get_socket_server(app) |
166 | | - async with sio.session(socket_id) as socketio_session: |
167 | | - if user_id := socketio_session.get("user_id"): |
| 167 | + async with contextlib.AsyncExitStack() as stack: |
| 168 | + |
| 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 | + |
| 188 | + user_id = socketio_session["user_id"] |
168 | 189 | client_session_id = socketio_session["client_session_id"] |
169 | 190 | product_name = socketio_session["product_name"] |
170 | 191 |
|
171 | | - with log_context( |
172 | | - _logger, |
173 | | - logging.INFO, |
174 | | - "disconnection of %s with %s", |
175 | | - f"{user_id=}", |
176 | | - f"{client_session_id=}", |
177 | | - ): |
178 | | - with managed_resource(user_id, client_session_id, app) as user_session: |
179 | | - await user_session.remove_socket_id() |
180 | | - # signal same user other clients if available |
181 | | - await emit( |
182 | | - app, |
183 | | - "SIGNAL_USER_DISCONNECTED", |
184 | | - user_id, |
185 | | - client_session_id, |
186 | | - app, |
187 | | - product_name, |
| 192 | + except KeyError as err: |
| 193 | + _logger.exception( |
| 194 | + **create_troubleshootting_log_kwargs( |
| 195 | + f"Socket session {socket_id} does not have user_id or client_session_id during disconnect", |
| 196 | + error=err, |
| 197 | + error_context={ |
| 198 | + "socket_id": socket_id, |
| 199 | + "socketio_session": socketio_session, |
| 200 | + }, |
| 201 | + tip="Check if session is corrupted", |
188 | 202 | ) |
| 203 | + ) |
| 204 | + return |
189 | 205 |
|
190 | | - else: |
191 | | - # this should not happen!! |
192 | | - _logger.error( |
193 | | - "Unknown client diconnected sid: %s, session %s", |
194 | | - socket_id, |
195 | | - f"{socketio_session}", |
| 206 | + # Disconnecting |
| 207 | + with log_context( |
| 208 | + _logger, |
| 209 | + logging.INFO, |
| 210 | + "disconnection of %s with %s", |
| 211 | + f"{user_id=}", |
| 212 | + f"{client_session_id=}", |
| 213 | + ): |
| 214 | + with managed_resource(user_id, client_session_id, app) as user_session: |
| 215 | + await user_session.remove_socket_id() |
| 216 | + |
| 217 | + # signal same user other clients if available |
| 218 | + await emit( |
| 219 | + app, |
| 220 | + "SIGNAL_USER_DISCONNECTED", |
| 221 | + user_id, |
| 222 | + client_session_id, |
| 223 | + app, |
| 224 | + product_name, |
196 | 225 | ) |
197 | 226 |
|
198 | 227 |
|
|
0 commit comments