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(reload): do not re-stale cells that reran after a reload (#10912)

Dylan Madisettifc918bdcd097
diff --git a/marimo/_runtime/reload/autoreload.py b/marimo/_runtime/reload/autoreload.py
index 9be1168dc1..161fb7f851 100644
--- a/marimo/_runtime/reload/autoreload.py
+++ b/marimo/_runtime/reload/autoreload.py
@@ -29,6 +29,7 @@ from typing import TYPE_CHECKING, Any, Generic, TypeVar
from marimo import _loggers
from marimo._ast.cell import CellImpl
from marimo._messaging.tracebacks import write_traceback
+from marimo._types.ids import CellId_t
if TYPE_CHECKING:
from collections.abc import Callable
@@ -210,7 +211,14 @@ class ModuleReloader:
# set of modules names known to be stale but haven't been reloaded
self.stale_modules: set[str] = set()
# for thread-safety
- self.lock = threading.Lock()
+ self.lock = threading.RLock()
+ # Bumped once per reload pass, even one that fails.
+ self.reload_generation = 0
+ # cell -> (generation, ordinal) of its last run.
+ self._cell_runs: dict[CellId_t, tuple[int, int]] = {}
+ self._run_counter = 0
+ # source path -> (mtime, generation) of its last successful reload
+ self._reloaded_sources: dict[str, tuple[float, int]] = {}
self._module_dependency_finder = ModuleDependencyFinder()
# modname -> cached `__file__` for modules classified as non-user.
# Populated by every `check()` call (memoizing `is_user_module`);
@@ -220,8 +228,11 @@ class ModuleReloader:
# module shadowing an installed package).
self._skip: dict[str, str | None] = {}
+ # module name -> mtime last seen by the module watcher
+ self.watcher_modules_mtimes: dict[str, float] = {}
+
# Timestamp existing modules
- self.check(modules=sys.modules, reload=False)
+ self.check_for_watcher(modules=sys.modules)
def filename_and_mtime(
self, module: types.ModuleType
@@ -257,6 +268,49 @@ class ModuleReloader:
return None
return ModuleMTime(py_filename, pymtime)
+ def record_cell_run(self, cell_id: CellId_t) -> None:
+ """Record a run of `cell_id` under the current generation."""
+ with self.lock:
+ self._run_counter += 1
+ self._cell_runs[cell_id] = (
+ self.reload_generation,
+ self._run_counter,
+ )
+
+ def forget_cell(self, cell_id: CellId_t) -> None:
+ """Drop the run record of a cell that left the graph."""
+ with self.lock:
+ self._cell_runs.pop(cell_id, None)
+
+ def cell_ran_at_or_after(self, cell_id: CellId_t, generation: int) -> bool:
+ """Whether `cell_id` last ran under `generation` or a later one."""
+ with self.lock:
+ return self._cell_runs.get(cell_id, (0, 0))[0] >= generation
+
+ def cell_ran_after(self, cell_id: CellId_t, other: CellId_t) -> bool:
+ """Whether the last run of `cell_id` came after the last run of
+ `other`. False when either cell has no recorded run."""
+ with self.lock:
+ run = self._cell_runs.get(cell_id)
+ other_run = self._cell_runs.get(other)
+ return (
+ run is not None
+ and other_run is not None
+ and run[1] > other_run[1]
+ )
+
+ def required_generation(self, module: types.ModuleType) -> int:
+ """The generation a cell must have run under to hold the source of
+ `module` now on disk."""
+ with self.lock:
+ module_mtime = self.filename_and_mtime(module)
+ if module_mtime is None:
+ return self.reload_generation + 1
+ reloaded = self._reloaded_sources.get(module_mtime.name)
+ if reloaded is None or reloaded[0] != module_mtime.mtime:
+ return self.reload_generation + 1
+ return reloaded[1]
+
def cell_uses_stale_modules(self, cell: CellImpl) -> bool:
with self.lock:
return bool(
@@ -285,6 +339,21 @@ class ModuleReloader:
Returns a set of modules that were found to have been modified.
"""
+ return self._check(
+ modules,
+ reload=reload,
+ skip_non_user_modules=skip_non_user_modules,
+ for_watcher=False,
+ )
+
+ def _check(
+ self,
+ modules: dict[str, types.ModuleType],
+ *,
+ reload: bool,
+ skip_non_user_modules: bool,
+ for_watcher: bool,
+ ) -> set[types.ModuleType]:
# module watcher thread and kernel thread might try to use the
# reloader at the same time, but reloader mutates state
@@ -314,6 +383,7 @@ class ModuleReloader:
# from a clean mtime baseline.
del self._skip[modname]
self.modules_mtimes.pop(modname, None)
+ self.watcher_modules_mtimes.pop(modname, None)
self.stale_modules.discard(modname)
is_non_user = False
else:
@@ -330,24 +400,36 @@ class ModuleReloader:
py_filename, pymtime = module_mtime.name, module_mtime.mtime
existing_mtime = self.modules_mtimes.get(modname)
- if existing_mtime is None:
+ reloader_detected_change = (
+ existing_mtime is not None
+ and pymtime > existing_mtime
+ and self.failed.get(py_filename) != pymtime
+ )
+ if existing_mtime is None or pymtime > existing_mtime:
self.modules_mtimes[modname] = pymtime
- continue
- if pymtime <= existing_mtime:
- continue
- if self.failed.get(py_filename, None) == pymtime:
- continue
- self.modules_mtimes[modname] = pymtime
- modified_modules.add(m)
- self.stale_modules.add(modname)
- self._module_dependency_finder.evict_from_cache(m)
+ watcher_detected_change = False
+ if for_watcher:
+ watcher_mtime = self.watcher_modules_mtimes.get(modname)
+ watcher_detected_change = (
+ watcher_mtime is not None and pymtime > watcher_mtime
+ )
+ if watcher_mtime is None or pymtime > watcher_mtime:
+ self.watcher_modules_mtimes[modname] = pymtime
+
+ if reloader_detected_change:
+ self.stale_modules.add(modname)
+ self._module_dependency_finder.evict_from_cache(m)
+
+ if watcher_detected_change or reloader_detected_change:
+ modified_modules.add(m)
if not reload:
return modified_modules
# Pre-filter stale modules to only those present in modules dict
relevant_stale_modules = self.stale_modules & modules.keys()
+ generation_bumped = False
for modname in relevant_stale_modules:
# Reload after the check loop: if there are any
# previously discovered stale modules, reload those as well
@@ -358,6 +440,9 @@ class ModuleReloader:
continue
py_filename, pymtime = module_mtime.name, module_mtime.mtime
+ if not generation_bumped:
+ self.reload_generation += 1
+ generation_bumped = True
LOGGER.debug(f"Reloading '{modname}'.")
try:
superreload(m, self.old_objects)
@@ -370,6 +455,10 @@ class ModuleReloader:
)
self.failed[py_filename] = pymtime
else:
+ self._reloaded_sources[py_filename] = (
+ pymtime,
+ self.reload_generation,
+ )
# TODO or always evict?
self._module_dependency_finder.evict_from_cache(m)
@@ -383,6 +472,18 @@ class ModuleReloader:
module, excludes
)
+ def check_for_watcher(
+ self, modules: dict[str, types.ModuleType]
+ ) -> set[types.ModuleType]:
+ """Return modules that changed since the previous watcher poll."""
+ # One scan updates both mtime baselines.
+ return self._check(
+ modules,
+ reload=False,
+ skip_non_user_modules=False,
+ for_watcher=True,
+ )
+
def update_function(old: object, new: object) -> None:
"""Upgrade the code object of a function"""
diff --git a/marimo/_runtime/reload/manager.py b/marimo/_runtime/reload/manager.py
index ebb72e0b90..fb8cfedc14 100644
--- a/marimo/_runtime/reload/manager.py
+++ b/marimo/_runtime/reload/manager.py
@@ -15,8 +15,12 @@ if TYPE_CHECKING:
from collections.abc import Iterator
from marimo._ast.cell import CellImpl
- from marimo._runtime.runner.hook_context import OnFinishHookContext
+ from marimo._runtime.runner.hook_context import (
+ OnFinishHookContext,
+ PreExecutionHookContext,
+ )
from marimo._runtime.runtime import Kernel
+ from marimo._types.ids import CellId_t
AutoReloadMode = Literal["off", "lazy", "autorun"]
@@ -28,7 +32,9 @@ class AutoreloadManager:
self._kernel = kernel
self._reloader: ModuleReloader | None = None
self._watcher: ModuleWatcher | None = None
+ self._running_cell: CellId_t | None = None
+ kernel._hooks.add_pre_execution(self._pre_execution_hook)
# Re-arm the watcher after every kernel run, regardless of trigger.
kernel._hooks.add_on_finish(self._on_finish_hook)
@@ -78,23 +84,42 @@ class AutoreloadManager:
if reloader.cell_uses_stale_modules(cell):
self._kernel.graph.set_stale({cell.cell_id}, prune_imports=True)
+ def forget_cell(self, cell_id: CellId_t) -> None:
+ """Drop per-cell reload bookkeeping for a cell leaving the graph."""
+ if self._reloader is not None:
+ self._reloader.forget_cell(cell_id)
+
+ def _pre_execution_hook(
+ self, cell: CellImpl, ctx: PreExecutionHookContext
+ ) -> None:
+ del ctx
+ # Only the runner fires this hook. Callbacks, RPCs and the debugger
+ # reload without recording a run.
+ self._running_cell = cell.cell_id
+
@contextlib.contextmanager
def cell_scope(self) -> Iterator[None]:
"""Reload modified modules on entry; record mtimes for newly-imported modules on exit."""
+ cell_id, self._running_cell = self._running_cell, None
if self._reloader is None:
yield
return
snapshot = set(sys.modules)
# Entry: skip stdlib/site-packages so cells don't pay for stat-ing
# them. This is the perf-critical call.
- self._reloader.check(
- modules=sys.modules, reload=True, skip_non_user_modules=True
- )
+ # Hold the lock across both so the watcher cannot see the reload
+ # before the run record.
+ with self._reloader.lock:
+ self._reloader.check(
+ modules=sys.modules, reload=True, skip_non_user_modules=True
+ )
+ if cell_id is not None:
+ self._reloader.record_cell_run(cell_id)
try:
yield
finally:
# Exit: record mtimes for modules the cell just imported. Don't
- # skip here — `new_modules` is small (typically 0-3) and we need
+ # skip here: `new_modules` is small (typically 0-3) and we need
# an mtime baseline for newly-imported installed packages so the
# next edit isn't silently treated as the initial state.
new_modules = set(sys.modules) - snapshot
diff --git a/marimo/_runtime/reload/module_watcher.py b/marimo/_runtime/reload/module_watcher.py
index 339d007066..90ad4cc061 100644
--- a/marimo/_runtime/reload/module_watcher.py
+++ b/marimo/_runtime/reload/module_watcher.py
@@ -123,29 +123,39 @@ def _check_modules(
modules: dict[str, types.ModuleType],
reloader: ModuleReloader,
sys_modules: dict[str, types.ModuleType],
-) -> dict[str, types.ModuleType]:
- """Returns the set of modules used by the graph that have been modified"""
- modified_modules = reloader.check(modules=sys_modules, reload=False)
+) -> dict[str, int]:
+ """Returns the modules used by the graph that depend on a modified
+ module, each mapped to the reload generation that holds the change."""
+ # One lock hold, so the generation matches the change this scan saw.
+ with reloader.lock:
+ modified_modules = reloader.check_for_watcher(modules=sys_modules)
+ generations = {
+ m: reloader.required_generation(m)
+ for m in modified_modules
+ if m is not None
+ }
# TODO(akshayka): could also exclude modules part of the standard library;
# haven't found a reliable way to do this, however.
excludes = _get_excluded_modules(sys_modules)
- target_modules = {m for m in modified_modules if m is not None}
- target_filenames = {
- t.__file__ for t in target_modules if hasattr(t, "__file__")
- }
-
- stale_modules: dict[str, types.ModuleType] = {
- modname: module
- for modname, module in modules.items()
- if _depends_on(
- src_module=module,
- target_modules=target_modules,
- target_filenames=target_filenames,
- excludes=excludes,
- reloader=reloader,
+ stale_modules: dict[str, int] = {}
+ for target, generation in generations.items():
+ target_filenames = (
+ {target.__file__} if hasattr(target, "__file__") else set()
)
- }
+ for modname, module in modules.items():
+ if _depends_on(
+ src_module=module,
+ target_modules={target},
+ target_filenames=target_filenames,
+ excludes=excludes,
+ reloader=reloader,
+ ):
+ # A module with several changed targets holds them all only
+ # from the latest reload.
+ stale_modules[modname] = max(
+ stale_modules.get(modname, generation), generation
+ )
return stale_modules
@@ -177,13 +187,13 @@ def watch_modules(
while not should_exit.is_set():
# Collect the modules used by each cell
modules: dict[str, types.ModuleType] = {}
- modname_to_cell_id: dict[str, CellId_t] = {}
+ importers: dict[str, set[CellId_t]] = {}
with graph.lock:
for cell_id, cell in graph.cells.items():
for modname in modules_imported_by_cell(cell, sys_modules):
if modname in sys_modules:
modules[modname] = sys_modules[modname]
- modname_to_cell_id[modname] = cell_id
+ importers.setdefault(modname, set()).add(cell_id)
stale_modules = _check_modules(
modules=modules,
@@ -197,26 +207,59 @@ def watch_modules(
)
with graph.lock:
LOGGER.debug("Acquired graph lock.")
- for modname in stale_modules:
- # prune definitions that are derived from stale modules
- cell_id = modname_to_cell_id[modname]
- cell = graph.cells[cell_id]
- defs_to_prune = [
- import_data.definition
- for import_data in cell.imports
- if import_data.module == modname
- ]
- cell.import_workspace.imported_defs -= set(defs_to_prune)
-
- # If any modules are stale, communicate that to the FE
- # and update the backend's view of the importing cells'
- # staleness
- stale_cell_ids = dataflow.transitive_closure(
- graph,
- {modname_to_cell_id[modname] for modname in stale_modules},
- relatives=dataflow.get_import_block_relatives(graph),
+ stale_importers: set[CellId_t] = set()
+ fresh_importers: dict[CellId_t, set[str]] = {}
+ for modname, generation in stale_modules.items():
+ for cell_id in importers[modname]:
+ importer = graph.cells.get(cell_id)
+ if importer is None:
+ # Deleted while the watcher crawled.
+ continue
+ names = {
+ import_data.definition
+ for import_data in importer.imports
+ if import_data.module == modname
+ }
+ if reloader.cell_ran_at_or_after(cell_id, generation):
+ fresh_importers.setdefault(cell_id, set())
+ fresh_importers[cell_id] |= names
+ continue
+ stale_importers.add(cell_id)
+ # Pruning reopens the traversal through this cell
+ # to the readers of these names.
+ importer.import_workspace.imported_defs -= names
+
+ relatives = dataflow.get_import_block_relatives(graph)
+ # A stale importer's readers hold its old bindings,
+ # however recently they ran.
+ cells_to_mark = dataflow.transitive_closure(
+ graph, stale_importers, relatives=relatives
)
- for cid in stale_cell_ids:
+ for cell_id, names in fresh_importers.items():
+ # The kernel does not rerun readers of names an import
+ # block already imported. A reader is current only if it
+ # and every cell between it and the importer ran after
+ # the importer.
+ frontier = {
+ reader
+ for name in names
+ for reader in graph.get_referring_cells(
+ name, language="python"
+ )
+ }
+ seen: set[CellId_t] = set()
+ while frontier:
+ cid = frontier.pop()
+ if cid in seen or cid in cells_to_mark:
+ continue
+ seen.add(cid)
+ if reloader.cell_ran_after(cid, cell_id):
+ frontier |= relatives(cid, True)
+ continue
+ cells_to_mark |= dataflow.transitive_closure(
+ graph, {cid}, relatives=relatives
+ )
+ for cid in cells_to_mark:
graph.cells[cid].set_stale(stale=True, stream=stream)
LOGGER.debug("Released graph lock and updated stale statuses.")
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.py
index 03cfa9d0a6..a4d7d23aaa 100644
--- a/marimo/_runtime/runtime.py
+++ b/marimo/_runtime/runtime.py
@@ -1169,6 +1169,7 @@ class Kernel:
In contrast to deleting a cell, which fully scrubs the cell
from the kernel and graph.
"""
+ self.autoreload_manager.forget_cell(cell_id)
if cell_id not in self.errors:
self._invalidate_cell_state(cell_id, deletion=True)
return self.graph.delete_cell(cell_id)
diff --git a/tests/_runtime/reload/test_autoreload.py b/tests/_runtime/reload/test_autoreload.py
index ed8a6f7d3c..674f6d2292 100644
--- a/tests/_runtime/reload/test_autoreload.py
+++ b/tests/_runtime/reload/test_autoreload.py
@@ -29,6 +29,7 @@ from marimo._runtime.reload.autoreload import (
update_instances,
update_property,
)
+from marimo._types.ids import CellId_t
def test_reload_function(tmp_path: pathlib.Path, py_modname: str):
@@ -426,6 +427,47 @@ class TestModuleReloaderMethods:
reloader.check(sys.modules, reload=True)
assert len(reloader.stale_modules) == 0
+ def test_required_generation_follows_kernel_reloads(
+ self, tmp_path: pathlib.Path, py_modname: str
+ ):
+ """Until the kernel reloads an edit, only a future reload can bring
+ it in. After the reload, cells run under that generation hold it."""
+ sys.path.append(str(tmp_path))
+ py_file = tmp_path / pathlib.Path(py_modname + ".py")
+ py_file.write_text("x = 1")
+ mod = importlib.import_module(py_modname)
+
+ reloader = ModuleReloader()
+ pending = reloader.reload_generation + 1
+ assert reloader.required_generation(mod) == pending
+
+ update_file(py_file, "x = 2")
+ assert reloader.required_generation(mod) == pending
+
+ reloader.check(sys.modules, reload=True)
+ assert mod.x == 2
+ assert reloader.reload_generation == pending
+ assert reloader.required_generation(mod) == pending
+
+ update_file(py_file, "x = 3")
+ assert reloader.required_generation(mod) == pending + 1
+
+ def test_cell_run_records(self):
+ reloader = ModuleReloader()
+ first, second = CellId_t("0"), CellId_t("1")
+ reloader.reload_generation = 1
+ reloader.record_cell_run(first)
+ reloader.record_cell_run(second)
+ assert reloader.cell_ran_at_or_after(first, 1)
+ assert not reloader.cell_ran_at_or_after(first, 2)
+ assert reloader.cell_ran_after(second, first)
+ assert not reloader.cell_ran_after(first, second)
+
+ reloader.forget_cell(first)
+ assert not reloader.cell_ran_at_or_after(first, 1)
+ assert not reloader.cell_ran_after(second, first)
+ reloader.forget_cell(first)
+
class TestSkipCache:
def test_is_user_module_stdlib(self):
@@ -550,6 +592,61 @@ class TestSkipCache:
# Stale mtime is cleared so the next edit isn't masked by it.
assert reloader.modules_mtimes.get(py_modname, 0) < 1e12
+ def test_watcher_mtimes_cleared_when_module_rebound(
+ self, tmp_path: pathlib.Path, py_modname: str
+ ):
+ # A rebind must also clear the watcher's baseline, or the watcher
+ # misses a replacement file with an older mtime and every later
+ # edit until its mtime exceeds the old file's.
+ sys.path.append(str(tmp_path))
+ user_file = tmp_path / pathlib.Path(py_modname + ".py")
+ user_file.write_text("x = 1")
+ user_mod = importlib.import_module(py_modname)
+
+ fake_installed = types.ModuleType(py_modname)
+ fake_installed.__file__ = os.path.join(
+ os.path.dirname(os.__file__), py_modname + ".py"
+ )
+ sys.modules[py_modname] = fake_installed
+
+ reloader = ModuleReloader()
+ reloader.check(sys.modules, reload=False)
+ assert py_modname in reloader._skip
+ # Synthetic far-future baseline standing in for the old file's mtime.
+ reloader.watcher_modules_mtimes[py_modname] = 1e12
+
+ sys.modules[py_modname] = user_mod
+ reloader.check(sys.modules, reload=False)
+ assert reloader.watcher_modules_mtimes.get(py_modname, 0) < 1e12
+
+ reloader.check_for_watcher(sys.modules)
+ update_file(user_file, "x = 2")
+ assert any(
+ m is user_mod for m in reloader.check_for_watcher(sys.modules)
+ )
+
+ def test_watcher_stats_each_module_once(
+ self, tmp_path: pathlib.Path, py_modname: str, monkeypatch
+ ):
+ sys.path.append(str(tmp_path))
+ user_file = tmp_path / pathlib.Path(py_modname + ".py")
+ user_file.write_text("x = 1")
+ user_mod = importlib.import_module(py_modname)
+ reloader = ModuleReloader()
+
+ calls: list[types.ModuleType] = []
+ original = reloader.filename_and_mtime
+
+ def spy(module: types.ModuleType):
+ calls.append(module)
+ return original(module)
+
+ monkeypatch.setattr(reloader, "filename_and_mtime", spy)
+
+ reloader.check_for_watcher({py_modname: user_mod})
+
+ assert calls == [user_mod]
+
def test_normalized_path_cached_across_checks(self):
# Regression guard: os.path.realpath is expensive (filesystem syscalls
# per path component). _normalized_path caches it so repeated check()
diff --git a/tests/_runtime/reload/test_module_watcher.py b/tests/_runtime/reload/test_module_watcher.py
index 27e872a538..6366370837 100644
--- a/tests/_runtime/reload/test_module_watcher.py
+++ b/tests/_runtime/reload/test_module_watcher.py
@@ -5,15 +5,21 @@ import copy
import pathlib
import sys
import textwrap
+import threading
import types
from queue import Queue
+from typing import TYPE_CHECKING, Any
import pytest
from reload_test_utils import random_modname, update_file
+import marimo._runtime.reload.module_watcher as mw
from marimo._config.config import DEFAULT_CONFIG
from marimo._dependencies.dependencies import DependencyManager
-from marimo._runtime.commands import UpdateUserConfigCommand
+from marimo._runtime.commands import (
+ DeleteCellCommand,
+ UpdateUserConfigCommand,
+)
from marimo._runtime.reload.autoreload import ModuleReloader
from marimo._runtime.reload.module_watcher import (
_check_modules,
@@ -21,7 +27,10 @@ from marimo._runtime.reload.module_watcher import (
_get_excluded_modules,
)
from marimo._runtime.runtime import Kernel
-from tests.conftest import ExecReqProvider
+from tests.conftest import ExecReqProvider, MockedKernel
+
+if TYPE_CHECKING:
+ from collections.abc import Callable
INTERVAL = 0.2
@@ -29,8 +38,6 @@ INTERVAL = 0.2
@pytest.fixture(autouse=True)
def _setup_test_sleep():
"""Automatically set up faster sleep interval for all tests in this module"""
- import marimo._runtime.reload.module_watcher as mw
-
old_interval = mw._TEST_SLEEP_INTERVAL
mw._TEST_SLEEP_INTERVAL = INTERVAL
yield
@@ -616,6 +623,174 @@ async def test_reload_function_in_import_block(
assert k.globals["y"] == y
+async def test_reload_self_import_cycle(
+ tmp_path: pathlib.Path,
+ py_modname: str,
+ execution_kernel: Kernel,
+ exec_req: ExecReqProvider,
+):
+ k = execution_kernel
+ sys.path.append(str(tmp_path))
+
+ py_function_file = tmp_path / pathlib.Path(py_modname + "_helper" + ".py")
+ py_function_file.write_text(
+ textwrap.dedent(
+ f"""
+ from {py_modname} import func
+
+ def foo():
+ return func()*2
+ """
+ )
+ )
+
+ py_file = tmp_path / pathlib.Path(py_modname + ".py")
+ py_file.write_text(
+ textwrap.dedent(
+ """
+ def func():
+ return 5
+ """
+ )
+ )
+ k.app_metadata.filename = str(py_file)
+
+ config = copy.deepcopy(DEFAULT_CONFIG)
+ config["runtime"]["auto_reload"] = "lazy"
+ k.set_user_config(UpdateUserConfigCommand(config=config))
+ await k.run(
+ [
+ er_1 := exec_req.get(f"from {py_modname}_helper import foo"),
+ er_2 := exec_req.get("x = foo()"),
+ ]
+ )
+ assert k.globals["x"] == 10
+ update_file(py_file, "def func(): return 2")
+
+ assert await _wait_for(lambda: k.graph.cells[er_1.cell_id].stale)
+ assert k.graph.cells[er_2.cell_id].stale
+ await k.run_stale_cells()
+ assert k.globals["x"] == 4
+
+
+def _park_watcher(
+ monkeypatch: pytest.MonkeyPatch, reloader: ModuleReloader, modname: str
+) -> tuple[threading.Event, threading.Event]:
+ """Pause the watcher before its next scan, once it has an mtime baseline
+ for `modname`.
+
+ `parked` is set while the watcher waits. Set `release` to let it scan.
+ """
+ parked = threading.Event()
+ release = threading.Event()
+ real_check_modules = mw._check_modules
+
+ def gated(**kwargs: Any) -> dict[str, int]:
+ if modname in reloader.watcher_modules_mtimes and not release.is_set():
+ parked.set()
+ release.wait(timeout=10)
+ return real_check_modules(**kwargs)
+
+ monkeypatch.setattr(mw, "_check_modules", gated)
+ return parked, release
+
+
+def _park_watcher_in_crawl(
+ monkeypatch: pytest.MonkeyPatch,
+) -> tuple[threading.Event, threading.Event]:
+ """Pause the watcher inside its dependency crawl, between detecting a
+ change and marking cells stale.
+
+ `parked` is set while the watcher waits. Set `release` to let it mark.
+ """
+ parked = threading.Event()
+ release = threading.Event()
+ real_depends_on = mw._depends_on
+
+ def held(**kwargs: Any) -> bool:
+ parked.set()
+ release.wait(timeout=10)
+ return real_depends_on(**kwargs)
+
+ monkeypatch.setattr(mw, "_depends_on", held)
+ return parked, release
+
+
+async def _wait_for(predicate: Callable[[], bool], polls: int = 25) -> bool:
+ for _ in range(polls):
+ await asyncio.sleep(INTERVAL)
+ if predicate():
+ return True
+ return predicate()
+
+
+async def test_reload_self_import_cycle_race_with_other_cell(
+ tmp_path: pathlib.Path,
+ py_modname: str,
+ execution_kernel: Kernel,
+ exec_req: ExecReqProvider,
+ monkeypatch: pytest.MonkeyPatch,
+):
+ """A cell run that reloads the edited module must not hide the edit
+ from the watcher. Cells that import dependents of the module are still
+ marked stale (#10191)."""
+ k = execution_kernel
+ sys.path.append(str(tmp_path))
+
+ py_function_file = tmp_path / pathlib.Path(py_modname + "_helper" + ".py")
+ py_function_file.write_text(
+ textwrap.dedent(
+ f"""
+ from {py_modname} import func
+
+ def foo():
+ return func()*2
+ """
+ )
+ )
+
+ py_file = tmp_path / pathlib.Path(py_modname + ".py")
+ py_file.write_text(
+ textwrap.dedent(
+ """
+ def func():
+ return 5
+ """
+ )
+ )
+ k.app_metadata.filename = str(py_file)
+
+ config = copy.deepcopy(DEFAULT_CONFIG)
+ config["runtime"]["auto_reload"] = "autorun"
+ k.set_user_config(UpdateUserConfigCommand(config=config))
+ await k.run(
+ [
+ er_1 := exec_req.get(f"from {py_modname}_helper import foo"),
+ er_2 := exec_req.get("x = foo()"),
+ er_3 := exec_req.get("pass"),
+ ]
+ )
+ assert k.globals["x"] == 10
+
+ reloader = k.autoreload_manager._reloader
+ assert reloader is not None
+ parked, release = _park_watcher(monkeypatch, reloader, py_modname)
+ assert await _wait_for(parked.is_set)
+
+ # The unrelated cell's run reloads the edited module first and consumes
+ # the mtime change the kernel tracks.
+ update_file(py_file, "def func(): return 2")
+ await k.run([exec_req.get_with_id(er_3.cell_id, "pass")])
+ assert not k.graph.cells[er_1.cell_id].stale
+ release.set()
+
+ assert await _wait_for(lambda: k.graph.cells[er_1.cell_id].stale)
+ assert k.graph.cells[er_2.cell_id].stale
+ await k.run_stale_cells()
+ assert k.globals["x"] == 4
+
+
class TestIsSubmodule:
"""Unit tests for the is_submodule utility function"""
@@ -1200,3 +1375,468 @@ class TestAutoreloadManagerLifecycle:
watcher.run_is_processed.clear()
await k.run([exec_req.get("x = 1")])
assert watcher.run_is_processed.is_set()
+
+
+async def test_watcher_does_not_restale_cells_rerun_after_reload(
+ tmp_path: pathlib.Path,
+ py_modname: str,
+ execution_kernel: Kernel,
+ exec_req: ExecReqProvider,
+ monkeypatch: pytest.MonkeyPatch,
+):
+ """Cells that rerun while the watcher crawls dependencies are not marked
+ stale afterwards.
+
+ The gap between detection and marking can hold a second save, such as
+ format-on-save. The kernel then reloads and reruns the importing cells
+ before the watcher marks them.
+ """
+ k = execution_kernel
+ sys.path.append(str(tmp_path))
+ py_file = tmp_path / pathlib.Path(py_modname + ".py")
+ py_file.write_text("def foo():\n return 1\n")
+
+ config = copy.deepcopy(DEFAULT_CONFIG)
+ config["runtime"]["auto_reload"] = "lazy"
+ k.set_user_config(UpdateUserConfigCommand(config=config))
+ await k.run(
+ [
+ er_1 := exec_req.get(f"from {py_modname} import foo"),
+ er_2 := exec_req.get("x = foo()"),
+ ]
+ )
+ assert k.globals["x"] == 1
+
+ update_file(py_file, "def foo():\n return 2\n")
+ assert await _wait_for(lambda: k.graph.cells[er_1.cell_id].stale)
+
+ parked, release = _park_watcher_in_crawl(monkeypatch)
+
+ update_file(py_file, "def foo():\n return 3\n")
+ assert await _wait_for(parked.is_set)
+
+ await k.run_stale_cells()
+ assert k.globals["x"] == 3
+ assert not k.graph.cells[er_1.cell_id].stale
+ assert not k.graph.cells[er_2.cell_id].stale
+
+ release.set()
+ await asyncio.sleep(5 * INTERVAL)
+
+ assert not k.graph.cells[er_1.cell_id].stale
+ assert not k.graph.cells[er_2.cell_id].stale
+ # The late marking must not prune the import block's metadata either.
+ # Otherwise its next run reruns every descendant.
+ assert k.graph.cells[er_1.cell_id].import_workspace.imported_defs == {
+ "foo"
+ }
+
+
+async def test_watcher_marks_cell_whose_context_ran_without_rerun(
+ tmp_path: pathlib.Path,
+ py_modname: str,
+ execution_kernel: Kernel,
+ exec_req: ExecReqProvider,
+ monkeypatch: pytest.MonkeyPatch,
+):
+ """A UI callback enters the cell's execution context without running
+ its code. No rerun occurs, so the watcher still marks the cell stale."""
+ k = execution_kernel
+ sys.path.append(str(tmp_path))
+ py_file = tmp_path / pathlib.Path(py_modname + ".py")
+ py_file.write_text("def foo():\n return 1\n")
+
+ config = copy.deepcopy(DEFAULT_CONFIG)
+ config["runtime"]["auto_reload"] = "lazy"
+ k.set_user_config(UpdateUserConfigCommand(config=config))
+ await k.run([er_1 := exec_req.get(f"from {py_modname} import foo")])
+
+ parked, release = _park_watcher_in_crawl(monkeypatch)
+ update_file(py_file, "def foo():\n return 2\n")
+ assert await _wait_for(parked.is_set)
+
+ # Non-cell work in the cell's context reloads modules but does not
+ # rerun the cell.
+ with k._install_execution_context(er_1.cell_id):
+ pass
+ assert not k.graph.cells[er_1.cell_id].stale
+
+ release.set()
+ assert await _wait_for(lambda: k.graph.cells[er_1.cell_id].stale)
+
+
+async def test_watcher_skips_cell_that_reran_before_its_poll(
+ tmp_path: pathlib.Path,
+ py_modname: str,
+ execution_kernel: Kernel,
+ exec_req: ExecReqProvider,
+ monkeypatch: pytest.MonkeyPatch,
+):
+ """Edit a module, then rerun its importing cell before the watcher
+ polls.
+
+ The rerun reloads the module, so the cell holds the new code. The
+ watcher sees the edit against its own mtime baseline but leaves that
+ cell and its imported definitions alone. Its reader still holds the
+ old value and is marked stale.
+ """
+ k = execution_kernel
+ sys.path.append(str(tmp_path))
+ py_file = tmp_path / pathlib.Path(py_modname + ".py")
+ py_file.write_text("def foo():\n return 1\n")
+
+ config = copy.deepcopy(DEFAULT_CONFIG)
+ config["runtime"]["auto_reload"] = "lazy"
+ k.set_user_config(UpdateUserConfigCommand(config=config))
+ await k.run(
+ [
+ er_1 := exec_req.get(f"from {py_modname} import foo"),
+ er_2 := exec_req.get("x = foo()"),
+ ]
+ )
+ assert k.globals["x"] == 1
+
+ reloader = k.autoreload_manager._reloader
+ assert reloader is not None
+ parked, release = _park_watcher(monkeypatch, reloader, py_modname)
+ assert await _wait_for(parked.is_set)
+
+ update_file(py_file, "def foo():\n return 2\n")
+ await k.run(
+ [exec_req.get_with_id(er_1.cell_id, f"from {py_modname} import foo")]
+ )
+ assert k.globals["foo"]() == 2
+ assert not k.graph.cells[er_1.cell_id].stale
+ assert not k.graph.cells[er_2.cell_id].stale
+
+ release.set()
+ # Wait until the watcher scans the edit, then give it one poll to
+ # finish marking.
+ assert await _wait_for(
+ lambda: (
+ reloader.watcher_modules_mtimes.get(py_modname)
+ == py_file.stat().st_mtime
+ )
+ )
+ await asyncio.sleep(2 * INTERVAL)
+
+ assert not k.graph.cells[er_1.cell_id].stale, (
+ "watcher flagged a cell that already reran with the new code"
+ )
+ assert k.graph.cells[er_1.cell_id].import_workspace.imported_defs == {
+ "foo"
+ }
+ assert k.graph.cells[er_2.cell_id].stale, (
+ "reader computed x from the old foo but is not stale"
+ )
+ await k.run_stale_cells()
+ assert k.globals["x"] == 2
+
+
+async def test_watcher_marks_descendant_when_import_cell_did_not_rerun(
+ tmp_path: pathlib.Path,
+ py_modname: str,
+ execution_kernel: Kernel,
+ exec_req: ExecReqProvider,
+ monkeypatch: pytest.MonkeyPatch,
+):
+ """A descendant that reruns before the watcher marks still reads the
+ importing cell's old binding, so it must be marked stale with it."""
+ k = execution_kernel
+ sys.path.append(str(tmp_path))
+ py_file = tmp_path / pathlib.Path(py_modname + ".py")
+ py_file.write_text("value = 1\n")
+
+ config = copy.deepcopy(DEFAULT_CONFIG)
+ config["runtime"]["auto_reload"] = "lazy"
+ k.set_user_config(UpdateUserConfigCommand(config=config))
+ await k.run(
+ [
+ er_1 := exec_req.get(f"from {py_modname} import value"),
+ er_2 := exec_req.get("y = value"),
+ ]
+ )
+ assert k.globals["y"] == 1
+
+ parked, release = _park_watcher_in_crawl(monkeypatch)
+ update_file(py_file, "value = 2\n")
+ assert await _wait_for(parked.is_set)
+
+ # The rerun reloads the module but still reads the import cell's old
+ # binding.
+ await k.run([exec_req.get_with_id(er_2.cell_id, "y = value")])
+ assert k.globals["y"] == 1
+
+ release.set()
+ assert await _wait_for(lambda: k.graph.cells[er_1.cell_id].stale)
+ assert k.graph.cells[er_2.cell_id].stale
+
+ await k.run_stale_cells()
+ assert k.globals["y"] == 2
+
+
+async def test_watcher_marks_reader_that_ran_before_its_importer(
+ tmp_path: pathlib.Path,
+ py_modname: str,
+ execution_kernel: Kernel,
+ exec_req: ExecReqProvider,
+ monkeypatch: pytest.MonkeyPatch,
+):
+ """Both cells rerun under the new generation, reader first. The
+ reader still read the old binding, so only the reader is stale."""
+ k = execution_kernel
+ sys.path.append(str(tmp_path))
+ py_file = tmp_path / pathlib.Path(py_modname + ".py")
+ py_file.write_text("value = 1\n")
+
+ config = copy.deepcopy(DEFAULT_CONFIG)
+ config["runtime"]["auto_reload"] = "lazy"
+ k.set_user_config(UpdateUserConfigCommand(config=config))
+ await k.run(
+ [
+ er_1 := exec_req.get(f"from {py_modname} import value"),
+ er_2 := exec_req.get("y = value"),
+ ]
+ )
+ reloader = k.autoreload_manager._reloader
+ assert reloader is not None
+ parked, release = _park_watcher(monkeypatch, reloader, py_modname)
+ assert await _wait_for(parked.is_set)
+
+ update_file(py_file, "value = 2\n")
+ await k.run([exec_req.get_with_id(er_2.cell_id, "y = value")])
+ await k.run(
+ [exec_req.get_with_id(er_1.cell_id, f"from {py_modname} import value")]
+ )
+ assert k.globals["value"] == 2
+ assert k.globals["y"] == 1
+
+ release.set()
+ assert await _wait_for(lambda: k.graph.cells[er_2.cell_id].stale)
+ assert not k.graph.cells[er_1.cell_id].stale
+ await k.run_stale_cells()
+ assert k.globals["y"] == 2
+
+
+async def test_watcher_marks_every_importer_of_a_module(
+ tmp_path: pathlib.Path,
+ py_modname: str,
+ execution_kernel: Kernel,
+ exec_req: ExecReqProvider,
+):
+ """Two cells import the same module under different aliases and a
+ third reads both. An edit marks both importers and the reader."""
+ k = execution_kernel
+ sys.path.append(str(tmp_path))
+ py_file = tmp_path / pathlib.Path(py_modname + ".py")
+ py_file.write_text("value = 1\n")
+
+ config = copy.deepcopy(DEFAULT_CONFIG)
+ config["runtime"]["auto_reload"] = "lazy"
+ k.set_user_config(UpdateUserConfigCommand(config=config))
+ await k.run(
+ [
+ er_a := exec_req.get(f"from {py_modname} import value as a"),
+ er_c := exec_req.get(f"from {py_modname} import value as c"),
+ er_b := exec_req.get("y = (a, c)"),
+ ]
+ )
+ assert k.globals["y"] == (1, 1)
+
+ update_file(py_file, "value = 2\n")
+ assert await _wait_for(
+ lambda: all(
+ k.graph.cells[er.cell_id].stale for er in (er_a, er_c, er_b)
+ )
+ ), "watcher marked only one importer of the module"
+ await k.run_stale_cells()
+ assert k.globals["y"] == (2, 2)
+
+
+async def test_watcher_survives_cell_deleted_during_crawl(
+ tmp_path: pathlib.Path,
+ py_modname: str,
+ execution_kernel: Kernel,
+ exec_req: ExecReqProvider,
+ monkeypatch: pytest.MonkeyPatch,
+):
+ k = execution_kernel
+ sys.path.append(str(tmp_path))
+ py_file = tmp_path / pathlib.Path(py_modname + ".py")
+ py_file.write_text("value = 1\n")
+ other_file = tmp_path / pathlib.Path(py_modname + "_other.py")
+ other_file.write_text("w = 1\n")
+
+ config = copy.deepcopy(DEFAULT_CONFIG)
+ config["runtime"]["auto_reload"] = "lazy"
+ k.set_user_config(UpdateUserConfigCommand(config=config))
+ await k.run(
+ [
+ er_1 := exec_req.get(f"from {py_modname} import value"),
+ er_2 := exec_req.get(f"from {py_modname}_other import w"),
+ ]
+ )
+
+ parked, release = _park_watcher_in_crawl(monkeypatch)
+ update_file(py_file, "value = 2\n")
+ assert await _wait_for(parked.is_set)
+ await k.delete_cell(DeleteCellCommand(cell_id=er_1.cell_id))
+ release.set()
+ await asyncio.sleep(4 * INTERVAL)
+
+ # The watcher thread is still alive and marks later edits.
+ update_file(other_file, "w = 2\n")
+ assert await _wait_for(lambda: k.graph.cells[er_2.cell_id].stale)
+
+
+async def test_watcher_marks_descendants_of_a_stale_reader(
+ tmp_path: pathlib.Path,
+ py_modname: str,
+ execution_kernel: Kernel,
+ exec_req: ExecReqProvider,
+ monkeypatch: pytest.MonkeyPatch,
+):
+ """Rerunning the importer and a grandchild, but not the child between
+ them, leaves the grandchild holding the child's old output. Running
+ after the importer does not make a cell current when its parent is
+ stale."""
+ k = execution_kernel
+ sys.path.append(str(tmp_path))
+ py_file = tmp_path / pathlib.Path(py_modname + ".py")
+ py_file.write_text("value = 1\n")
+
+ config = copy.deepcopy(DEFAULT_CONFIG)
+ config["runtime"]["auto_reload"] = "lazy"
+ k.set_user_config(UpdateUserConfigCommand(config=config))
+ await k.run(
+ [
+ er_a := exec_req.get(f"from {py_modname} import value"),
+ er_b := exec_req.get("y = value"),
+ er_c := exec_req.get("z = y"),
+ ]
+ )
+ reloader = k.autoreload_manager.reloader
+ assert reloader is not None
+ parked, release = _park_watcher(monkeypatch, reloader, py_modname)
+ assert await _wait_for(parked.is_set)
+
+ update_file(py_file, "value = 2\n")
+ await k.run([exec_req.get_with_id(er_a.cell_id, er_a.code)])
+ await k.run([exec_req.get_with_id(er_c.cell_id, er_c.code)])
+ assert k.globals["value"] == 2
+ assert k.globals["z"] == 1
+
+ release.set()
+ assert await _wait_for(lambda: k.graph.cells[er_b.cell_id].stale)
+ assert k.graph.cells[er_c.cell_id].stale, "grandchild holds the old y"
+ assert not k.graph.cells[er_a.cell_id].stale
+ await k.run_stale_cells()
+ assert k.globals["z"] == 2
+
+
+async def test_watcher_checks_fresh_imports_of_a_stale_importer(
+ tmp_path: pathlib.Path,
+ py_modname: str,
+ execution_kernel: Kernel,
+ exec_req: ExecReqProvider,
+ monkeypatch: pytest.MonkeyPatch,
+):
+ """An import block that is stale for one module is still checked for
+ the module it already reloaded, so the readers of that module are
+ marked too."""
+ k = execution_kernel
+ sys.path.append(str(tmp_path))
+ first = tmp_path / pathlib.Path(py_modname + ".py")
+ first.write_text("value = 1\n")
+ second = tmp_path / pathlib.Path(py_modname + "_second.py")
+ second.write_text("value = 1\n")
+
+ config = copy.deepcopy(DEFAULT_CONFIG)
+ config["runtime"]["auto_reload"] = "lazy"
+ k.set_user_config(UpdateUserConfigCommand(config=config))
+ code = (
+ f"from {py_modname} import value as a\n"
+ f"from {py_modname}_second import value as b"
+ )
+ await k.run(
+ [
+ er_a := exec_req.get(code),
+ er_x := exec_req.get("x = a"),
+ er_y := exec_req.get("y = b"),
+ ]
+ )
+ reloader = k.autoreload_manager.reloader
+ assert reloader is not None
+ parked, release = _park_watcher(monkeypatch, reloader, py_modname)
+ assert await _wait_for(parked.is_set)
+
+ update_file(first, "value = 2\n")
+ await k.run([exec_req.get_with_id(er_a.cell_id, code)])
+ update_file(second, "value = 2\n")
+ assert (k.globals["a"], k.globals["x"]) == (2, 1)
+
+ release.set()
+ assert await _wait_for(lambda: k.graph.cells[er_a.cell_id].stale)
+ assert k.graph.cells[er_x.cell_id].stale, "reader of the reloaded module"
+ assert k.graph.cells[er_y.cell_id].stale
+ await k.run_stale_cells()
+ assert (k.globals["x"], k.globals["y"]) == (2, 2)
+
+
+async def test_reload_in_cell_run_can_create_ui_elements(
+ tmp_path: pathlib.Path,
+ py_modname: str,
+ mocked_kernel: MockedKernel,
+ exec_req: ExecReqProvider,
+):
+ """A module that builds a UI element at import reloads inside the
+ cell's execution context."""
+ k = mocked_kernel.k
+ sys.path.append(str(tmp_path))
+ py_file = tmp_path / pathlib.Path(py_modname + ".py")
+ py_file.write_text(
+ "import marimo as mo\nslider = mo.ui.slider(0, 10, value=1)\n"
+ )
+
+ config = copy.deepcopy(DEFAULT_CONFIG)
+ config["runtime"]["auto_reload"] = "lazy"
+ k.set_user_config(UpdateUserConfigCommand(config=config))
+ await k.run([er := exec_req.get(f"from {py_modname} import slider")])
+ assert k.globals["slider"].value == 1
+
+ update_file(
+ py_file,
+ "import marimo as mo\nslider = mo.ui.slider(0, 10, value=2)\n",
+ )
+ await k.run([exec_req.get_with_id(er.cell_id, er.code)])
+ assert k.graph.cells[er.cell_id].run_result_status == "success"
+ assert k.globals["slider"].value == 2
+
+
+async def test_reload_output_goes_to_the_cell(
+ tmp_path: pathlib.Path,
+ py_modname: str,
+ mocked_kernel: MockedKernel,
+ exec_req: ExecReqProvider,
+):
+ """Prints and errors raised while a module reloads belong to the cell
+ whose run triggered the reload."""
+ k = mocked_kernel.k
+ sys.path.append(str(tmp_path))
+ py_file = tmp_path / pathlib.Path(py_modname + ".py")
+ py_file.write_text("value = 1\n")
+
+ config = copy.deepcopy(DEFAULT_CONFIG)
+ config["runtime"]["auto_reload"] = "lazy"
+ k.set_user_config(UpdateUserConfigCommand(config=config))
+ await k.run([er := exec_req.get(f"import {py_modname}")])
+
+ mocked_kernel.stdout.messages.clear()
+ update_file(py_file, "print('reload output')\nvalue = 2\n")
+ await k.run([exec_req.get_with_id(er.cell_id, er.code)])
+ assert "reload output" in "".join(mocked_kernel.stdout.messages)
+
+ mocked_kernel.stderr.messages.clear()
+ update_file(py_file, "value = !\n")
+ await k.run([exec_req.get_with_id(er.cell_id, er.code)])
+ assert "SyntaxError" in "".join(mocked_kernel.stderr.messages)