Skip to content

perf(workspace-fs): watch only directories on Linux instead of every file #8324

Closed

Description

@maleadt

On Linux the host-service watches each worktree with chokidar 4, which opens an fs.watch (one inotify watch) on every file and every directory, and crawls the whole tree on the host event loop with two lstat calls per entry. An inotify watch on a directory already reports changes to the files in it (writes, chmod, create, delete, rename), with the child's name. The per-file watches only add a duplicate copy of those events. Watching directories only is what @parcel/watcher (and so VS Code) does on Linux, and what chokidar's unreleased v6 does ("files never own handles", docs/architecture.md on its v6 branch).

For a large repository the cost is high. An LLVM worktree (16.9k directories, 186k files), measured on the same 32-core machine under the same load, alternating runs:

chokidar 4.0.3 (current) directory-only prototype
time to attach 15.0–15.9 s 1.8–2.2 s
max event-loop stall during attach 840–900 ms 26–27 ms
inotify watches 203,384 16,927
RSS / JS heap 1.2 GB / ~880 MB 140 MB / ~58 MB

In production, with several LLVM workspaces, the host-service's vitals log shows FSEventWrap going between ~100 and 420k, event-loop busyShare at 1.0, and delays of up to 4.4 s. While that happens, terminal create/attach and every other request to that host stall. Each attach also uses ~200k of the user's fs.inotify.max_user_watches (default 524,288 on many distributions), so two or three such workspaces are enough to get ENOSPC.

#7878 and #7879 reduce how often a worktree is crawled. This issue is about how much each crawl costs, so the two fixes complement each other.

Equivalence. I ran chokidar (configured as in chokidar-backend.ts) and a directory-only prototype side by side over 21 file operations: create, append, truncate, atomic save, chmod, touch, mkdir -p followed by an immediate write, file and directory renames, rm -rf, file replaced by a directory, symlinks, a burst of 200 files, an ignored .git write, and a directory moved in from outside the tree. After coalesceWatchEvents, the two produce the same events, with one exception: hard links. A per-file watch follows the inode, so it reports a write made through a link outside the tree, or through a second link inside it. A directory watch does not (inotify(7): "events are not generated for the files inside the directory when the events are performed via a pathname (i.e., a link) that lies outside the monitored directory"). Working-tree files that git checks out are never hard links, and node_modules is already ignored.

Proposal. Replace the Linux chokidar backend with a small directory-only backend (one fs.watch per directory, lstat on each event to classify it as create, update or delete), and ideally run it in a worker, as git-directory-worker.ts already does since #7865. That worker uses fs.watch(path, { recursive: true }), which on Linux is Node's JS emulation and also watches every file (lib/internal/fs/recursive_watch.js), so it could use the same backend.

A directory-only backend has to get these right:

  • watch a new directory before scanning it, so files created in between are not missed;
  • emit deletes for the descendants when a directory is moved away;
  • re-attach after an atomic replace of a directory.

libuv drops IN_Q_OVERFLOW, so neither the current backend nor a fs.watch-based one can detect lost events. That is not a regression.

Activity

  1. github-actions commented on Oct 8, 2026

    @github-actions
    Contributor

    Triage: perf(workspace-fs): watch only directories on Linux

    Summary. On Linux the host-service watches each workspace worktree with chokidar 4. Chokidar opens one fs.watch (one inotify watch) for every file and every directory that is not ignored, and builds that set by crawling the whole tree on the host-service main event loop. In a very large repo (LLVM, about 203k entries), attaching takes about 15 s and blocks the event loop for up to about 900 ms at a time. It uses about 1.2 GB RSS and about 200k of the user's fs.inotify.max_user_watches. Requests to the host, such as terminal create/attach, stall while this runs. Two or three such workspaces are enough to hit ENOSPC. The issue proposes a directory-only backend: one inotify watch per directory, with lstat on each event to classify it. Ideally it would run in a worker. I read the code and the report fits it. This is a design and performance issue, not a logic bug.

    Affected code

    • packages/workspace-fs/src/watch-backend/index.ts:21-25: defaultWatchBackend() picks chokidarWatchBackend on Linux. The comment there says why @parcel/watcher is not used on Linux: its inotify thread crashes on EINTR. So the directory-only approach that parcel uses is currently not available on Linux.
    • packages/workspace-fs/src/watch-backend/chokidar-backend.ts:22-30: watch(rootPath, { ignoreInitial: true, followSymlinks: false, atomic: false, ignored }). Chokidar 4 adds an fs.watch for each file it finds while crawling. ignoreInitial only stops the initial add events from being sent; the crawl, the lstat calls and the per-file watches still happen. The returned promise resolves only on ready, at the end of the full crawl (:39-43), so the attach time is exactly the crawl time.
    • packages/workspace-fs/src/watch.ts:668: FsWatcherManager calls this.backend.subscribe(...) on the host-service event loop. The ENOSPC handling at watch.ts:523-533 logs only once and does not fall back to anything.
    • packages/host-service/src/workers/git-directory-worker.ts:15-21: uses fs.watch(path, { recursive: true }). On Linux, Node implements recursive watch in JS (lib/internal/fs/recursive_watch.js), which also opens a watch per file. This one already runs off the main thread (started from workers/host-worker.ts:53, driven by events/git-directory-watcher.ts:56), so its cost is inotify watches and memory, not event-loop stalls.

    Root cause

    • Per-file watches. Chokidar 4 opens an fs.watch for every non-ignored file in addition to every directory. On Linux an inotify watch on a directory already reports create, modify, attrib, delete and move for its children, so the extra watches only duplicate events. That explains the watch count: about 203k watches against about 17k directories.
    • Crawl on the main loop. The crawl and its lstat calls run on the host-service main loop, which explains the stalls.
    • Mitigations already in place. The ignore list (static globs, nested-repo pruning and gitignored-dir pruning at watch.ts:641-647, compiled by ignore-matcher.ts) reduces the tree. It does nothing for source-heavy trees like LLVM, where most files are tracked.
    • Out of scope. fix(workspace-fs): keep idle watchers warm across workspace switches #7878 and fix(desktop): read hover-card diff stats once instead of watching the worktree #7879 (crawl frequency) do not affect the cost of each crawl, as the issue says.

    Reproduction

    • Existing benchmark. packages/workspace-fs/src/watch-backend/chokidar-crawl.bench.ts already measures attachMs, peak RSS and event-loop max lag for chokidarWatchBackend. Its fixture is small (100 packages × 40 files per root). To reproduce the report:

      • raise packageCount and visibleFilesPerPackage (or point it at an LLVM checkout);
      • while it runs, count inotify watches with find /proc/$PID/fdinfo -type f | xargs grep -c '^inotify' | awk -F: '{s+=$2} END {print s}'.

      Expect watches ≈ files + directories with chokidar, against ≈ directories with a directory-only backend.

    • Quick manual check. Open a large repo as a workspace on Linux. Watch the host-service vitals log for FSEventWrap count and busyShare, and time a terminal attach while the watcher is attaching.

    Suggested fix direction

    1. New backend. Add something like watch-backend/directory-backend.ts that implements NativeWatchBackend (types.ts):

      • Do an async (ideally batched) readdir({ withFileTypes: true }) walk that prunes with createIgnoreMatcher. That gives directory-ness without a separate lstat per entry.
      • Open one fs.watch(dir) per directory.
      • On each event, lstat dir/filename to emit create, update or delete.
      • Select it in defaultWatchBackend() on Linux.
    2. Edge cases. The ones the issue lists are the right ones:

      • Attach the watch to a new directory before scanning it, and send create for whatever the scan finds.
      • When a directory is renamed away or removed, emit delete for its known descendants. This needs an in-memory directory→children index.
      • Re-attach when a watched directory's inode changes (atomic replace).

      With atomic: false the current backend lets the rename half reach watch-event-coalescing.ts; the new backend should keep that behaviour.

    3. Tests. watch-backend.test.ts and chokidar-ignore.test.ts should run against both backends, and the 21-operation equivalence matrix from the issue would be a good test fixture. Per AGENTS.md, keep it to one test per behaviour and use no real timeouts.

    4. Worker. Moving the walk into a worker (as git-directory-worker.ts does) removes the remaining stall. That worker's recursive: true watch could then use the same backend.

    5. Hard links. A directory watch does not see writes made through a hard link that lives outside the watched tree. Accepting that is reasonable for git checkouts, but it should be written down next to the backend.

  2. AlexGodard commented on Oct 24, 2026

    @AlexGodard
    Contributor

    A data point from two Linux hosts running the standalone host service, 1.40.0 (one native Arch, one WSL2 Ubuntu, 32 vCPUs), on a TypeScript monorepo.

    On both, the [host-service:vitals] line holds FSEventWrap at about 13.1k. One worktree of the repo has 12,235 files in 987 directories, 13,222 paths, so the count matches one watch per file and per directory, as described here. Directory-only watching would put it near 1k, about 13× fewer.

    On the WSL2 host the count also jumped from about 13.1k to 20.3k within one minute, at a time no workspace was being created there.

    We haven't tied our own event-loop stalls to the watcher (ours came from memory pressure), so this is the watch count only.

  3. added a commit that references this issue on Nov 13, 2026
    3e83652
  4. maleadt commented on Nov 14, 2026

    @maleadt
    Author

    I'm moving away from Paradev because of issues like this, so closing this.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions