Repository navigation
Serve every file subscription in a process from one native watcher - #5114
Merged
Merged
Conversation
A watchfiles watcher covers any number of directories, but each FileChanges started its own, one per recursion mode, so a process subscribing per page held two inotify instances per page. The per-user instance limit (128 by default) is the one that ran out in #4812, and to save instances the Claude Code pages follower polled for held fork threads every 0.25 s. file_changes now keeps one watcher per recursion mode per process and hands each batch to every subscription that accepts a path in it. watchfiles fixes a watcher's directories when it starts, so a subscription needing a directory the watcher lacks replaces it with one covering every open subscription. The others then wake once, since the retired watcher drops any batch it had not reported, and a collecting reader's batch() returns None, which the preview takes as a cue to read every input again. A directory wanted in one mode is served only by that mode's watcher. Page subscriptions are now non-recursive: page_roots lists every directory under page/ and data/, and the page reading stamps those directories, so a new one moves the reading and its owner subscribes again. On the 76 pages served on the author's Mac those trees hold a median of 1 directory and at most 3, so a process watching any number of pages holds one instance. The forks lane's follower waits on its pages' subscriptions and on the lane's name, bounded by the presence interval and the earliest hold, instead of polling. Over 40 messages per commit on macOS, a held thread reached it in a median 90 ms (p90 120 ms, slowest 127 ms) against 136 ms (p90 241 ms, slowest 249 ms). On Linux it costs one instance per Claude Code session beside the between-turn watch's one, while that watch drops from two to one wherever a page has page/ or data/. Page servers keep their timers, since each is a process per page. Measured in a Linux container: a process holding 10 page subscriptions and a session watch holds 1 inotify instance (main: 16 for the subscriptions alone). On macOS, starting a watcher grows with the square of its directories beyond a few dozen (0.1 s for 100, 3.6 s for 1,000), since notify restarts its FSEvents stream for each directory it adds. test_a_page_reading_moves_on_exactly_the_writes_its_watchers_wake_on now waits until the process's watcher reports a sentinel write before draining, since macOS delivered a setup write 0.5 s after subscribing and failed it, on main too.
# Conflicts: # tests/model/test_native_watch.py
leaf-agent
approved these changes
Nov 3, 2026
max-sixty
added a commit
that referenced
this pull request
Nov 3, 2026
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_
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A watchfiles watcher covers any number of directories, but each
FileChangesstarted its own, one per recursion mode, so a process subscribing per page held two inotify instances per page. The per-user instance limit (fs.inotify.max_user_instances, 128 by default) is the one that ran out in #4812, and to save instances the Claude Code pages follower polled for held fork threads every 0.25 s.file_changesnow keeps one watcher per recursion mode per process and hands each batch to every subscription that accepts a path in it. watchfiles fixes a watcher's directories when it starts, so a subscription needing a directory the watcher lacks replaces it with one covering every open subscription. The others then wake once, since the retired watcher drops any batch it had not reported, and a collecting reader'sbatch()returnsNone, which the preview takes as a cue to read every input again. A directory wanted in one mode is served only by that mode's watcher.Page subscriptions are now non-recursive:
page_rootslists every directory underpage/anddata/, and the page reading stamps those directories, so a new one moves the reading and its owner subscribes again. On the 76 pages served on the author's Mac those trees hold a median of 1 directory and at most 3, so a process watching any number of pages holds one instance.The forks lane's follower waits on its pages' subscriptions and on the lane's name, bounded by the presence interval and the earliest hold, instead of polling. Over 40 messages per commit on macOS, a held thread reached it in a median 90 ms (p90 120 ms, slowest 127 ms) against 136 ms (p90 241 ms, slowest 249 ms). On Linux it costs one instance per Claude Code session beside the between-turn watch's one, while that watch drops from two to one wherever a page has
page/ordata/. The follower and the watch stay separate processes, since only a background Stop hook's exit wakes the session after an Escape and only the hooks module's child feeds the band. Page servers keep their timers, since each is a process per page.Measured in a Linux container: a process holding 10 page subscriptions and a session watch holds 1 inotify instance (main: 16 for the subscriptions alone). On macOS, starting a watcher grows with the square of its directories beyond a few dozen (0.1 s for 100, 3.6 s for 1,000), since notify restarts its FSEvents stream for each directory it adds.
test_a_page_reading_moves_on_exactly_the_writes_its_watchers_wake_onnow waits until the process's watcher reports a sentinel write before draining, since macOS delivered a setup write 0.5 s after subscribing and failed it, on main too.test_a_page_subscription_hears_every_input_its_reading_reads, which #5081 moved totests/journeys/, now asserts that a page's subscription takes no tree.Verified with the focused watch, forks, hooks and preview tests, and the
claude-code(delivery and--forks),codex-queueandcodex-app-serverjourneys.dev/tests/model/test_interact_preview.py::test_preview_file_subscription_follows_atomic_replacementsfails on main as well.