fix(cloud): address workspaces a few at a time so one batch cannot exhaust file descriptors
Root cause
cloudWorkspace.access fails in production because a single batched request exhausts the function instance's file descriptors.
- The desktop
SandboxAccessProviderkeeps 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.httpBatchLinksends 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 ownprocess.memoryUsage()call (uv_resident_set_memoryopens/proc/self/stat) then throws EMFILE, unhandled. If the open workspace'swakeis 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 failedevent in projectapiis 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 fdd62668c9, 2026-11-18T01:34Z) were one burst of 22 events in 1 s.
Inferred, not verified
- The exact descriptor limit on the instance, and how many sockets each call holds at its peak. I could not read Vercel runtime logs: the Vercel MCP connection lacks access to the team scope.
- Why it started on 11-17/18. No commit on this path changes per-call fetch counts then. The likely reason is that the number of live workspaces per org crossed the threshold: #9800 (11-15) addresses
asleepworkspaces too, and #9802 backfilled legacy rows to live statuses. I did not count production rows. - I looked for a descriptor leak and ruled it out as the main cause.
@vercel/sandbox4.0.0 shares one module-level undiciAgent, and the DB clients are module singletons.nudgeandhostServiceAnswersdon't read their response bodies, which holds a socket until GC, but they are off the hot path and outside this fix's scope.
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 testin packages/trpc: 1199 pass, 0 failbun run typecheckin packages/trpc: cleanbiome 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
8increateConcurrencyLimit(8)inpackages/trpc/src/router/cloud-workspace/cloud-workspace.ts. - Remove for good: this is a stopgap. Delete
concurrency-limit.tsand thelimitAddresswrapper inaccessonce 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.