|
4 | 4 | SEE http://python-socketio.readthedocs.io/en/latest/ |
5 | 5 | """ |
6 | 6 |
|
7 | | -import contextlib |
8 | 7 | import logging |
9 | 8 | from typing import Any |
10 | 9 |
|
@@ -165,63 +164,58 @@ async def connect( |
165 | 164 | @register_socketio_handler |
166 | 165 | async def disconnect(socket_id: SocketID, app: web.Application) -> None: |
167 | 166 | """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 | + ) |
201 | 186 | ) |
| 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}, |
202 | 195 | ) |
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 | + ) |
225 | 219 |
|
226 | 220 |
|
227 | 221 | @register_socketio_handler |
|
0 commit comments