Skip to content

fix(cloud): address workspaces a few at a time so one batch cannot exhaust file descriptors - #10170

Merged
saddlepaddle merged 2 commits into
mainfrom
saddlepaddle/fix-cloud-access-emfile
Nov 23, 2026
Merged

saddlepaddle merged 2 commits into
mainfrom
saddlepaddle/fix-cloud-access-emfile

Conversation

@saddlepaddle

@saddlepaddle saddlepaddle commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Root cause

cloudWorkspace.access fails in production because a single batched request exhausts the function instance's file descriptors.

  • The desktop SandboxAccessProvider keeps a live address (access, wake: false) for every live cloud workspace in the organization. Cloud workspaces are org-wide, so a large org means many calls. httpBatchLink sends them as one HTTP request, and tRPC runs every call in that batch concurrently.
  • Each call makes several outbound fetches, one after another: Neon HTTP (one fetch per query: the row read, the membership check, and the awake/asleep writes), PostHog /flags, the Vercel Sandbox API (describeSandbox), and realtime/QStash on a transition. undici does not cap connections per origin, so N concurrent calls open about N sockets on each origin.
  • Past the descriptor limit every fetch on the instance fails (NeonDbError: Error connecting to database: TypeError: fetch failed). Sentry's own process.memoryUsage() call (uv_resident_set_memory opens /proc/self/stat) then throws EMFILE, unhandled. If the open workspace's wake is in the same batch, it fails too, and the desktop shows "Waking up workspace / Retry" on a box that is awake.

Verified (Sentry)

  • Over 24 h, every EMFILE / fetch failed event in project api is on this one transaction. That is 5,759 events in 208 traces, about 28 per request. No other transaction on the same instances fails, so the instance does not leak over time. One request exhausts it.
  • In each of the ten worst traces, 64 distinct cloud_workspace.ids fail. Errors arrive 20 to 64 at a time within the same second, which is one batch failing together.
  • The first failures (API-4E/4F, release fdd6266, 2026-11-18T01:34Z) were one burst of 22 events in 1 s.

Inferred, not verified

Fix

Address-only access calls run at most 8 at a time per instance (createConcurrencyLimit). A wake is not throttled, so the open workspace is never queued behind the sidebar. Released desktop and mobile builds benefit without an update. The API contract does not change.

The trade-off: a batch of 64 addresses now takes about 8 rounds instead of 1. Addresses are background work, so this costs some seconds once, not a visible stall. A wake that shares the batch still waits for the batch response (httpBatchLink is not streaming), but it now succeeds instead of failing.

Test

cloud-workspace.test.ts › "cloudWorkspace.access in one large batch": sends 64 concurrent address calls with the provider read held, then sends a wake.

  • Before the fix: it fails with Expected: <= 8, Received: 64.
  • After the fix: it passes, and the wake resolves while the 64 addresses are still held.

Checks

  • bun test in packages/trpc: 1199 pass, 0 fail
  • bun run typecheck in packages/trpc: clean
  • biome check packages/trpc/src/router/cloud-workspace/: clean

Not yet verified in production: whether EMFILE stops after deploy. Watch API-4J/4N on the release that carries this.

Fixes API-4J, API-4N, API-4E, API-4F, API-4Q, API-4R

How to undo

  • Revert: revert this PR's squash commit on main. It changes no schema, env var or API contract, so the revert is complete on its own and every released client keeps working either way.
  • Tune instead of undoing: the limit is the 8 in createConcurrencyLimit(8) in packages/trpc/src/router/cloud-workspace/cloud-workspace.ts.
  • Remove for good: this is a stopgap. Delete concurrency-limit.ts and the limitAddress wrapper in access once clients stop addressing every workspace in the list (address only the open one), or once the API has a global cap on outbound connections.

Summary by cubic

Fixes production EMFILE / fetch failed errors where one batched cloudWorkspace.access request exhausted the instance's file descriptors. The desktop addresses every live cloud workspace in one tRPC batch, and each address opens several sockets per origin, so a large org runs past the descriptor limit and every fetch in the batch fails — including wakes.

Address-only access calls now run at most 8 at a time per instance via createConcurrencyLimit. A wake is not throttled, so the open workspace is never queued behind the sidebar. The trade-off is that a batch of 64 addresses takes about 8 rounds instead of 1, but addresses are background work, so it costs a few seconds once rather than a visible stall.

Written for commit c3672b9. Summary will update on new commits.

View guided diff Turn on auto-fix

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@expo

expo Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

No installed development build can load this pull request as an update: its native fingerprint is 7730e8932546 and no development build has it. Add the mobile-build label to build one.

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

🧹 Preview Cleanup Complete

The following preview resources have been cleaned up:

  • ✅ Neon database branch

Thank you for your contribution! 🎉

…haust file descriptors

The desktop keeps a live address for every live cloud workspace in the
organization, and tRPC batches those `access` calls into one request (64+
in production). Each call makes several outbound fetches (Neon HTTP per
query, PostHog flags, the Vercel Sandbox API, realtime/QStash), and the
batch ran them all at once. undici does not cap connections per origin,
so a single request ran the function instance out of file descriptors:
every fetch then failed (NeonDbError: fetch failed) and Sentry's own
process.memoryUsage() threw EMFILE. A wake batched into the same request
failed with it, which is the "Waking up workspace" banner on awake boxes.

Address-only `access` calls now run at most 8 at a time per instance. A
wake is not held behind them.

Fixes API-4J
Fixes API-4N
Fixes API-4E
Fixes API-4F
Fixes API-4Q
Fixes API-4R

This branch was previously deployed

1 inactive deployment
preview — c3672b98 Deployed Nov 23, 2026 by saddlepaddle via Cleanup Preview Resources #5985
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant