fix(watch): drop events from directories no longer watched (#5033)

A directory moved out of the root can still deliver an event after its
watch is removed. Drop events whose parent is not in the watched map,
and stale file paths whose parent no longer exists.
This commit is contained in:
Deepjyoti-Sarmah committed 2026-11-08 19:02:26 +05:30
1 parent 0c60a6ce10
commit cf36ca04d4
1 file changed
+4
+4
View File
@@ -3615,6 +3615,8 @@ def watch(watch_path: Path, debounce: float = 3.0) -> None:
if event.is_directory:
if not _is_read_only_event(event):
path = Path(os.fsdecode(event.src_path)).resolve()
if path.parent not in watched:
return
if event.event_type == "created":
add_tree(path, emit_existing=True)
elif event.event_type == "deleted":
@@ -3628,6 +3630,8 @@ def watch(watch_path: Path, debounce: float = 3.0) -> None:
if _is_read_only_event(event):
return
path = Path(os.fsdecode(event.src_path))
if path.parent not in watched or not path.parent.exists():
return
# Atomic replacement reports the new root config in dest_path.
dest_raw = getattr(event, "dest_path", None)
dest_path = Path(os.fsdecode(dest_raw)) if dest_raw else None