Repository navigation
fix(app-conversations): share concurrent workspace file listings - #1579
simonrosenberg wants to merge 2 commits into
Conversation
Each GET /api/v1/app-conversations/{id}/files ran its own `find` in the
sandbox. A client sending many listings at once (a history replay in
Canvas) stacked hundreds of `find` runs on one sandbox, which timed out
into 502s and pushed it out of memory.
Concurrent listings of the same conversation directory now await one
shared upstream call. A finished listing is not cached, so later
requests still see new files.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
📁 PR Artifacts Notice This PR contains a
|
ak684
left a comment
There was a problem hiding this comment.
Nice fix. Tested it against a real sandbox on a test deployment (agent-server 1.60.1, 1.25M-file workspace, this diff applied to 1.87.1):
- 50 concurrent listings: today that's 50
finds and every response comes back empty after ~33s. With this PR it's 1findand all 50 are correct in 2.8s. - A client disconnecting mid-listing didn't break the other waiters, so the 502 caveat in the Notes doesn't seem to happen in practice.
One thing I'd like changed before merge. A request that joins a find already in flight can get a listing older than the request itself:
- refresh at 0s starts a
find - agent writes a file at 1s
- Canvas's trailing refresh at 2s joins the same
findand doesn't see the file
On main step 3 sees the file. With this PR it missed it 3/3 times once a listing took longer than 2s (about 1M files, or a sandbox that's busy on CPU). If that was the agent's last action, the file stays missing until the user hits Refresh.
Suggested change: only join a listing whose find hasn't started yet; otherwise queue one fresh listing behind the running one. Still at most one find at a time, and in the same test the new file showed up 3/3 with the same wait as main (2.9s). Trade-off: a burst costs 2 finds instead of 1, and a refresh that lands mid-listing waits for the rest of that one plus a new one.
What I tested (your existing listing tests pass unchanged)
class _WorkspaceListing:
task: asyncio.Task[Any]
started = False
_workspace_listings: dict[tuple[UUID, str], _WorkspaceListing] = {}
def _shared_workspace_listing(
conversation_id: UUID,
cwd: str,
ctx: AgentServerContext,
httpx_client: httpx.AsyncClient,
) -> asyncio.Task[Any]:
"""Join a listing whose `find` has not started yet. If one is already
running, queue a single fresh listing behind it, so a result never
predates the request that receives it."""
key = (conversation_id, cwd)
latest = _workspace_listings.get(key)
if latest is not None and not latest.started:
return latest.task
running = latest.task if latest is not None else None
listing = _WorkspaceListing()
async def _run() -> Any:
if running is not None:
await asyncio.wait([running])
listing.started = True
return await _run_workspace_listing(cwd, ctx, httpx_client)
def _forget(_done: asyncio.Task[Any]) -> None:
if _workspace_listings.get(key) is listing:
del _workspace_listings[key]
listing.task = asyncio.create_task(_run())
listing.task.add_done_callback(_forget)
_workspace_listings[key] = listing
return listing.taskIt would need one more test: a request that arrives after the find has started gets a fresh listing, and several of those share it.
Happy to approve once that's in. If you'd rather ship as is and follow up, say so and I'll approve this one.
HUMAN:
Follow-up to the 2026-11-27 prod 5xx page (the Canvas
/filesrequest storm). I sent 50 concurrent/fileslistings through the real handler to a local fake agent-server.mainruns 50 concurrentfindcommands and this branch runs 1, with all 50 responses correct. The router tests and pre-commit (including mypy) pass.AGENT:
Why
GET /api/v1/app-conversations/{id}/filesruns afindin the sandbox throughexecute_bash_commandfor every request. On 11-27, Canvas 1.45.0 sent about 240 of these within 30s for one long conversation after a history replay. The sandbox fell behind (Command timed out after 30 seconds), the app returned 502 onhttpx.ReadTimeout(about 600 in an hour), and the sandbox was OOMKilled. Those 502s were 610 of the 1,025 5xx that tripped the P1 "SaaS Prod Core App HTTP 5xx Error Rate > 0.15%" alert at 14:35 UTC.The client-side cause is fixed in OpenHands/OpenHands#20132. This PR makes the server safe regardless of what a client sends.
Summary
(conversation_id, cwd)now await one shared upstream call, wrapped inasyncio.shieldso one client disconnecting does not cancel the listing for the others.findand sees new files._get_agent_server_contextbefore joining a shared listing.Issue Number
Related: OpenHands/OpenHands#20131 (client side).
How to Test
uv run pytest tests/unit/app_server/test_app_conversation_router.py: 102 passed.New
test_concurrent_requests_share_one_listing_per_conversation: three concurrent listings for one conversation plus one for another produce two upstream calls.mainit fails withassert 4 == 2.New
test_sequential_requests_each_run_a_listingguards against reusing a finished listing.pre-commit run --config ./dev_config/python/.pre-commit-config.yaml, including mypy: all pass.End-to-end, paired before and after:
.pr/files_listing_storm_probe.pycalls the reallist_conversation_fileswith a realhttpx.AsyncClient. Its target is a fake agent-server on localhost that takes 1s perexecute_bash_command. The probe fires 50 concurrent listings for one conversation. Only the router file differed between arms (log):findrunsmain@ 8a08a4bCI on 149e4ea: Python tests (4 shards), lint, CodeQL and both image builds are green.
Video/Screenshots
This is a backend-only change, so there is nothing visual. The validation proof is the paired probe output in How to Test.
Type
Notes
findper conversation directory per uvicorn worker instead of one per request.RequestError, which maps to 502 as it does today.🤖 Generated with Claude Code
Enterprise server image for this PR: