agent-orchestrator icon indicating copy to clipboard operation
agent-orchestrator copied to clipboard

bug(workspacewatch): one-fd-per-directory watch exhausts the fd limit on large workspaces, aborting workspace/events with a 500

Open Pulkit7070 opened this issue 3 months ago • 4 comments

Summary

On a workspace with many directories, GET /api/v1/sessions/{id}/workspace/events fails with watch workspace directory "...": open ...: too many open files and the live-file SSE subscription never starts. The watcher registers one OS watch per directory (one fd on macOS/kqueue, one inotify watch on Linux), so a large tree (node_modules, vendored deps, a monorepo) exhausts the process/system fd limit.

Root cause

addInitialDirectories (backend/internal/workspacewatch/watcher.go:152-193) collects every directory in the workspace — for a git worktree, the directory of each git ls-files entry plus all ancestors; otherwise a full filepath.WalkDir — and calls watcher.Add(dir) on each with no bound on the count. fsnotify opens an fd per watched directory (kqueue) / consumes an inotify watch, so once the number of directories exceeds RLIMIT_NOFILE (or the inotify max_user_watches), watcher.Add returns too many open files (EMFILE/ENFILE). The loop returns on the first such failure (watcher.go:186-192); the untyped error reaches streamWorkspaceChanges (backend/internal/httpd/controllers/sessions.go:622) -> envelope.WriteError -> 500, and the no-backoff EventSource reconnect (#4323) then retries in a loop.

Reproduction (verified)

Create a git worktree with 400 directories each containing a tracked file, lower RLIMIT_NOFILE to 128 to model a daemon already near its ceiling (a real machine hits the macOS default of 256 with a typical node_modules), then call workspacewatch.Watch(ctx, root):

watch workspace directory ".../pkg264": open .../pkg264: too many open files

The endpoint returns 500 and the subscription never establishes.

Why it matters

Unlike #4414 (a single missing directory, fixable by skip-and-continue), skip-and-continue does not help here: the watcher fundamentally registers an unbounded number of per-directory watches, so any sufficiently large workspace fails, and the descriptor pressure can degrade the whole daemon/machine, not just this endpoint.

Expected / options

  • Bound the number of watches, or watch coarsely (watch the root recursively where the platform supports it, or watch a capped/most-relevant set and reconcile) rather than one-per-directory unbounded.
  • At minimum, when watcher.Add fails with EMFILE/ENFILE, skip that directory and continue (partial coverage) instead of aborting the whole subscription with a 500, and return a typed degraded response rather than an untyped 500.

Affected code

  • backend/internal/workspacewatch/watcher.go:152-193
  • backend/internal/httpd/controllers/sessions.go:622
  • backend/internal/httpd/envelope/envelope.go (untyped error -> 500)

Related

  • #4414 (same watcher aborts on a single missing directory) — sibling root cause in the same loop.
  • #4323 (no-backoff SSE reconnect amplifies a transient setup 5xx).
  • #4325 (untyped service errors collapse to 500 INTERNAL_ERROR).

Pulkit7070 avatar Aug 26 '26 08:08 Pulkit7070

@Pulkit7070 @ApexYash11 Is this still valid?

illegalcall avatar Sep 08 '26 14:09 illegalcall

Assigned to @Ayash-Bera Please check first if it's still valid

Rishet11 avatar Sep 15 '26 19:09 Rishet11

@Rishet11 I believe the PR for this issue is already out there. Maybe you can ask Ayush to review it.

ApexYash11 avatar Sep 16 '26 11:09 ApexYash11

Still valid, re-verified today against current main (a47db0e06).

  • addInitialDirectories (backend/internal/workspacewatch/watcher.go) still registers one watch per directory with no bound and returns the first failed watcher.Add as an untyped error, so GET /api/v1/sessions/{id}/workspace/events 500s once the watch budget is exhausted.
  • Live reproduction against current main: a worktree with 530,000 directories (above this host per-user inotify max_user_watches ceiling of 524,288) makes workspacewatch.Watch abort with watch workspace directory "...": no space left on device. On macOS the same loop fails with too many open files (kqueue holds one fd per watched directory), the exact error from the issue body.

PR #4510 fixes it: watches are bounded (4096 cap, shallowest-first), failed adds are skipped and rolled into one aggregated WARN instead of aborting the subscription, and the same 530k-directory reproduction succeeds on the PR branch with partial coverage while root-level changes still notify. All CI checks on the PR head are green.

@illegalcall @Rishet11 @Ayash-Bera ready to merge once the head is re-approved, since the PR head moved after the last review when it synced main.

Ayash-Bera avatar Sep 17 '26 15:09 Ayash-Bera