Skip to content

fs: keep watching directories recreated under a recursive watch - #67747

Open
yohannesjoya wants to merge 1 commit into
nodejs:mainfrom
yohannesjoya:fs-watch-recreated-dir
Open

yohannesjoya wants to merge 1 commit into
nodejs:mainfrom
yohannesjoya:fs-watch-recreated-dir

Conversation

@yohannesjoya

Copy link
Copy Markdown
Contributor

On Linux, recursive fs.watch() keeps one inotify watcher per directory. When a directory below the root is removed and created again before its events are handled (for example rmSync(dir, { recursive: true }) followed by mkdirSync(dir) in a code generator), the parent's event finds a directory at the same path and the old watcher is kept, but it watches the removed directory and reports nothing more. Changes in the new directory are never reported, so node --watch-path stops restarting.

This handles the event the old watcher emits about the directory itself: if the path now holds a different directory, or nothing, the stale watcher is dropped and the current entry is added again (emitting rename for it and its contents). Inotify reports a chmod of the directory the same way, and the new directory often gets the same inode number (it did every time on ext4 here), so directories are compared by inode number and birth time. That costs one lstat() per watched directory below the root, on Linux only.

Fixes: #67592

The new test fails (hangs) on main and passes with this change. tools/test.py for parallel/test-fs-watch*, parallel/test-watch*, sequential/test-*watch* and parallel/test-vfs-watch* passes locally (3 repeats), as does make lint-js for the changed files. I also ran the reproduction from the issue with --watch-path: it now restarts on every regeneration.

AI disclosure: I used an AI assistant to help find the cause and write the test. I reviewed the change and ran the tests above myself.

On Linux, recursive fs.watch() keeps one inotify watcher per directory.
When a directory below the root was removed and created again before
its events were handled, the parent's event found a directory at the
same path, and the old watcher, which no longer reports anything, was
kept. Changes inside the new directory were then never reported, which
also stopped `node --watch-path` from restarting.

Handle the event the old watcher emits about the directory itself: if
the path now holds a different directory, or nothing, drop the old
watcher and start over with what is there. Inotify reports metadata
changes to the directory in the same way, and the new directory often
reuses the inode number, so directories are compared by inode number
and birth time.

Fixes: nodejs#67592
Signed-off-by: Yohannes Desta <yohannesdestagebru10@gmail.com>
@nodejs-github-bot nodejs-github-bot added fs Issues and PRs related to file-system APIs and the fs module. needs-ci PRs that need a full CI run. labels Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fs Issues and PRs related to file-system APIs and the fs module. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

node --watch-path: Does not restart when re-creating subdir on Linux

2 participants