A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
Fix: Prevent text duplication in RTC during quick reconnection (#3883)
devin-ai-integration[bot]41a95adcf1fb
diff --git a/marimo/_server/api/endpoints/ws.py b/marimo/_server/api/endpoints/ws.pyindex 99ddff44ff..8698befc4c 100644--- a/marimo/_server/api/endpoints/ws.py+++ b/marimo/_server/api/endpoints/ws.py@@ -157,16 +157,16 @@ async def send_updates(async def clean_cell(cell_id_and_file_key: CellIdAndFileKey, timeout: float = 60) -> None:- # Clients disconnect/reconnect to keep the WebSocket connection alive,- # or because of network issues.- # Keep the ycell in memory for one minute after client disconnects,- # so that it can be synchronized and to prevent content duplication.- # This task will be cancelled when any client reconnets.- await asyncio.sleep(timeout)- async with ycell_lock:- ycell = ycells[cell_id_and_file_key]- if not ycell.clients:- del ycells[cell_id_and_file_key]+ try:+ await asyncio.sleep(timeout)+ async with ycell_lock:+ if cell_id_and_file_key in ycells:+ ycell = ycells[cell_id_and_file_key]+ if not ycell.clients:+ del ycells[cell_id_and_file_key]+ except asyncio.CancelledError:+ # Task was cancelled due to client reconnection+ pass@router.websocket("/ws/{cell_id}")diff --git a/tests/_server/api/endpoints/test_ws_rtc.py b/tests/_server/api/endpoints/test_ws_rtc.pynew file mode 100644index 0000000000..9b2a079a8a--- /dev/null+++ b/tests/_server/api/endpoints/test_ws_rtc.py@@ -0,0 +1,56 @@+import asyncio+from typing import AsyncGenerator++import pytest+from pycrdt import Doc, Text++from marimo._server.api.endpoints.ws import (+ CellIdAndFileKey,+ YCell,+ clean_cell,+ ycells,+)+from marimo._server.file_router import MarimoFileKey+from marimo._types.ids import CellId_t++[email protected] # type: ignore+async def setup_ycell() -> AsyncGenerator[None, None]:+ """Setup and teardown for ycell tests"""+ # Clear any existing ycells+ ycells.clear()+ yield+ # Cleanup after test+ ycells.clear()++[email protected] # type: ignore[email protected]("setup_ycell") # type: ignore+async def test_quick_reconnection() -> None:+ """Test that quick reconnection properly handles cleanup task cancellation"""+ # Setup+ cell_id = CellId_t("test_cell")+ file_key = MarimoFileKey("test_file")+ key = CellIdAndFileKey(cell_id, file_key)++ # Create initial ycell+ ydoc = Doc[Text]()+ ycell = YCell(ydoc=ydoc, clients=1)+ ycells[key] = ycell++ # Start cleanup task+ cleanup_task = asyncio.create_task(clean_cell(key))++ # Simulate quick reconnection by creating a new client before cleanup finishes+ ycells[key].clients += 1++ # Cancel cleanup task (simulating what happens in ycell_provider)+ cleanup_task.cancel()+ try:+ await cleanup_task+ except asyncio.CancelledError:+ pass++ # Verify state+ assert len(ycells) == 1+ assert ycells[key].clients == 2 # Original client + reconnected client