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: recover polling file watcher from transient file absence (#8860)
Krishna Chaitanya18617c23b3d2
diff --git a/marimo/_utils/file_watcher.py b/marimo/_utils/file_watcher.pyindex bf4f801b2a..4354acdc05 100644--- a/marimo/_utils/file_watcher.py+++ b/marimo/_utils/file_watcher.py@@ -53,6 +53,10 @@ class FileWatcher(ABC):class PollingFileWatcher(FileWatcher):POLL_SECONDS = 1.0 # Poll every 1s+ # Number of consecutive missing-file polls before giving up.+ # Some editors (e.g. vim) save by deleting and recreating files,+ # so the file may be transiently absent.+ MAX_MISSING_POLLS = 5def __init__(self,@@ -64,6 +68,7 @@ class PollingFileWatcher(FileWatcher):self._running = Falseself.loop = loopself.last_modified: Optional[float] = self._get_modified()+ self._missing_count = 0def start(self) -> None:self._running = True@@ -81,8 +86,26 @@ class PollingFileWatcher(FileWatcher):async def _poll(self) -> None:while self._running:if not await async_path.exists(self.path):- LOGGER.warning(f"File at {self.path} does not exist.")- raise FileNotFoundError(f"File at {self.path} does not exist.")+ self._missing_count += 1+ if self._missing_count >= self.MAX_MISSING_POLLS:+ LOGGER.warning(+ f"File at {self.path} does not exist after "+ f"{self.MAX_MISSING_POLLS} consecutive checks. "+ "Stopping file watcher."+ )+ self._running = False+ return+ LOGGER.debug(+ f"File at {self.path} temporarily missing "+ f"(attempt {self._missing_count}/"+ f"{self.MAX_MISSING_POLLS}). "+ "Will retry."+ )+ await asyncio.sleep(self.POLL_SECONDS)+ continue++ # File exists again; reset the missing counter+ self._missing_count = 0# Check for file changesmodified = self._get_modified()diff --git a/tests/_utils/test_file_watcher.py b/tests/_utils/test_file_watcher.pyindex 180db9bcef..d357bd9cc1 100644--- a/tests/_utils/test_file_watcher.py+++ b/tests/_utils/test_file_watcher.py@@ -6,6 +6,7 @@ import osimport shutilfrom pathlib import Pathfrom tempfile import NamedTemporaryFile+from unittest.mock import patchimport pytest@@ -151,6 +152,75 @@ async def test_file_watcher_manager() -> None:os.remove(tmp_path2)+@patch("marimo._utils.file_watcher.PollingFileWatcher.POLL_SECONDS", 0.05)+async def test_polling_file_watcher_transient_missing(tmp_path: Path) -> None:+ """Test that the watcher survives a transient file deletion (e.g. vim save).++ Editors like vim save by writing to a temp file, deleting the original,+ and renaming the temp. The polling watcher should tolerate the brief+ absence and fire the callback once the file reappears with new content.+ """+ tmp_file = tmp_path / "nb.py"+ tmp_file.write_bytes(b"original")++ callback_calls: list[Path] = []++ async def test_callback(path: Path) -> None:+ callback_calls.append(path)++ loop = asyncio.get_event_loop()+ watcher = PollingFileWatcher(tmp_file, test_callback, loop)+ watcher.start()++ await asyncio.sleep(0.1)++ # Simulate vim-style save: delete original, then recreate+ os.remove(tmp_file)+ await asyncio.sleep(0.1) # File is missing for a poll cycle+ with open(tmp_file, "w") as f: # noqa: ASYNC230+ f.write("updated content")++ # Wait for the watcher to detect the change+ await asyncio.sleep(0.2)++ watcher.stop()++ # The watcher should still be running and have detected the change+ assert watcher._missing_count == 0+ assert len(callback_calls) >= 1+ assert callback_calls[0] == tmp_file+++@patch("marimo._utils.file_watcher.PollingFileWatcher.MAX_MISSING_POLLS", 3)+@patch("marimo._utils.file_watcher.PollingFileWatcher.POLL_SECONDS", 0.05)+async def test_polling_file_watcher_permanently_missing(+ tmp_path: Path,+) -> None:+ """Test that the watcher stops after MAX_MISSING_POLLS consecutive misses."""+ tmp_file = tmp_path / "nb.py"+ tmp_file.write_bytes(b"")++ callback_calls: list[Path] = []++ async def test_callback(path: Path) -> None:+ callback_calls.append(path)++ loop = asyncio.get_event_loop()+ watcher = PollingFileWatcher(tmp_file, test_callback, loop)+ watcher.start()++ await asyncio.sleep(0.1)++ # Remove the file permanently+ os.remove(tmp_file)++ # Wait for the watcher to exceed MAX_MISSING_POLLS and stop+ await asyncio.sleep(0.5)++ assert watcher._running is False+ assert len(callback_calls) == 0++# This test is not working and watchdog makes CI hang in other areas# So we test this manually with `uv run --with=watchdog marimo edit nb.py --watch`@pytest.mark.xfail(reason="Test not working")