Skip to content

Reuse one httpx client in phabricator-client and close it in every caller - #7823

Open
WadekarPrashant wants to merge 3 commits into
mozilla:masterfrom
WadekarPrashant:phabricator-stateful-client
Open

WadekarPrashant wants to merge 3 commits into
mozilla:masterfrom
WadekarPrashant:phabricator-stateful-client

Conversation

@WadekarPrashant

Copy link
Copy Markdown

Closes #7821

Same change as #7822 for testrail-client, following bugzilla-client: PhabricatorClient.conduit_call opened a new httpx.AsyncClient on every Conduit call.

libs/phabricator-client

  • One httpx.AsyncClient created in __init__, with the timeout and the optional Mozilla-Edge-Key header set once.
  • Adds close(), __aenter__ and __aexit__, matching BugzillaClient.
  • URLs are still built per call ({url}/api/{method}), so requests are unchanged.
  • Tests: requests share one HTTP client, async with and close() close it, and the edge key header is sent only when configured. The httpx.AsyncClient stubs in phabricator-proxy and hackbot-runtime tests now accept the constructor arguments.

Where a client is built, and who closes it

Caller Lifetime
agents/{bug-fix,uplift-resolve,autowebcompat-intervention} brokers Closed in the Starlette lifespan, next to the Bugzilla client already closed there
uplift_resolve/agent.py async with around fetching the source diffs
hackbot_runtime/revision.py checkout_revision async with around resolving the stack
hackbot-api webhooks Created in the app lifespan and closed at shutdown, like HackbotClient; the dependency reads it from request.state
hackbot-api Phabricator action handlers One client per action, async with, passed to the helpers

Callers that only receive a client (agent_tools.phabricator, phabricator_proxy, uplift_resolve/models.py, phabricator_authorization.py, phabricator_webhook.py) are unchanged.

Notes on hackbot-api:

  • The webhook client could not be closed per request: get_phabricator_authorizer keeps the client it is first built with in app.state, so it has to live as long as the app.
  • The action handlers used an lru_cache singleton that was never closed. Each action now opens its own client, matching the TestRail handler in Reuse one httpx client in testrail-client and close it in the TestRail handler #7822. The repository PHID lookup, which alru_cache used to keep, is now kept per repository name, so it still runs once per process. A test covers it.
  • phabricator_handler.py's diff is mostly re-indentation; "Hide whitespace" shows the real change.

Testing

  • libs/phabricator-client: 49 passed
  • libs/phabricator-proxy: 10 passed
  • libs/hackbot-runtime: all test_revision.py tests pass
  • agents/bug-fix: 12 passed, 4 skipped
  • services/hackbot-api: 581 passed
  • ruff check / ruff format (v0.15.21) and the other pre-commit hooks pass on the changed files

PhabricatorClient.conduit_call opened a new httpx.AsyncClient for every Conduit call. The client now holds one AsyncClient, with the timeout and the optional Mozilla-Edge-Key header set once, and can be closed with close() or by using it as an async context manager, as BugzillaClient does.

Tests that stub httpx.AsyncClient now accept its constructor arguments.
The brokers close their Phabricator client in the app lifespan, next to the Bugzilla client they already close there. The uplift-resolve agent and checkout_revision use their client with async with, for just the calls that need it.
The webhook authorizer keeps the client it is first built with for the life of the app, so the webhook client is now created in the app lifespan and closed at shutdown, like the hackbot client, instead of once per request.

The Phabricator action handlers no longer share an lru_cache singleton that was never closed: each action opens its own client with async with and passes it to the helpers. The repository PHID lookup, which alru_cache used to keep, is now kept by repository name so it still runs once per process.
@WadekarPrashant
WadekarPrashant requested review from a team as code owners December 4, 2026 19:56

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor phabricator-client to use stateful httpx client and context manager

1 participant