From 4ca96cffc8268ecf31c8d240bfecb3d2647ba8ff Mon Sep 17 00:00:00 2001 From: Classic298 <27028174+Classic298@users.noreply.github.com> Date: Wed, 25 Nov 2026 02:15:01 +0100 Subject: [PATCH] fix: deleting a chat while its background sub-agent runs leaves the chat in place on Postgres (#35035) On Postgres with Redis, deleting a chat whose background sub-agent was still working often failed without an error. The sub-agent stopped and its thread went away, but the chat stayed in the sidebar. Stopping the sub-agent makes it post its result into that same chat, and that write and the delete blocked each other until the database cancelled one of them, usually the delete. Deleting a chat now locks the chat first, like every other write to a chat, so the two run one after the other. Deleting all chats or a folder's chats could clash the same way and gets the same change. Fixes #35034 --- backend/open_webui/models/chats.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/backend/open_webui/models/chats.py b/backend/open_webui/models/chats.py index a916fd6161..f67159ca37 100644 --- a/backend/open_webui/models/chats.py +++ b/backend/open_webui/models/chats.py @@ -2909,8 +2909,8 @@ class ChatTable: try: async with get_async_db_context(db) as session: await session.execute(update(AutomationRun).filter_by(chat_id=id).values(chat_id=None)) - await session.execute(delete(ChatMessage).filter_by(chat_id=id)) recipients = await delete_resource_comments('chat', select(Chat.id).filter_by(id=id), db=session) + await session.execute(delete(ChatMessage).filter_by(chat_id=id)) await delete_resource_tags('chat', select(Chat.id).filter_by(id=id), session) await session.execute( delete(ChatViewState).where(ChatViewState.chat_id.in_(select(Chat.id).filter_by(id=id))) @@ -2927,10 +2927,10 @@ class ChatTable: try: async with get_async_db_context(db) as session: await session.execute(update(AutomationRun).filter_by(chat_id=id).values(chat_id=None)) - await session.execute(delete(ChatMessage).filter_by(chat_id=id)) recipients = await delete_resource_comments( 'chat', select(Chat.id).filter_by(id=id, user_id=user_id), db=session ) + await session.execute(delete(ChatMessage).filter_by(chat_id=id)) await delete_resource_tags('chat', select(Chat.id).filter_by(id=id, user_id=user_id), session) await session.execute( delete(ChatViewState).where( @@ -2956,12 +2956,12 @@ class ChatTable: .filter(AutomationRun.chat_id.in_(select(Chat.id).filter_by(user_id=user_id))) .values(chat_id=None) ) - await session.execute( - delete(ChatMessage).filter(ChatMessage.chat_id.in_(select(Chat.id).filter_by(user_id=user_id))) - ) recipients = await delete_resource_comments( 'chat', select(Chat.id).filter_by(user_id=user_id), db=session ) + await session.execute( + delete(ChatMessage).filter(ChatMessage.chat_id.in_(select(Chat.id).filter_by(user_id=user_id))) + ) await delete_resource_tags('chat', select(Chat.id).filter_by(user_id=user_id), session) await session.execute( delete(ChatViewState).where(ChatViewState.chat_id.in_(select(Chat.id).filter_by(user_id=user_id))) @@ -2985,8 +2985,8 @@ class ChatTable: await session.execute( update(AutomationRun).filter(AutomationRun.chat_id.in_(chat_ids_stmt)).values(chat_id=None) ) - await session.execute(delete(ChatMessage).filter(ChatMessage.chat_id.in_(chat_ids_stmt))) recipients = await delete_resource_comments('chat', chat_ids_stmt, db=session) + await session.execute(delete(ChatMessage).filter(ChatMessage.chat_id.in_(chat_ids_stmt))) await delete_resource_tags('chat', chat_ids_stmt, session) await session.execute(delete(SharedChatTable).filter(SharedChatTable.chat_id.in_(chat_ids_stmt))) recipients.update(