From 939bcdb79e3dcad2278f1e7f4f517a3f2f36f3ec Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Wed, 12 Aug 2026 01:13:38 -0600 Subject: [PATCH] refac Co-Authored-By: G30 <50341825+silentoplayz@users.noreply.github.com> --- backend/open_webui/socket/main.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/backend/open_webui/socket/main.py b/backend/open_webui/socket/main.py index 7cb7904fed..d88fd827f1 100644 --- a/backend/open_webui/socket/main.py +++ b/backend/open_webui/socket/main.py @@ -176,6 +176,7 @@ YDOC_MANAGER = YdocManager( async def periodic_session_pool_cleanup(): """Reap orphaned SESSION_POOL entries that missed heartbeats (e.g. crashed instance).""" retry_delay = random.uniform(WEBSOCKET_REDIS_LOCK_TIMEOUT / 2, WEBSOCKET_REDIS_LOCK_TIMEOUT) + renew_interval = max(WEBSOCKET_REDIS_LOCK_TIMEOUT / 2, 0.5) while True: if not session_aquire_func(): log.debug('Session cleanup lock held by another node. Retrying.') @@ -197,7 +198,21 @@ async def periodic_session_pool_cleanup(): del SESSION_POOL[sid] except KeyError: pass - await asyncio.sleep(SESSION_POOL_TIMEOUT) + + next_cleanup_at = time.monotonic() + SESSION_POOL_TIMEOUT + lock_lost = False + while True: + sleep_for = min(renew_interval, next_cleanup_at - time.monotonic()) + if sleep_for <= 0: + break + await asyncio.sleep(sleep_for) + if not session_renew_func(): + log.warning('Unable to renew session cleanup lock. Retrying cleanup ownership.') + lock_lost = True + break + + if lock_lost: + break finally: session_release_func()