Skip to content

fix(app-conversations): share concurrent workspace file listings - #1579

Draft
simonrosenberg wants to merge 2 commits into
mainfrom
files-listing-single-flight
Draft

simonrosenberg wants to merge 2 commits into
mainfrom
files-listing-single-flight

Conversation

@simonrosenberg

@simonrosenberg simonrosenberg commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

HUMAN:

Follow-up to the 2026-11-27 prod 5xx page (the Canvas /files request storm). I sent 50 concurrent /files listings through the real handler to a local fake agent-server. main runs 50 concurrent find commands and this branch runs 1, with all 50 responses correct. The router tests and pre-commit (including mypy) pass.

  • A human has tested these changes.

AGENT:


Why

GET /api/v1/app-conversations/{id}/files runs a find in the sandbox through execute_bash_command for 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 on httpx.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

  • Concurrent listings of the same (conversation_id, cwd) now await one shared upstream call, wrapped in asyncio.shield so one client disconnecting does not cancel the listing for the others.
  • Nothing is cached after the call finishes, so a later request still runs a fresh find and sees new files.
  • Error mapping (502 on upstream status, decode, or request errors), the response shape, and the auth path are unchanged. Every request still resolves _get_agent_server_context before 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.

    • Negative control: with the router reset to main it fails with assert 4 == 2.
  • New test_sequential_requests_each_run_a_listing guards 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.py calls the real list_conversation_files with a real httpx.AsyncClient. Its target is a fake agent-server on localhost that takes 1s per execute_bash_command. The probe fires 50 concurrent listings for one conversation. Only the router file differed between arms (log):

    upstream find runs peak concurrent responses OK
    main @ 8a08a4b 50 50 50/50
    this PR 1 1 50/50
  • CI 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

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes

  • Sharing is per worker process, so a storm runs at most one find per conversation directory per uvicorn worker instead of one per request.
  • The shared call uses the request-scoped httpx client of the request that started it. That request awaits the call to completion, so the client stays open. If that request were cancelled mid-call, the other waiters would get a RequestError, which maps to 502 as it does today.

🤖 Generated with Claude Code


Enterprise server image for this PR:

ghcr.io/openhands/enterprise-server:sha-ef16fb8

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>
@github-actions github-actions Bot added the type: fix A bug fix label Nov 27, 2026
@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  openhands/app_server/app_conversation
  app_conversation_router.py
Project Total  

This report was generated by python-coverage-comment-action

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

📁 PR Artifacts Notice

This PR contains a .pr/ directory with PR-specific documents. This directory will be automatically removed when the PR is approved.

For fork PRs: Manual removal is required before merging.

@ak684 ak684 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 1 find and 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:

  1. refresh at 0s starts a find
  2. agent writes a file at 1s
  3. Canvas's trailing refresh at 2s joins the same find and 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.task

It 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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: fix A bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants