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: Graceful exit when cell is executing (#253)
Akshay Agrawalca1915e8eabd
diff --git a/marimo/_runtime/cell_runner.py b/marimo/_runtime/cell_runner.pyindex 2b6f1e6684..2c876c51fa 100644--- a/marimo/_runtime/cell_runner.py+++ b/marimo/_runtime/cell_runner.py@@ -82,7 +82,7 @@ class RunResult:# Raw output of celloutput: 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 processself.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.pyindex 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/exceptassert 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