From d4bda8e6a0816d39f9ca2bc297527457fa3e0a8d Mon Sep 17 00:00:00 2001 From: MollyAI Date: Sun, 22 Mar 2026 15:15:12 -0400 Subject: [PATCH] Fix watch-folder startup race --- src/recallforge/watch_folder.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/recallforge/watch_folder.py b/src/recallforge/watch_folder.py index 4594065..8cd7414 100644 --- a/src/recallforge/watch_folder.py +++ b/src/recallforge/watch_folder.py @@ -176,13 +176,17 @@ def _build_snapshot(self, config: WatchConfig) -> Dict[str, Tuple[int, int, int] continue return snap - def _scanner_loop(self, watch_id: str) -> None: + def _scanner_loop( + self, + watch_id: str, + initial_snapshot: Optional[Dict[str, Tuple[int, int, int]]] = None, + ) -> None: config = WatchConfig.from_dict(self.watches[watch_id]["config"]) root = Path(config.folder_path).expanduser().resolve() queue = self.queues[watch_id] evt = self.running[watch_id] - prev = self._build_snapshot(config) + prev = dict(initial_snapshot or self._build_snapshot(config)) while evt.is_set(): current = self._build_snapshot(config) @@ -349,8 +353,13 @@ def start_watch(self, config: WatchConfig) -> str: self.queues[watch_id] = Queue() self.running[watch_id] = threading.Event() self.running[watch_id].set() + initial_snapshot = self._build_snapshot(config) - s = threading.Thread(target=self._scanner_loop, args=(watch_id,), daemon=True) + s = threading.Thread( + target=self._scanner_loop, + args=(watch_id, initial_snapshot), + daemon=True, + ) w = threading.Thread(target=self._worker_loop, args=(watch_id,), daemon=True) s.start() w.start()