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: clear error state on disabled-transitively cells when ancestor recovers (#8784)

Vishak Baddurba12764ee3dd
diff --git a/frontend/playwright.config.ts b/frontend/playwright.config.ts
index ab0ce0f501..a4670f3b0d 100644
--- a/frontend/playwright.config.ts
+++ b/frontend/playwright.config.ts
@@ -38,6 +38,7 @@ const appToOptions = {
"bugs.py": { command: "edit" },
"cells.py": { command: "edit" },
"disabled_cells.py": { command: "edit" },
+ "disabled_ancestor_error.py": { command: "edit" },
"kitchen_sink.py": { command: "edit" },
"layout_grid.py": { command: "edit" },
"stdin.py": { command: "edit" },
diff --git a/marimo/_runtime/dataflow/graph.py b/marimo/_runtime/dataflow/graph.py
index a30a71735e..f974c3d348 100644
--- a/marimo/_runtime/dataflow/graph.py
+++ b/marimo/_runtime/dataflow/graph.py
@@ -142,6 +142,14 @@ class DirectedGraph(GraphTopology):
for cid in self.ancestors(cell_id)
)
+ def is_any_ancestor_errored(self, cell_id: CellId_t) -> bool:
+ """Check if any ancestor of a cell has an error."""
+ return any(
+ self.topology.cells[cid].run_result_status
+ in ("exception", "marimo-error")
+ for cid in self.ancestors(cell_id)
+ )
+
def disable_cell(self, cell_id: CellId_t) -> None:
"""Disables a cell in the graph.
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.py
index f1a004bc76..875ed7c41d 100644
--- a/marimo/_runtime/runtime.py
+++ b/marimo/_runtime/runtime.py
@@ -25,7 +25,7 @@ from typing import (
from uuid import uuid4
from marimo import _loggers
-from marimo._ast.cell import CellConfig, CellImpl
+from marimo._ast.cell import CellConfig, CellImpl, RuntimeStateType
from marimo._ast.compiler import _build_source_position_map, compile_cell
from marimo._ast.errors import ImportStarError
from marimo._ast.names import SETUP_CELL_NAME
@@ -1401,12 +1401,39 @@ class Kernel:
# common cases. We could also be more aggressive and run this before
# every cell, or even before pickle.dump/pickle.dumps()
with patches.patch_main_module_context(self._module):
+ # Snapshot disabled cells that are in an error/cancelled state
+ # BEFORE running, so we can clear them after the run if their
+ # ancestor recovered.
+ pre_run_errored_disabled = {
+ cid
+ for cid, cell in self.graph.cells.items()
+ if self.graph.is_disabled(cid)
+ and cell.run_result_status
+ in ("exception", "marimo-error", "cancelled")
+ }
while cell_ids := await self._run_cells_internal(cell_ids):
LOGGER.debug("Running state updates ...")
if self.lazy() and cell_ids:
self.graph.set_stale(cell_ids, prune_imports=True)
break
LOGGER.debug("Finished run.")
+ # Clear stale error state from disabled cells whose ancestor
+ # recovered. Uses pre-run snapshot since run_result_status is
+ # updated during the run.
+ for cid in pre_run_errored_disabled:
+ cell_impl = self.graph.cells[cid]
+ if not self.graph.is_any_ancestor_errored(cid):
+ cell_impl.set_run_result_status("disabled")
+ status = cast(
+ RuntimeStateType,
+ "idle"
+ if cell_impl.config.disabled
+ else "disabled-transitively",
+ )
+ cell_impl.set_runtime_state(status)
+ CellNotificationUtils.broadcast_empty_output(
+ cell_id=cid, status=status
+ )
async def _if_autorun_then_run_cells(
self, cell_ids: set[CellId_t]
@@ -1814,6 +1841,7 @@ class Kernel:
relatives=dataflow.get_import_block_relatives(self.graph),
)
)
+
if self.module_watcher is not None:
self.module_watcher.run_is_processed.set()
diff --git a/marimo/_smoke_tests/disabled_ancestor_error.py b/marimo/_smoke_tests/disabled_ancestor_error.py
new file mode 100644
index 0000000000..339ba2c1bd
--- /dev/null
+++ b/marimo/_smoke_tests/disabled_ancestor_error.py
@@ -0,0 +1,48 @@
+# /// script
+# [tool.marimo.runtime]
+# auto_instantiate = true
+# ///
+
+import marimo
+
+__generated_with = "0.23.1"
+app = marimo.App()
+
+
+def _():
+ import marimo as mo
+
+ toggle = mo.ui.checkbox(label="Trigger ancestor error")
+ toggle
+ return (toggle,)
+
+
+def _(toggle):
+ if toggle.value:
+ raise ValueError("ancestor error")
+
+ x = 1
+ return (x,)
+
+
[email protected](disabled=True)
+def _(x):
+ y = x + 1
+ return (y,)
+
+
+def _(y):
+ z = y + 1
+ return
+
+
+def _():
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_runtime/test_dataflow.py b/tests/_runtime/test_dataflow.py
index e859e9e6ba..e220e57682 100644
--- a/tests/_runtime/test_dataflow.py
+++ b/tests/_runtime/test_dataflow.py
@@ -1579,3 +1579,55 @@ def test_prune_cells_for_overrides_preserves_order() -> None:
graph, execution_order, {"b": 100}
)
assert result == ["0", "2", "3"]
+
+
+def test_is_any_ancestor_errored() -> None:
+ """Test that is_any_ancestor_errored correctly detects ancestor errors."""
+ graph = dataflow.DirectedGraph()
+ # Create a chain: 0 -> 1 -> 2
+ code = "x = 0"
+ first_cell = parse_cell(code)
+ graph.register_cell("0", first_cell)
+ code = "y = x"
+ second_cell = parse_cell(code)
+ graph.register_cell("1", second_cell)
+ code = "z = y"
+ third_cell = parse_cell(code)
+ graph.register_cell("2", third_cell)
+
+ # No errors initially
+ assert not graph.is_any_ancestor_errored("0")
+ assert not graph.is_any_ancestor_errored("1")
+ assert not graph.is_any_ancestor_errored("2")
+
+ # Set cell 0 to exception state
+ graph.cells["0"].set_run_result_status("exception")
+ assert not graph.is_any_ancestor_errored("0") # no ancestors
+ assert graph.is_any_ancestor_errored("1") # parent 0 has error
+ assert graph.is_any_ancestor_errored("2") # grandparent 0 has error
+
+ # Fix cell 0 - clear the error
+ graph.cells["0"].set_run_result_status("success")
+ assert not graph.is_any_ancestor_errored("0")
+ assert not graph.is_any_ancestor_errored("1")
+ assert not graph.is_any_ancestor_errored("2")
+
+
+def test_is_any_ancestor_errored_marimo_error() -> None:
+ """Test that is_any_ancestor_errored detects marimo-error status too."""
+ graph = dataflow.DirectedGraph()
+ code = "x = 0"
+ first_cell = parse_cell(code)
+ graph.register_cell("0", first_cell)
+ code = "y = x"
+ second_cell = parse_cell(code)
+ graph.register_cell("1", second_cell)
+
+ # Set cell 0 to marimo-error state (e.g. registration/syntax error)
+ graph.cells["0"].set_run_result_status("marimo-error")
+ assert not graph.is_any_ancestor_errored("0") # no ancestors
+ assert graph.is_any_ancestor_errored("1") # parent 0 has marimo-error
+
+ # Fix cell 0
+ graph.cells["0"].set_run_result_status("success")
+ assert not graph.is_any_ancestor_errored("1")