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
This commit is contained in:
Classic298 authored and GitHub committed 2026-11-25 05:15:01 +04:00
1 parent aceed36114
commit 4ca96cffc8
1 file changed
+6 -6
+6 -6
View File
@@ -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(