Skip to content

Commit 428cf47

Browse files
committed
fix: make chat_message reconcile upsert-only
1 parent b5338bb commit 428cf47

1 file changed

Lines changed: 3 additions & 10 deletions

File tree

backend/open_webui/models/chats.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -501,20 +501,13 @@ async def backfill_messages_by_chat_id(self, chat_id: str, user_id: str, message
501501
async def reconcile_messages_by_chat_id(
502502
self, chat_id: str, user_id: str, messages: dict[str, dict]
503503
) -> None:
504-
"""Sync ``chat_message`` rows with the committed JSON blob.
504+
"""Upsert ``chat_message`` rows from the blob; never delete.
505505
506-
Upserts current messages via ``backfill_messages_by_chat_id``
507-
and deletes orphaned rows whose message_id no longer appears
508-
in the blob. Best-effort: errors are logged but never raised.
506+
Diff-based pruning (table − blob) is unsafe: a lost blob write
507+
from a read-modify-write race would drop a still-valid row.
509508
"""
510509
try:
511510
await self.backfill_messages_by_chat_id(chat_id, user_id, messages)
512-
513-
existing_map = await ChatMessages.get_messages_map_by_chat_id(chat_id)
514-
if existing_map is not None:
515-
orphaned_ids = set(existing_map.keys()) - set(messages.keys())
516-
if orphaned_ids:
517-
await ChatMessages.delete_message_ids_by_chat_id(chat_id, orphaned_ids)
518511
except Exception as e:
519512
log.warning('Failed to reconcile chat_message rows for chat %s: %s', chat_id, e)
520513

0 commit comments

Comments
 (0)