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: edge cases with errors and staleness (#2264)

Akshay Agrawal83a360d7974c
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.py
index 02ee3e114c..a95221872a 100644
--- a/marimo/_runtime/runtime.py
+++ b/marimo/_runtime/runtime.py
@@ -654,9 +654,15 @@ class Kernel:
if previous_cell is not None:
LOGGER.debug("Deleting cell %s", cell_id)
previous_children = self._deactivate_cell(cell_id)
+
error = self._try_registering_cell(
cell_id, code, carried_imports=carried_imports
)
+ if error is not None and previous_cell is not None:
+ # The frontend keeps the cell around in the case of a
+ # registration error; let the FE know the cell is no longer
+ # stale.
+ previous_cell.set_stale(False)
# For any newly imported namespaces, add them to the metadata
#
@@ -1000,17 +1006,20 @@ class Kernel:
self.errors = all_errors
for cid in self.errors:
+ cell = self.graph.cells[cid] if cid in self.graph.cells else None
if (
- # Cells with syntax errors are not in the graph
- cid in self.graph.cells
- and not self.graph.cells[cid].config.disabled
+ cell is not None
+ and not cell.config.disabled
and self.graph.is_disabled(cid)
):
# this may be the first time we're seeing the cell: set its
# status
- self.graph.cells[cid].set_runtime_state(
- "disabled-transitively"
- )
+ cell.set_runtime_state("disabled-transitively")
+
+ if cell is not None:
+ # The error is up-to-date, since we just processed the graph
+ cell.set_stale(False)
+
CellOp.broadcast_error(
data=self.errors[cid],
clear_console=True,
@@ -1042,7 +1051,8 @@ class Kernel:
for cid in cells_transitioned_to_error
if cid in self.graph.children
]
- ),
+ )
+ - cells_with_errors_after_mutation,
cells_that_no_longer_have_errors,
)
)
diff --git a/tests/_runtime/test_runtime.py b/tests/_runtime/test_runtime.py
index 9488ccc4f0..66244c713d 100644
--- a/tests/_runtime/test_runtime.py
+++ b/tests/_runtime/test_runtime.py
@@ -14,6 +14,7 @@ from marimo._messaging.errors import (
DeleteNonlocalError,
Error,
MarimoStrictExecutionError,
+ MarimoSyntaxError,
MultipleDefinitionError,
)
from marimo._messaging.ops import CellOp
@@ -578,6 +579,93 @@ class TestExecution:
assert not k.graph.cells[er.cell_id].stale
assert k.globals["y"] == 2
+ async def test_cell_transitioned_to_error_is_not_stale(
+ self, lazy_kernel: Kernel
+ ) -> None:
+ k = lazy_kernel
+ await k.run(
+ [
+ ExecutionRequest(cell_id="0", code="x=0"),
+ ExecutionRequest(cell_id="1", code="x"),
+ ]
+ )
+
+ # make cell 1 stale
+ await k.run(
+ [
+ ExecutionRequest(cell_id="0", code="x=1"),
+ ]
+ )
+
+ # introduce an error to cell 1; it shouldn't be stale
+ await k.run(
+ [
+ ExecutionRequest(cell_id="1", code="x=0"),
+ ]
+ )
+ assert set(k.errors.keys()) == {"1"}
+ assert not k.graph.cells["1"].stale
+
+ async def test_cell_transitioned_to_syntax_error_is_not_stale(
+ self, lazy_kernel: Kernel
+ ) -> None:
+ k = lazy_kernel
+ await k.run(
+ [
+ ExecutionRequest(cell_id="0", code="x=0"),
+ ExecutionRequest(cell_id="1", code="x"),
+ ]
+ )
+
+ # make cell 1 stale
+ await k.run(
+ [
+ ExecutionRequest(cell_id="0", code="x=1"),
+ ]
+ )
+ cell = k.graph.cells["1"]
+ assert cell.stale
+
+ # introduce a syntax error to cell 1; it shouldn't be stale
+ await k.run(
+ [
+ ExecutionRequest(cell_id="1", code="x ^ !"),
+ ]
+ )
+ assert set(k.errors.keys()) == {"1"}
+ assert isinstance(k.errors["1"][0], MarimoSyntaxError)
+ assert not cell.stale
+
+ async def test_child_of_errored_cell_with_error_not_stale(
+ self,
+ any_kernel: Kernel,
+ ) -> None:
+ k = any_kernel
+ await k.run(
+ [
+ ExecutionRequest(cell_id="0", code="x=0"),
+ ]
+ )
+
+ # multiple definition error
+ await k.run(
+ [
+ ExecutionRequest(cell_id="1", code="y; x=1"),
+ ]
+ )
+
+ # 0 also has a multiple definition error; 1 now depends on 0, but it
+ # is errored and its error is up-to-date, so don't mark it as stale.
+ await k.run(
+ [
+ ExecutionRequest(cell_id="0", code="y = 0; x=1"),
+ ]
+ )
+
+ assert "x" not in k.globals
+ assert set(k.errors.keys()) == {"0", "1"}
+ assert not k.graph.cells["1"].stale
+
async def test_syntax_error(self, any_kernel: Kernel) -> None:
k = any_kernel
await k.run(