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: make the kernel SIGINT handler reentrancy-safe (#10975)
Kiran Gadhave8a4a688d7f42
diff --git a/marimo/_runtime/handlers.py b/marimo/_runtime/handlers.pyindex c471317a82..2031a7191c 100644--- a/marimo/_runtime/handlers.py+++ b/marimo/_runtime/handlers.py@@ -6,8 +6,6 @@ from dataclasses import dataclassfrom typing import TYPE_CHECKING, Anyfrom marimo import _loggers-from marimo._messaging.notification import InterruptedNotification-from marimo._messaging.notification_utils import broadcast_notificationfrom marimo._runtime.context import get_contextfrom marimo._runtime.context.kernel_context import KernelRuntimeContextfrom marimo._runtime.context.types import safe_get_context@@ -24,7 +22,13 @@ if TYPE_CHECKING:def construct_interrupt_handler() -> SigintHandler:def interrupt_handler(signum: int, frame: Any) -> None:- """Tries to interrupt the kernel."""+ """Interrupt the running cell.++ Python can run this handler nested inside itself when two+ SIGINTs arrive close together, so it does not write to the kernel+ stream. It does not send `InterruptedNotification`. The interrupted+ run reports the interruption after its running cell stops.+ """del signumdel frame@@ -46,9 +50,6 @@ def construct_interrupt_handler() -> SigintHandler:if sched is None and exec_ctx is None:return- LOGGER.info("Interrupt request received")- broadcast_notification(InterruptedNotification())-# DuckDB connections are sometimes left in an inconsistent state# when interrupted by a SIGINT; route through duckdb's own API.if exec_ctx is not None and exec_ctx.duckdb_connection is not None:diff --git a/marimo/_runtime/runner/cell_runner.py b/marimo/_runtime/runner/cell_runner.pyindex 22f936fc66..65f5dfd93f 100644--- a/marimo/_runtime/runner/cell_runner.py+++ b/marimo/_runtime/runner/cell_runner.py@@ -23,6 +23,8 @@ from marimo._messaging.errors import (MarimoSQLError,UnknownError,)+from marimo._messaging.notification import InterruptedNotification+from marimo._messaging.notification_utils import broadcast_notificationfrom marimo._messaging.tracebacks import write_tracebackfrom marimo._runtime import dataflowfrom marimo._runtime.context.types import safe_get_context@@ -866,6 +868,12 @@ class Runner:except KeyboardInterrupt:LOGGER.info("Runner interrupted via SIGINT")+ if self.interrupted:+ # Sent from normal control flow, not from the SIGINT handler.+ # The handler can run nested inside itself and must not take+ # the stream lock that this broadcast needs.+ broadcast_notification(InterruptedNotification())+finish_ctx = OnFinishHookContext(graph=self.graph,cells_to_run=self.cells_to_run,diff --git a/marimo/_server/api/endpoints/execution.py b/marimo/_server/api/endpoints/execution.pyindex b6d68a9f67..82e7a1a0be 100644--- a/marimo/_server/api/endpoints/execution.py+++ b/marimo/_server/api/endpoints/execution.py@@ -346,13 +346,25 @@ async def execute_code(http_request=HTTPRequest.from_request(request),)- async def _watch_disconnect() -> None:- """Wait for client disconnect and interrupt the kernel."""- await wait_for_http_disconnect(request)- session.try_interrupt()-async def sse_generator() -> AsyncGenerator[str, None]:- disconnect_task = asyncio.create_task(_watch_disconnect())+ interrupt_sent = False++ def interrupt_once() -> None:+ # The disconnect watcher and the response cancellation can+ # both observe one client disconnect. The kernel must get+ # one interrupt. Both callers run on the event-loop thread,+ # so a plain flag is enough.+ nonlocal interrupt_sent+ if interrupt_sent:+ return+ interrupt_sent = True+ session.try_interrupt()++ async def watch_disconnect() -> None:+ await wait_for_http_disconnect(request)+ interrupt_once()++ disconnect_task = asyncio.create_task(watch_disconnect())# Correlation ID: tags both the scratchpad command and the# listener so we wait for *our* completion and ignore# ``CompletedRun`` events from other commands on this session@@ -389,10 +401,11 @@ async def execute_code(yield build_done_event(session, listener)except asyncio.CancelledError:- # On ASGI spec < 2.4, Starlette consumes http.disconnect- # itself and cancels this generator before _watch_disconnect- # observes it; still interrupt the kernel on the way out.- session.try_interrupt()+ # On ASGI spec < 2.4 (uvicorn), Starlette cancels this+ # generator when the client disconnects. On spec >= 2.4 it+ # does not, and only the watcher fires. Interrupt here too+ # so both server kinds stop the kernel.+ interrupt_once()raisefinally:await cancel_and_wait(disconnect_task)diff --git a/tests/_runtime/runner/test_cell_runner.py b/tests/_runtime/runner/test_cell_runner.pyindex c7315c7808..634abfc88c 100644--- a/tests/_runtime/runner/test_cell_runner.py+++ b/tests/_runtime/runner/test_cell_runner.py@@ -7,6 +7,10 @@ import pytestfrom marimo._ast.cell import CellImplfrom marimo._dependencies.dependencies import DependencyManagerfrom marimo._messaging.errors import MarimoSQLError+from marimo._messaging.notification import (+ CellNotification,+ InterruptedNotification,+)from marimo._messaging.notification_utils import CellNotificationUtilsfrom marimo._runtime.capture import capture_stderrfrom marimo._runtime.runner.cell_runner import Runner@@ -497,6 +501,46 @@ async def test_runner_interrupted_flag_flips_on_async_cell_cancellation(assert runner.interrupted is True+async def test_run_all_sends_one_interrupted_notification_after_idle(+ mocked_kernel: MockedKernel, exec_req: ExecReqProvider+) -> None:+ """An interrupted run sends exactly one `InterruptedNotification`,+ and it follows the cell's `idle` status. The runner sends it from+ normal control flow, not from the signal handler."""+ k = mocked_kernel.k+ await k.run([er := exec_req.get("raise KeyboardInterrupt")])+ mocked_kernel.stream.messages.clear()++ runner = Runner(+ roots={er.cell_id},+ graph=k.graph,+ glbls=k.globals,+ debugger=k.debugger,+ hooks=create_default_hooks(),+ execution_context=k._install_execution_context,+ )+ with capture_stderr():+ await runner.run_all()++ operations = mocked_kernel.stream.parsed_operations+ interrupted = [+ i+ for i, op in enumerate(operations)+ if isinstance(op, InterruptedNotification)+ ]+ idle = [+ i+ for i, op in enumerate(operations)+ if isinstance(op, CellNotification)+ and op.cell_id == er.cell_id+ and op.status == "idle"+ ]+ assert runner.interrupted+ assert len(interrupted) == 1+ assert idle+ assert idle[-1] < interrupted[0]++@pytest.mark.parametrize("with_execution_context", [True, False])@pytest.mark.parametrize("interrupt_count", [1, 2])async def test_post_execution_interrupt_still_finalizes_cell(diff --git a/tests/_runtime/test_interrupt_handlers.py b/tests/_runtime/test_interrupt_handlers.pyindex 6e4bfee3e7..4762dc45f9 100644--- a/tests/_runtime/test_interrupt_handlers.py+++ b/tests/_runtime/test_interrupt_handlers.py@@ -2,11 +2,13 @@from __future__ import annotationsimport signal+import threadingfrom unittest.mock import MagicMock, patchimport pytestfrom marimo._dependencies.dependencies import DependencyManager+from marimo._messaging.streams import ThreadSafeStreamfrom marimo._runtime.context.kernel_context import KernelRuntimeContextfrom marimo._runtime.context.types import ExecutionContextfrom marimo._runtime.handlers import construct_interrupt_handler@@ -185,3 +187,69 @@ def test_ignore_console_ctrl_c_keeps_interrupt_main_working() -> None:[sys.executable, "-c", script], timeout=30, capture_output=True)assert completed.returncode == 0, completed.stderr.decode()+++def _active_scheduler_context() -> MagicMock:+ sched = MagicMock()+ sched.has_active_tasks.return_value = False+ ctx = MagicMock(spec=KernelRuntimeContext)+ ctx.execution_context = None+ ctx.active_scheduler = sched+ return ctx+++def test_handler_does_not_broadcast() -> None:+ """The handler runs between arbitrary bytecodes and can run nested.+ It must not write to the kernel stream, because the stream write+ takes a non-reentrant lock."""+ ctx = _active_scheduler_context()+ ctx.stream = MagicMock()++ with (+ patch("marimo._runtime.handlers.safe_get_context", return_value=ctx),+ patch(+ "marimo._messaging.notification_utils.get_context",+ return_value=ctx,+ ),+ ):+ interrupt_handler = construct_interrupt_handler()+ with pytest.raises(MarimoInterrupt):+ interrupt_handler(signal.SIGINT, None)++ ctx.stream.write.assert_not_called()+++def test_handler_finishes_while_stream_lock_is_held() -> None:+ """A second SIGINT can arrive while the kernel holds `stream_lock`.+ The handler must still finish, so the kernel main thread cannot+ deadlock inside the signal handler."""+ stream = ThreadSafeStream(+ pipe=MagicMock(), input_queue=MagicMock(), redirect_console=False+ )+ ctx = _active_scheduler_context()+ ctx.stream = stream+ outcome: list[str] = []++ def run_handler() -> None:+ with (+ patch(+ "marimo._runtime.handlers.safe_get_context",+ return_value=ctx,+ ),+ patch(+ "marimo._messaging.notification_utils.get_context",+ return_value=ctx,+ ),+ ):+ interrupt_handler = construct_interrupt_handler()+ try:+ interrupt_handler(signal.SIGINT, None)+ except MarimoInterrupt:+ outcome.append("raised")++ worker = threading.Thread(target=run_handler, daemon=True)+ with stream.stream_lock:+ worker.start()+ worker.join(timeout=2)+ assert not worker.is_alive(), "handler blocked on stream_lock"+ assert outcome == ["raised"]diff --git a/tests/_server/api/endpoints/test_execution.py b/tests/_server/api/endpoints/test_execution.pyindex 549f4151e2..6f85f6e5f3 100644--- a/tests/_server/api/endpoints/test_execution.py+++ b/tests/_server/api/endpoints/test_execution.py@@ -1,6 +1,7 @@# Copyright 2026 Marimo. All rights reserved.from __future__ import annotations+import asyncioimport jsonimport sysimport time@@ -42,6 +43,55 @@ HEADERS = {}+def _count_execute_interrupts(+ client: TestClient, *, watcher_fires: bool, stream_cancelled: bool+) -> int:+ """POST `/api/kernel/execute` with a simulated client disconnect and+ return how many times the session interrupted the kernel.++ Args:+ client (TestClient): Client with a live session `SESSION_ID`.+ watcher_fires (bool): The disconnect watcher sees the disconnect.+ stream_cancelled (bool): The response stream is cancelled.+ """+ from unittest.mock import patch++ from marimo._server import scratchpad as scratchpad_mod++ session = get_session_manager(client).get_session(SESSION_ID)+ assert session is not None++ async def wait_for_disconnect(request: object) -> None: # noqa: ARG001+ if not watcher_fires:+ await asyncio.Event().wait()++ async def stream(self: object): # noqa: ARG001+ # Yield to the event loop so the watcher task runs first.+ await asyncio.sleep(0.05)+ if stream_cancelled:+ raise asyncio.CancelledError()+ if False:+ yield ""++ with (+ patch(+ "marimo._server.api.endpoints.execution.wait_for_http_disconnect",+ wait_for_disconnect,+ ),+ patch.object(scratchpad_mod.ScratchCellListener, "stream", stream),+ patch.object(session, "put_control_request"),+ patch.object(session, "try_interrupt") as try_interrupt,+ ):+ response = client.post(+ "/api/kernel/execute",+ headers=HEADERS,+ json={"code": "x = 1"},+ )++ assert response.status_code == 200, response.text+ return try_interrupt.call_count++class TestExecutionRoutes_EditMode:@staticmethod@with_session(SESSION_ID)@@ -333,6 +383,49 @@ class TestExecutionRoutes_EditMode:assert cell_outputs.output[cell_id] is sampleassert cell_outputs.console_outputs[cell_id] == [sample_console]+ @staticmethod+ @with_session(SESSION_ID)+ def test_execute_interrupts_kernel_on_watched_disconnect(+ client: TestClient,+ ) -> None:+ """On ASGI spec >= 2.4 servers only the disconnect watcher sees+ the disconnect. It must interrupt the kernel."""+ assert (+ _count_execute_interrupts(+ client, watcher_fires=True, stream_cancelled=False+ )+ == 1+ )++ @staticmethod+ @with_session(SESSION_ID)+ def test_execute_interrupts_kernel_on_cancelled_response(+ client: TestClient,+ ) -> None:+ """On ASGI spec < 2.4 servers the response is cancelled and the+ watcher never fires. The cancellation must interrupt the kernel."""+ assert (+ _count_execute_interrupts(+ client, watcher_fires=False, stream_cancelled=True+ )+ == 1+ )++ @staticmethod+ @with_session(SESSION_ID)+ def test_execute_interrupts_kernel_once_per_disconnect(+ client: TestClient,+ ) -> None:+ """On ASGI spec 2.3 servers both the disconnect watcher and the+ response cancellation observe one client disconnect. The kernel+ must receive one interrupt, not two."""+ assert (+ _count_execute_interrupts(+ client, watcher_fires=True, stream_cancelled=True+ )+ == 1+ )+@staticmethod@with_session(SESSION_ID)def test_takeover_no_file_key(client: TestClient) -> None: