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(server): stop scratchpad console cross-delivery between concurrent executes (#10036)
Matthew Housebbed51219286
diff --git a/marimo/_mcp/code_server/main.py b/marimo/_mcp/code_server/main.pyindex 56d170e975..4fa2f67dab 100644--- a/marimo/_mcp/code_server/main.py+++ b/marimo/_mcp/code_server/main.py@@ -130,8 +130,10 @@ def setup_code_mcp_server(# Correlation ID: see /api/kernel/execute for rationale.run_id = str(uuid4())listener = ScratchCellListener(run_id=run_id)- with session.scoped(listener):- async with session.scratchpad_lock:+ # Ensure we take a lock on the scratchpad before scoping the+ # listener. See #10035.+ async with session.scratchpad_lock:+ with session.scoped(listener):notebook_cells, cell_outputs = snapshot_for_scratchpad(session)session.put_control_request(ExecuteScratchpadCommand(diff --git a/marimo/_server/api/endpoints/execution.py b/marimo/_server/api/endpoints/execution.pyindex 7a1d24e13f..fa8e418f96 100644--- a/marimo/_server/api/endpoints/execution.py+++ b/marimo/_server/api/endpoints/execution.py@@ -361,8 +361,10 @@ async def execute_code(run_id = str(uuid4())try:listener = ScratchCellListener(run_id=run_id)- with session.scoped(listener):- async with session.scratchpad_lock:+ # Ensure we take a lock on the scratchpad before scoping the+ # listener. See #10035.+ async with session.scratchpad_lock:+ with session.scoped(listener):http_req = HTTPRequest.from_request(request)server_url, auth_token = get_code_mode_credentials(app_state, requestdiff --git a/marimo/_server/scratchpad.py b/marimo/_server/scratchpad.pyindex 7f4dd34ea2..f51aefc49c 100644--- a/marimo/_server/scratchpad.py+++ b/marimo/_server/scratchpad.py@@ -342,8 +342,10 @@ async def run_scratchpad_code(run_id = str(uuid4())listener = ScratchCellListener(run_id=run_id)- with session.scoped(listener):- async with session.scratchpad_lock:+ # Ensure we take a lock on the scratchpad before scoping the listener.+ # See #10035.+ async with session.scratchpad_lock:+ with session.scoped(listener):notebook_cells, cell_outputs = snapshot_for_scratchpad(session)session.put_control_request(ExecuteScratchpadCommand(diff --git a/tests/_server/test_scratchpad.py b/tests/_server/test_scratchpad.pyindex bf78b481a8..678fa57a4c 100644--- a/tests/_server/test_scratchpad.py+++ b/tests/_server/test_scratchpad.py@@ -965,3 +965,32 @@ class TestRunScratchpadCode:assert result.success is Falseassert result.errors == ["cell 'child-cell' raised ZeroDivisionError"]++ @pytest.mark.asyncio+ async def test_listener_registered_only_while_lock_held(self) -> None:+ """The listener must be scoped only while `scratchpad_lock` is held.+ Scoping before the lock lets a concurrent execute waiting on the lock+ accumulate this run's console output (see #10035)."""+ session = _FakeSession()+ locked_when_scoped: list[bool] = []+ original_scoped = session.scoped++ @contextmanager+ def recording_scoped(+ listener: ScratchCellListener,+ ) -> Iterator[ScratchCellListener]:+ locked_when_scoped.append(session.scratchpad_lock.locked())+ with original_scoped(listener) as scoped_listener:+ yield scoped_listener++ session.scoped = recording_scoped # type: ignore[method-assign]++ await run_scratchpad_code(+ session.as_session(),+ _build_request(),+ code="x = 1",+ server_url="u",+ auth_token="t",+ )++ assert locked_when_scoped == [True]