From 02597019d5f7b2826839750dc34d7fac66d5ddac Mon Sep 17 00:00:00 2001 From: Ihar Hrachyshka Date: Fri, 13 Nov 2026 00:49:29 -0500 Subject: [PATCH] fix(websocket): release room lock before emitting progress (#1705) Emitting progress can synchronously disconnect an expired client. Disconnect cleanup takes the same room lock held by the broadcast, deadlocking the download worker and freezing progress updates. Release the lock before emitting, as broadcast_status_update() does. ## Summary by CodeRabbit * **Bug Fixes** * Improved handling of user-specific download progress updates when clients are connected. Administrator progress updates and the redaction of book identifiers in user updates remain unchanged. --- shelfmark/api/websocket.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/shelfmark/api/websocket.py b/shelfmark/api/websocket.py index 60fd32ea..e469690c 100644 --- a/shelfmark/api/websocket.py +++ b/shelfmark/api/websocket.py @@ -190,9 +190,11 @@ class WebSocketManager: if user_id is not None: room = f"user_{user_id}" with self._rooms_lock: - if room in self._user_rooms: - user_data = {**data, "book_id": viewer_download_id(book_id, request_id)} - socketio.emit("download_progress", user_data, to=room) + has_clients = room in self._user_rooms + # emit() can synchronously disconnect a client whose cleanup needs this lock. + if has_clients: + user_data = {**data, "book_id": viewer_download_id(book_id, request_id)} + socketio.emit("download_progress", user_data, to=room) logger.debug("Broadcasted progress for book %s: %s%%", book_id, progress) except Exception: logger.exception("Error broadcasting download progress")