diff --git a/backend/open_webui/socket/main.py b/backend/open_webui/socket/main.py index d07a8650e5..529dfbbfa0 100644 --- a/backend/open_webui/socket/main.py +++ b/backend/open_webui/socket/main.py @@ -237,11 +237,16 @@ YDOC_MANAGER = YdocManager( ) -def get_session_pool_batches(): +async def get_session_pool_batches(): """All session pool entries, in bounded batches for the Redis backing.""" if WEBSOCKET_MANAGER == 'redis': - return SESSION_POOL.scan_batches() - return [list(SESSION_POOL.items())] + batches = SESSION_POOL.scan_batches() + while True: + batch = await asyncio.to_thread(next, batches, None) + if batch is None: + return + yield batch + yield list(SESSION_POOL.items()) async def publish_chat_presence(chat_id: str): @@ -259,7 +264,7 @@ async def publish_chat_presence(chat_id: str): async with lock, asyncio.timeout(WEBSOCKET_REDIS_LOCK_TIMEOUT / 2): now = time.time() people = {} - for batch in get_session_pool_batches(): + async for batch in get_session_pool_batches(): for _, session in batch: if ( session @@ -298,7 +303,7 @@ async def publish_note_presence(note_id: str): async with lock, asyncio.timeout(WEBSOCKET_REDIS_LOCK_TIMEOUT / 2): now = time.time() people = {} - for batch in get_session_pool_batches(): + async for batch in get_session_pool_batches(): for _, session in batch: if ( not session @@ -348,7 +353,7 @@ async def periodic_session_pool_cleanup(): break now = int(time.time()) - for batch in get_session_pool_batches(): + async for batch in get_session_pool_batches(): expired = [ sid for sid, entry in batch @@ -515,7 +520,7 @@ def get_session_ids_from_room(room): return list(members) if members else [] -def get_session_ids_by_user_ids(user_ids: list[str]) -> list[str]: +async def get_session_ids_by_user_ids(user_ids: list[str]) -> list[str]: """Get known session IDs for users across the local rooms and shared session pool.""" if not user_ids: return [] @@ -524,7 +529,7 @@ def get_session_ids_by_user_ids(user_ids: list[str]) -> list[str]: session_ids = set() for user_id in user_ids: session_ids.update(get_session_ids_from_room(f'user:{user_id}')) - for batch in get_session_pool_batches(): + async for batch in get_session_pool_batches(): session_ids.update(sid for sid, user in batch if user and user.get('id') in user_ids) return list(session_ids) @@ -572,7 +577,7 @@ async def enter_room_for_users(room: str, user_ids: list[str]): async def leave_room_for_users(room: str, user_ids: list[str]): """Make all sessions of each user leave a room, including sessions on other workers.""" - for sid in get_session_ids_by_user_ids(user_ids): + for sid in await get_session_ids_by_user_ids(user_ids): try: if room.startswith('doc_'): document_id = room.removeprefix('doc_') @@ -602,7 +607,7 @@ async def disconnect_user_sessions(user_id: str, *, refresh_access: bool = False async def disconnect_sessions_for_users(user_ids: list[str], *, refresh_access: bool = False) -> list[str]: """Disconnect all Socket.IO sessions belonging to these users and return their session IDs.""" - session_ids = get_session_ids_by_user_ids(user_ids) + session_ids = await get_session_ids_by_user_ids(user_ids) for sid in session_ids: if refresh_access: try: @@ -709,7 +714,7 @@ async def refresh_chat_access(chat_id=None): # The pool includes sessions on other workers; Socket.IO routes room changes via Redis. access = {} chat_ids = set() - for batch in get_session_pool_batches(): + async for batch in get_session_pool_batches(): chat_owner_ids: dict[str, str] = {} if not chat_id: # Owners keep access; role changes and deletion disconnect them separately. @@ -751,7 +756,7 @@ async def refresh_chat_access(chat_id=None): async def refresh_note_access(note_id=None): """Make users who lost access to notes they have open, e.g. through a folder, leave their rooms.""" user_ids_by_note: dict[str, set[str]] = {} - for batch in get_session_pool_batches(): + async for batch in get_session_pool_batches(): for _, session in batch: for id in (session or {}).get('note_ids') or []: if not note_id or id == note_id: @@ -1092,7 +1097,7 @@ async def ydoc_document_join(sid, data): # Serialise per user so parallel joins cannot exceed the limit async with SOCKET_EVENT_LOCKS.setdefault(('user', user_id), asyncio.Lock()): - user_sids = get_session_ids_by_user_ids([user_id]) + user_sids = await get_session_ids_by_user_ids([user_id]) if sid not in await YDOC_MANAGER.get_users(document_id) and ( sum([await YDOC_MANAGER.count_documents_for_user(user_sid) for user_sid in user_sids]) >= YDOC_MANAGER.MAX_DOCUMENTS_PER_USER @@ -1787,9 +1792,9 @@ async def get_event_emitter(request_info, update_db=True): return None -def get_user_chat_session_id(user_id: str, chat_id: str) -> str | None: +async def get_user_chat_session_id(user_id: str, chat_id: str) -> str | None: now = time.time() - for batch in get_session_pool_batches(): + async for batch in get_session_pool_batches(): for session_id, session in batch: if ( session @@ -1824,7 +1829,7 @@ async def get_event_call(request_info): return {'error': 'Client session disconnected.'} chat_id, message_id = metas[0]['parent_chat_id'], metas[0]['parent_message_id'] if chat_id not in ((SESSION_POOL.get(session_id) or {}).get('chat_ids') or []): - session_id = await asyncio.to_thread(get_user_chat_session_id, request_info['user_id'], chat_id) + session_id = await get_user_chat_session_id(request_info['user_id'], chat_id) # session_id is client-supplied; only the requesting user's own live session may be targeted. session = SESSION_POOL.get(session_id) @@ -1901,9 +1906,7 @@ async def get_event_call(request_info): if get_user_id_from_session_pool(next_session_id) != request_info['user_id']: next_session_id = None else: - next_session_id = await asyncio.to_thread( - get_user_chat_session_id, request_info['user_id'], chat_id - ) + next_session_id = await get_user_chat_session_id(request_info['user_id'], chat_id) if next_session_id is not None: session_id = request_info['session_id'] = next_session_id break