From 054a831a21a0d0a8e56743d4a79fa1af0c220190 Mon Sep 17 00:00:00 2001 From: Classic298 <27028174+Classic298@users.noreply.github.com> Date: Tue, 29 Sep 2026 12:42:56 +0200 Subject: [PATCH] fix: typing quickly in a note can leave the saved note missing the last characters When edits to a note arrived a few milliseconds apart, an older edit could finish its server-side checks after a newer one and replace its pending save. The saved note then stayed a few characters behind the editor, most often on a shared note edited by someone other than the owner. The last edit made is now always the one that gets saved. Fixes #31585 --- backend/open_webui/socket/main.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/backend/open_webui/socket/main.py b/backend/open_webui/socket/main.py index f157ed2f81..5c8a37df74 100644 --- a/backend/open_webui/socket/main.py +++ b/backend/open_webui/socket/main.py @@ -6,6 +6,7 @@ import logging import random import sys import time +import weakref from contextlib import suppress from typing import Any @@ -203,6 +204,7 @@ REDIS_EVENT_CHANNEL = f'{REDIS_KEY_PREFIX}:direct_completion' EVENT_QUEUES: dict[str, asyncio.Queue] = {} EVENT_PUBLISH_LOCK = asyncio.Lock() +DOCUMENT_UPDATE_LOCKS: weakref.WeakValueDictionary[str, asyncio.Lock] = weakref.WeakValueDictionary() def get_session_pool_batches(): @@ -825,6 +827,18 @@ async def yjs_document_state(sid, data): @sio.on('ydoc:document:update') async def yjs_document_update(sid, data): + try: + document_id = normalize_document_id(data['document_id']) + except Exception as e: + log.error('Error in yjs_document_update: %s', e) + return + + # Keep per-document updates in arrival order + async with DOCUMENT_UPDATE_LOCKS.setdefault(document_id, asyncio.Lock()): + await handle_yjs_document_update(sid, data) + + +async def handle_yjs_document_update(sid, data): """Handle Yjs document updates""" try: document_id = data['document_id']