Skip to content

Watch files through one watchdog observer per process - #5237

Merged
max-sixty merged 5 commits into
mainfrom
one-watch-per-process
Nov 3, 2026
Merged

max-sixty merged 5 commits into
mainfrom
one-watch-per-process

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

watchfiles fixes a watcher's directories when it starts, and its watch finds events by polling: notify's thread adds each change the kernel reports to a set, and watch sleeps one step between looks at it. So the watcher #5114 shares across a process restarts over every subscription whenever one needs a new directory, waking them all and telling path readers their batch may be lost, and each wake waits one to two steps: a median 82 ms on Linux and 88 ms on macOS at the 50 ms step #5114 set (30 writes each, in a container and on a Mac). A 5 ms step gets 10 ms and 17 ms by waking every Leaf process 200 times a second.

file_changes now schedules each directory, in each recursion mode, once on the process's watchdog observer, adding and removing directories while it runs, and hands each event to every subscription that accepts a path it names. Subscribing wakes no other subscription and no batch is lost. watchdog waits on the kernel, so a write reached its subscriber in a median 0.6 ms on Linux and 11 ms on macOS, with no measurable idle CPU. On Linux watchdog 7.0 shares one inotify instance across the process; 1,000 directories took one instance. Each scheduled directory costs the process an idle thread. On macOS it also opens an FSEvents stream, which reports the directory's whole tree for watchdog to filter in Python, so 20,000 writes in a page's revisions/ cost the process about 0.7 s of CPU.

Page subscriptions still take no tree. On Linux watchdog's recursive watch reports a directory moved into its tree from outside but never watches it, so in a container later writes inside it went unheard. It also skips a directory beneath its root that the kernel refuses a watch without raising, so nothing falls back to polling, and reporting that directory's contents can then stop the reader every watch in the process shares. Watching each directory of page/ and data/ and stamping them, as #5114 does, has neither gap: a directory made or moved in moves the reading, and its owner subscribes again. Only the developer preview's input trees keep a recursive watch, since they can be too large for a thread per directory.

A subscription ignores a file being opened or closed, and watchdog's report that a directory was modified, which accompanies the event of the entry that changed it. The page directory is an input, so hearing that report would let every diagnostic the server writes wake the page's followers.

watchdog 7.0 needs two repairs here. On macOS it reports a directory created in, removed from or moved across a non-recursive watch only as the watched directory being modified, which its own tests assert, so the macOS observer uses an emitter that keeps an event whose path or whose path's parent is the watched directory. On Linux, importing watchdog.observers opens the inotify instance, so with the instance limit spent even the polling observer can't be imported; the fallback imports the package again with watchdog's platform check saying this isn't Linux, and the native observer is then named rather than taken from Observer, which that import bound to polling. In a container with every instance spent, a subscription polled and woke within a second, and the next one was native once instances freed.

Calling inotify and FSEvents through ctypes was prototyped as the alternative: one reader thread, no repairs, a twelfth of watchdog's macOS CPU on that flood, and the recursive mode dropped. It came to half again this module's length in foreign-function code Leaf would own, and crashed at exit in 11 of 20 runs until it stopped its FSEvents streams first, the kind of hazard a maintained dependency absorbs.

The preview ignores __pycache__ itself rather than through watchfiles' DefaultFilter, and no longer handles a lost batch.

Tests: the test of a replacement's lost batch goes with replacements. The deep-directory test also moves a directory into page/ from outside the page, the one-watcher test checks one observer, each directory scheduled once and that subscribing wakes no one, and the page reading test checks that reading an input wakes nothing, which inotify would report. The failure-path tests now break the observer where they broke watchfiles.

This was written by Claude Code on behalf of max-sixty

watchfiles fixes a watcher's directories when it starts and checks its own
buffer on a 50 ms step. So the watcher #5114 shared across a process had to
restart over every subscription whenever one needed a new directory, waking
them all and telling path readers their batch may be lost, and every wake
waited 50-100 ms.

file_changes now schedules each directory, in each recursion mode, once on the
process's watchdog observer, adding and removing directories while it runs,
and hands each event to every subscription that accepts a path it names.
Subscribing wakes no other subscription and no batch is lost. A write reached
its subscriber in a median 0.6 ms on Linux and 11 ms on macOS, against 79 ms
and 91 ms with watchfiles (30 writes each, in a container and on a Mac). On
Linux watchdog 7.0 shares one inotify instance across the process; 1,000
directories took one instance. Each scheduled directory costs the process an
idle thread.

Page subscriptions still take no tree. On Linux watchdog's recursive watch
reports a directory moved into its tree from outside but never watches it, so
in a container later writes inside it went unheard. It also skips a directory
beneath its root that the kernel refuses a watch without raising, so nothing
falls back to polling, and reporting that directory's contents can then stop
the reader every watch in the process shares. Watching each directory of
`page/` and `data/` and stamping them, as #5114 does, has neither gap: a
directory made or moved in moves the reading, and its owner subscribes again.
Only the developer preview's input trees keep a recursive watch, since they can
be too large for a thread per directory.

A subscription ignores a file being opened or closed, and watchdog's report
that a directory was modified, which accompanies the event of the entry that
changed it. The page directory is an input, so hearing that report would let
every diagnostic the server writes wake the page's followers.

watchdog 7.0 needs two repairs here. Its FSEvents emitter drops a
non-recursive watch's events for directories created, removed or renamed in
it, so the macOS observer uses an emitter that keeps an event whose path or
whose path's parent is the watched directory. On Linux, importing
`watchdog.observers` opens the inotify instance, so with the instance limit
spent even the polling observer can't be imported; the fallback imports the
package again with watchdog's platform check saying this isn't Linux, and the
native observer is then named rather than taken from `Observer`, which that
import bound to polling. In a container with every instance spent, a
subscription polled and woke within a second, and the next one was native
once instances freed.

The preview ignores `__pycache__` itself rather than through watchfiles'
DefaultFilter, and no longer handles a lost batch.

Tests: the test of a replacement's lost batch goes with replacements. The
deep-directory test also moves a directory into `page/` from outside the page,
the one-watcher test checks one observer, each directory scheduled once and
that subscribing wakes no one, and the page reading test checks that reading
an input wakes nothing, which inotify would report. The failure-path tests now
break the observer where they broke watchfiles.
The FSEvents stream reports every event beneath a watched directory, and the
emitter's filter resolved the directory's real path for each of them. Over
20,000 writes deep inside a non-recursive watch, the watching process spent
691 ms of CPU against 158 ms for watchdog's own emitter; resolving the path
when the emitter starts brings it to 163 ms.
It reports a directory made in a non-recursive watch's directory, removed
from it or moved across it as that directory being modified, and keeps a
rename inside it. watchdog's test_recursive_off asserts this, so the previous
commit's account of it as dropping renames, and as a bug, was wrong.
watchfiles' Rust side receives events as the kernel reports them; its
watch sleeps a step between looks at them, so a shorter step trades wake
time for idle wakes in every process. A ctypes prototype over inotify and
FSEvents avoided watchdog's thread per directory and both repairs, but ran
half again this module's length and crashed at exit until it stopped its
FSEvents streams first.
@max-sixty
max-sixty merged commit cba85fe into main Nov 3, 2026
4 checks passed
@max-sixty
max-sixty deleted the one-watch-per-process branch November 3, 2026 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants