Repository navigation
Description
What happens
On a Linux host with the kernel's default fs.inotify.max_user_instances of 128, Leaf's processes held 90 of one user's 128 inotify instances. The user had a few dozen pages from Claude Code sessions and background jobs, most of those jobs finished. Other programs run by the same user then couldn't start file watchers: inotify_init returned EMFILE.
The 90 instances, counted from anon_inode:inotify links under each process's /proc/<pid>/fd:
| Leaf process | Processes | Instances |
|---|---|---|
page server (server _serve) |
29 | 50 |
leaf hook --watch |
15 | 22 |
leaf hook --pages |
16 | 16 |
leaf wait |
1 | 2 |
The processes ran several Leaf versions. Each mechanism below is in the source at commit 9d88f0a.
Expected
Leaf's watching costs each user a small number of inotify instances that doesn't grow with every page left behind.
Where the instances come from
- A
FileChangesholds one instance per recursion mode.FileChanges.__init__infile_changes.pymakes onewatchfiles.watch()call for each recursion mode among its roots. Each call is oneRustNotify, which holds one inotify instance however many paths it watches, and watchfiles takes a singlerecursiveflag per call.page_rootsmakes a page'spage/anddata/recursive and its other directories non-recursive. So any process watching a page that has either directory holds 2. - A page server holds 1 or 2 for as long as it runs.
stop_when_service_endsandRowPublisher.runshare onepage_changessubscription. Both loops also wake on timers.stop_when_service_endswakes at least everyORPHAN_GRACE_SECS(1 s) for a session or job page and every 2 s for a persistent one.RowPublisher.runwakes everyPRESENCE_CACHE_S(2 s), or sooner when the activity fold's next transition is due, and on each wakeRowPublisher.refreshstats every page input. So the native subscription mainly shortens how fast a server reacts to a write. - A background job's page server runs until the job is deleted. For a
joblifetime,lifetime_is_livechecks only that the job'sstate.jsonexists, and when the worker retires, SessionEnd keeps the session's generation (state.end_harness_instance). So a done or blocked job keeps its server until the job is deleted orleaf server stopends it, and a comment on its page can still resume the job.ClaudeCodeHarness.lifetimedocuments this as intended. The "Page lifetime" section ofskills/leaf/references/serving-pages.mdsays a session page stops "when a terminal harness exits, or when a desktop Codex chat is archived or deleted", and says nothing of background jobs. - Each session's between-turn watch,
leaf hook --watch, holds 1 or 2.session.Watchsubscribes all of the session's pages through oneFileChanges, and the watch exits onceharness.process_runs()is false. - Each Claude Code session holding a page runs
leaf hook --pages, with 1 instance. Leaf's hooks module (hooks/claude-code.ts) starts it.cmd_pagesinhooks.pywatchesrows/and the session's claims directory, and also re-reads everyPRESENCE_CACHE_S(2 s).
The leaf wait in the table ran an older Leaf, a69bf53, and had been running for five days. cmd_wait still never asks harness.process_runs(), but in a Claude Code session the hook watches now refuse or supersede a leaf wait, so whether one can still outlive its worker on the current source is unconfirmed.
Repro
-
On Linux with
fs.inotify.max_user_instancesat 128, run several Claude Code background jobs. Each one serves a page that has adata/directory, then finishes. -
Count inotify instances per Leaf process:
for p in $(pgrep -f leaf); do echo "$p $(ls -l /proc/$p/fd 2>/dev/null | grep -c anon_inode:inotify)"; doneEach finished job leaves a server holding 2. Each running Claude Code session that holds a page adds 2 or 3: 1 or 2 for
leaf hook --watchand 1 forleaf hook --pages.
Options, with what each saves on the counts above
- Drop the native subscription where a timer already polls. Page servers would save 50 and
hook --pages16. A server's reaction to a write goes from about 50 ms to 1 or 2 s. - One
watch()call perFileChanges.page_rootswould list each directory underpage/anddata/as a non-recursive root, and the owner would re-plan when one appears._PageChanges.connectandWatch.markalready re-plan for a page's direct subdirectories. For nested ones,Watch.await_newswould also need to return when a directory appears underpage/ordata/, since the page reading stamps only the files there. This saves 1 per process that holds 2, about 29 here: 21 servers, 7hook --watchand 1leaf wait. With the first option taken, it saves 8. Per-path recursion in watchfiles would do the same, since notify'sWatcher::watchtakes a recursive mode per path. - Retire idle background-job servers, or serve all of a user's pages from one process. Retiring one needs a way to start it again when someone opens or comments on its page. One process holding a single subscription across all the pages it serves, as
session.Watchdoes for a session's pages, would cut the servers' 50 instances to 1 or 2. But today each server keeps the Leaf version that started it.
Seen on leaf 9d88f0a, installed 2026-10-30T19:52:24-07:00.
This was written by Claude Code on behalf of max-sixty
Activity
leaf-agent commentedon Oct 31, 2026
#4813 removes the retained-page component of this exhaustion: page servers and
hook --pagesno longer allocate native watchers. A live Linux server withdata/held zero inotify instances. Applied to the reported process mix, this removes 66 of the 90 instances; active delivery watches and explicit event followers retain theirs.Neighbor rows and the prompt band now follow file changes on their existing two-second intervals, potentially crossing both intervals before a row change reaches the prompt. Background-job pages remain served and resumable.
Both regression tests failed before the fix and pass afterward; 39 focused checks and the live Pi journey passed. Claude Code and Codex executable availability prevented their live journeys here. The fix is pushed; the queued review and
tend-landown CI follow-up and landing underawait_ci: false.Serve every file subscription in a process from one native watcher (#…