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: Only trigger cell when ALL its dependencies are unblocked (#8374)
Julien Lehuen9de151cbed1a
diff --git a/marimo/_runtime/runner/cell_runner.py b/marimo/_runtime/runner/cell_runner.pyindex 65b0c0cd3e..65a851135a 100644--- a/marimo/_runtime/runner/cell_runner.py+++ b/marimo/_runtime/runner/cell_runner.py@@ -665,6 +665,24 @@ class Runner:blamed_cell = var_cell_idreturn ref, blamed_cell+ def _find_first_blocked_missing_ref(+ self, cell_id: CellId_t+ ) -> Optional[CellId_t]:+ """Return the first out-of-run ancestor still in a stopped/errored state."""+ cell = self.graph.cells[cell_id]+ for ref in cell.refs:+ if ref in self.glbls:+ # The reference is already available+ continue+ for defining_cell_id in self.graph.get_defining_cells(ref):+ if defining_cell_id in self._run_position:+ # The defining cell is part of this run+ continue+ defining_cell = self.graph.cells[defining_cell_id]+ if defining_cell.run_result_status == "exception":+ return defining_cell_id+ return None+async def run_all(self) -> None:from marimo._runtime.runner.hook_context import (OnFinishHookContext,@@ -708,6 +726,31 @@ class Runner:LOGGER.debug("Cell runner processing %s", cell_id)cell = self.graph.cells[cell_id]+ blocked_ancestor_id = self._find_first_blocked_missing_ref(cell_id)+ if blocked_ancestor_id is not None:+ # Cancel cell_id and descendants before the check below.+ LOGGER.debug(+ "%s cancelled: ancestor %s still stopped",+ cell_id,+ blocked_ancestor_id,+ )+ if blocked_ancestor_id not in self.exceptions:+ # TODO (jlehuen): We wouldn't have to create this MarimoStopError if those were stored directly+ # in CellImpl.exception. See: marimo._runtime.runner.hooks_post_execution._set_run_result_status+ self.exceptions[blocked_ancestor_id] = self.graph.cells[+ blocked_ancestor_id+ ].exception or MarimoStopError(None)+ pending = {cell_id} | {+ cid+ for cid in dataflow.transitive_closure(+ self.graph, {cell_id}, inclusive=False+ )+ if cid in self.cells_to_run+ }+ for cid in pending:+ self.graph.cells[cid].set_run_result_status("cancelled")+ self.cancelled_cells.add(blocked_ancestor_id, pending)+# Update run result status for cells that won't run.## Hack: frontend sets status to queued on run, so we also have todiff --git a/marimo/_runtime/runner/hook_context.py b/marimo/_runtime/runner/hook_context.pyindex bf1465c5f9..8dc10d8af4 100644--- a/marimo/_runtime/runner/hook_context.py+++ b/marimo/_runtime/runner/hook_context.py@@ -35,7 +35,10 @@ class CancelledCells:def add(self, raising_cell: CellId_t, descendants: set[CellId_t]) -> None:"""Record that raising_cell caused descendants to be cancelled."""- self._by_raising_cell[raising_cell] = descendants+ if raising_cell in self._by_raising_cell:+ self._by_raising_cell[raising_cell].update(descendants)+ else:+ self._by_raising_cell[raising_cell] = descendantsself._all.update(descendants)def __contains__(self, cell_id: object) -> bool:diff --git a/tests/_runtime/runner/test_cancelled_cells.py b/tests/_runtime/runner/test_cancelled_cells.pyindex e3d8fc5861..5398a470c7 100644--- a/tests/_runtime/runner/test_cancelled_cells.py+++ b/tests/_runtime/runner/test_cancelled_cells.py@@ -60,6 +60,17 @@ class TestCancelledCells:assert CellId_t("c") in ccassert CellId_t("d") not in cc+ def test_same_raiser_accumulates_descendants(self) -> None:+ """add() for the same raising cell unions, not overwrites, descendants."""+ cc = CancelledCells()++ cc.add(CellId_t("r"), {CellId_t("a")})+ cc.add(CellId_t("r"), {CellId_t("b")})++ assert CellId_t("a") in cc+ assert CellId_t("b") in cc+ assert cc[CellId_t("r")] == {CellId_t("a"), CellId_t("b")}+def test_shared_reference_semantics(self) -> None:"""Mutations after passing to a frozen dataclass are visible."""cc = CancelledCells()diff --git a/tests/_runtime/runner/test_cell_runner.py b/tests/_runtime/runner/test_cell_runner.pyindex e24d6aeba9..d59a2d80d8 100644--- a/tests/_runtime/runner/test_cell_runner.py+++ b/tests/_runtime/runner/test_cell_runner.py@@ -207,3 +207,71 @@ async def test_ui_element_update_skips_overridden_cells(assert embed_result.defs["result"] == 200# x should still be the overridden value, not slider.valueassert embed_result.defs["x"] == 100+++async def test_converging_cell_stays_stopped_until_all_branches_trigger(+ k: Kernel, exec_req: ExecReqProvider+) -> None:+ await k.run(+ [+ exec_req.get("import marimo as mo"),+ exec_req.get_with_id("a", "mo.stop(True); a = 'A'"),+ exec_req.get("mo.stop(True); b = 'B'"),+ exec_req.get_with_id("res", "result = a + b"),+ ]+ )+ assert "a" not in k.globals+ assert "b" not in k.globals++ await k.run([exec_req.get_with_id("a", "a = 'A'")])++ assert "a" in k.globals+ assert "b" not in k.globals+ assert "result" not in k.globals+ assert k.graph.cells["res"].run_result_status == "cancelled"+++async def test_sibling_cells_of_stopped_ancestor_both_cancelled(+ k: Kernel, exec_req: ExecReqProvider+) -> None:+ await k.run(+ [+ exec_req.get("import marimo as mo"),+ exec_req.get_with_id("stopper", "mo.stop(True); s = 1"),+ exec_req.get_with_id("trigger", "t = 1"),+ exec_req.get_with_id("s1", "x = s + t"),+ exec_req.get_with_id("s2", "y = s + t"),+ ]+ )+ assert "x" not in k.globals+ assert "y" not in k.globals++ await k.run([exec_req.get_with_id("trigger", "t = 2")])++ assert "x" not in k.globals+ assert "y" not in k.globals+ assert k.graph.cells["s1"].run_result_status == "cancelled"+ assert k.graph.cells["s2"].run_result_status == "cancelled"+++async def test_converging_runs_when_all_branches_trigger(+ k: Kernel, exec_req: ExecReqProvider+) -> None:+ await k.run(+ [+ exec_req.get("import marimo as mo"),+ exec_req.get_with_id("a", "mo.stop(True); a = 'A'"),+ exec_req.get_with_id("b", "mo.stop(True); b = 'B'"),+ exec_req.get_with_id("res", "result = a + b"),+ ]+ )+ assert "a" not in k.globals+ assert "b" not in k.globals++ await k.run([exec_req.get_with_id("a", "a = 'A'")])+ await k.run([exec_req.get_with_id("b", "b = 'B'")])++ assert "a" in k.globals+ assert "b" in k.globals+ assert "result" in k.globals+ assert k.graph.cells["res"].run_result_status == "success"