Skip to content

Leaf holds most of a user's inotify instances, so other programs get EMFILE #4812

Closed

Description

@max-sixty

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 FileChanges holds one instance per recursion mode. FileChanges.__init__ in file_changes.py makes one watchfiles.watch() call for each recursion mode among its roots. Each call is one RustNotify, which holds one inotify instance however many paths it watches, and watchfiles takes a single recursive flag per call. page_roots makes a page's page/ and data/ 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_ends and RowPublisher.run share one page_changes subscription. Both loops also wake on timers. stop_when_service_ends wakes at least every ORPHAN_GRACE_SECS (1 s) for a session or job page and every 2 s for a persistent one. RowPublisher.run wakes every PRESENCE_CACHE_S (2 s), or sooner when the activity fold's next transition is due, and on each wake RowPublisher.refresh stats 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 job lifetime, lifetime_is_live checks only that the job's state.json exists, 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 or leaf server stop ends it, and a comment on its page can still resume the job. ClaudeCodeHarness.lifetime documents this as intended. The "Page lifetime" section of skills/leaf/references/serving-pages.md says 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.Watch subscribes all of the session's pages through one FileChanges, and the watch exits once harness.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_pages in hooks.py watches rows/ and the session's claims directory, and also re-reads every PRESENCE_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

  1. On Linux with fs.inotify.max_user_instances at 128, run several Claude Code background jobs. Each one serves a page that has a data/ directory, then finishes.

  2. 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)"; done
    

    Each 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 --watch and 1 for leaf 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 --pages 16. A server's reaction to a write goes from about 50 ms to 1 or 2 s.
  • One watch() call per FileChanges. page_roots would list each directory under page/ and data/ as a non-recursive root, and the owner would re-plan when one appears. _PageChanges.connect and Watch.mark already re-plan for a page's direct subdirectories. For nested ones, Watch.await_news would also need to return when a directory appears under page/ or data/, since the page reading stamps only the files there. This saves 1 per process that holds 2, about 29 here: 21 servers, 7 hook --watch and 1 leaf wait. With the first option taken, it saves 8. Per-path recursion in watchfiles would do the same, since notify's Watcher::watch takes 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.Watch does 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

  1. added
    bug
    on Oct 31, 2026
  2. leaf-agent commented on Oct 31, 2026

    @leaf-agent
    Collaborator

    #4813 removes the retained-page component of this exhaustion: page servers and hook --pages no longer allocate native watchers. A live Linux server with data/ 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-land own CI follow-up and landing under await_ci: false.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions