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
Replace scratchpad execution timeout with disconnect-triggered interrupt (#8749)
Trevor Manzdcd29a366053
diff --git a/marimo/_server/api/endpoints/execution.py b/marimo/_server/api/endpoints/execution.pyindex 461b0733d3..5e8d2c70a2 100644--- a/marimo/_server/api/endpoints/execution.py+++ b/marimo/_server/api/endpoints/execution.py@@ -2,6 +2,7 @@from __future__ import annotationsimport asyncio+import contextlibfrom typing import TYPE_CHECKING, Optionalfrom uuid import uuid4@@ -263,10 +264,8 @@ async def execute_code(""" # noqa: E501from marimo._runtime.commands import ExecuteScratchpadCommandfrom marimo._server.scratchpad import (- EXECUTION_TIMEOUT,ScratchCellListener,build_done_event,- build_timeout_event,)app_state = AppState(request)@@ -281,24 +280,39 @@ async def execute_code(http_request=HTTPRequest.from_request(request),)+ async def _watch_disconnect() -> None:+ """Wait for client disconnect and interrupt the kernel."""+ while True:+ # request._receive is the ASGI `receive` callable. Although+ # it's a private Starlette attribute, it's the standard way to+ # detect disconnects and doesn't race with StreamingResponse+ # (which only writes to the send channel, never reads receive).+ message = await request._receive()+ if message.get("type") == "http.disconnect":+ session.try_interrupt()+ return+async def sse_generator() -> AsyncGenerator[str, None]:- listener = ScratchCellListener()- with session.scoped(listener):- async with session.scratchpad_lock:- session.put_control_request(- ExecuteScratchpadCommand(- code=body.code,- request=HTTPRequest.from_request(request),- ),- from_consumer_id=None,- )- async for event in listener.stream(timeout=EXECUTION_TIMEOUT):- yield event-- if listener.timed_out:- yield build_timeout_event(EXECUTION_TIMEOUT)- else:+ disconnect_task = asyncio.create_task(_watch_disconnect())+ try:+ listener = ScratchCellListener()+ with session.scoped(listener):+ async with session.scratchpad_lock:+ session.put_control_request(+ ExecuteScratchpadCommand(+ code=body.code,+ request=HTTPRequest.from_request(request),+ ),+ from_consumer_id=None,+ )+ async for event in listener.stream():+ yield event+yield build_done_event(session)+ finally:+ disconnect_task.cancel()+ with contextlib.suppress(asyncio.CancelledError):+ await disconnect_taskreturn StreamingResponse(sse_generator(), media_type="text/event-stream")diff --git a/marimo/_server/scratchpad.py b/marimo/_server/scratchpad.pyindex c3104130cc..e0c6758895 100644--- a/marimo/_server/scratchpad.py+++ b/marimo/_server/scratchpad.py@@ -26,7 +26,9 @@ if TYPE_CHECKING:from marimo._messaging.types import KernelMessagefrom marimo._session.session import Session-EXECUTION_TIMEOUT = 30.0 # seconds+EXECUTION_TIMEOUT = (+ 30.0 # seconds — used only by wait(); stream() has no timeout+)# Channel name constants for SSE events_CHANNEL_MAP = {@@ -106,25 +108,15 @@ class ScratchCellListener(SessionEventListener):if msg.status == "idle":self._queue.put_nowait(None) # sentinel- async def stream(- self, timeout: float = EXECUTION_TIMEOUT- ) -> AsyncGenerator[str, None]:- """Yield SSE-formatted stdout/stderr events until execution completes."""- import time+ async def stream(self) -> AsyncGenerator[str, None]:+ """Yield SSE-formatted stdout/stderr events until execution completes.- deadline = time.monotonic() + timeout+ Streams indefinitely — the caller is responsible for cancellation+ (e.g. by disconnecting the SSE client, which triggers a kernel+ interrupt server-side).+ """while True:- remaining = deadline - time.monotonic()- if remaining <= 0:- self.timed_out = True- return- try:- msg = await asyncio.wait_for(- self._queue.get(), timeout=remaining- )- except asyncio.TimeoutError:- self.timed_out = True- return+ msg = await self._queue.get()if msg is None:# Done sentinel — but stdout/stderr are flushed everydiff --git a/tests/_server/test_scratchpad.py b/tests/_server/test_scratchpad.pyindex 063cc76a4c..c8e3181c0c 100644--- a/tests/_server/test_scratchpad.py+++ b/tests/_server/test_scratchpad.py@@ -358,7 +358,6 @@ class TestScratchCellListener:name, payload = _parse_sse(events[0])assert name == "stdout"assert payload["data"] == "hello\n"- assert not listener.timed_out@pytest.mark.asyncioasync def test_ignores_other_cells(self) -> None:@@ -379,15 +378,42 @@ class TestScratchCellListener:assert listener._queue.empty()@pytest.mark.asyncio- async def test_stream_timeout(self) -> None:+ async def test_stream_cancelled_on_disconnect(self) -> None:+ """stream() can be cancelled externally (simulating client disconnect)."""+ import asyncio++ from marimo._messaging.serde import serialize_kernel_message+listener = ScratchCellListener()event_bus = MagicMock()session = MagicMock()listener.on_attach(session, event_bus)+ # Send one stdout event but no idle sentinel — stream would block forever+ console = CellNotification(+ cell_id=SCRATCH_CELL_ID,+ console=CellOutput.stdout("partial\n"),+ )+ listener.on_notification_sent(+ session, serialize_kernel_message(console)+ )+events: list[str] = []- async for event in listener.stream(timeout=0.1):- events.append(event)+ got_first_event = asyncio.Event()++ async def consume() -> None:+ async for event in listener.stream():+ events.append(event)+ got_first_event.set()++ task = asyncio.create_task(consume())+ await got_first_event.wait()+ task.cancel()+ with pytest.raises(asyncio.CancelledError):+ await task- assert events == []- assert listener.timed_out is True+ # Got the partial event before cancellation+ assert len(events) == 1+ name, payload = _parse_sse(events[0])+ assert name == "stdout"+ assert payload["data"] == "partial\n"