Skip to content

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.

Fix: Graceful exit when cell is executing (#253)

Akshay Agrawalca1915e8eabd
diff --git a/marimo/_runtime/cell_runner.py b/marimo/_runtime/cell_runner.py
index 2b6f1e6684..2c876c51fa 100644
--- a/marimo/_runtime/cell_runner.py
+++ b/marimo/_runtime/cell_runner.py
@@ -82,7 +82,7 @@ class RunResult:
# Raw output of cell
output: Any
# Exception raised by cell, if any
- exception: Optional[BaseException]
+ exception: Optional[Exception | MarimoInterrupt | MarimoStopError]
def success(self) -> bool:
"""Whether the cell exected successfully"""
@@ -240,10 +240,11 @@ class Runner:
run_result = RunResult(output=e.output, exception=e)
# don't print a traceback, since quitting is the intended
# behavior (like sys.exit())
- except BaseException as e: # noqa: E722
- # except BaseException to catch everything, including
- # KeyboardInterrupt: nothing should go uncaught
- # cancel only the descendants of this cell
+ except Exception as e:
+ # Don't catch BaseExceptions:
+ # - KeyboardInterrupt shouldn't be raised, since marimo
+ # redirects it to a MarimoInterrupt
+ # - SystemExit should kill the process
self.cancel(cell_id)
run_result = RunResult(output=None, exception=e)
self.print_traceback()
diff --git a/marimo/_runtime/test_runtime.py b/marimo/_runtime/test_runtime.py
index 479a4623e1..4605e22d74 100644
--- a/marimo/_runtime/test_runtime.py
+++ b/marimo/_runtime/test_runtime.py
@@ -603,25 +603,3 @@ def test_interrupt(k: Kernel, exec_req: ExecReqProvider) -> None:
# make sure the interrupt wasn't caught by the try/except
assert k.globals["tries"] == 0
-
-
-def test_keyboard_interrupt_doesnt_crash_marimo(
- k: Kernel, exec_req: ExecReqProvider
-) -> None:
- er = exec_req.get(
- """
- from marimo._runtime.control_flow import MarimoInterrupt
-
- tries = 0
- while tries < 5:
- try:
- raise KeyboardInterrupt
- except Exception:
- ...
- tries += 1
- """
- )
- k.run([er])
-
- # make sure the kernel didn't crash ...
- assert k.globals["tries"] == 0