fix(client): avoid blocking the event loop when constructing AsyncClient #6009
All Vouch review confidence checks passed
Residual risk 0.20 = Change Risk (0.45) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.45 · 426 lines diff across 5 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.362
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #6009"
general
weight: 0.35
Jackmeson1
What happens
AsyncClientperforms a synchronous, timeout-free auth probe during construction, blocking other tasks on the caller's event loop.Deferring that probe behind a lock avoids the blocking constructor, but concurrent first calls retry a failed probe serially. Four callers against a stalled endpoint can take four transport timeouts to drain.
Cause
Construction uses a synchronous HTTP request. A lock around deferred initialization shares successful initialization but does not share a failed probe's outcome.
Change
Move the probe to an awaited initializer used by all 36 request methods, including
login. Concurrent first calls await one shared task and receive its result or error. Successful initialization is cached; a later call can retry after failure.Cancelling one caller does not cancel the probe. This also applies when the last current caller cancels: the probe continues until completion, its transport timeout or explicit client closure. Success is cached even if every caller has cancelled.
close()cancels and drains an active initialization task before completing session cleanup.The task returns failures to its waiters so an abandoned probe does not produce an asyncio exception report. API keys are applied after successful probing, and a token supplied before initialization takes precedence. A 404 still means an older server without auth.
Probe errors now surface from the first operation. Connection refusal raises
aiohttp.ClientConnectorError; non-200/non-404 responses use the existingRuntimeErrorhelper. The probe adopts the aiohttp session's 1800-second timeout and environment behavior (trust_env=False, without requests-specific CA-bundle handling). With an API key alone,_get_token()returnsNoneuntil initialization.The client and its session remain tied to their owning event loop.
Related #5278 addresses synchronous startup/metadata requests.
Tests
On Windows, with
PYTHONPATH=., Python 3.14.6 and 3.10.20, aiohttp 3.14.5, requests 2.34.2 and pytest 9.1.1:I ran
184aea5c: 115 passed on each Python, including 67 cleanup and initialization cases. Against 35bbd6cc, five cases fail because callers make separate failed probes, accumulate timeouts or restart initialization after cancellation.Coverage includes constructor responsiveness, shared success and failure, retry, cancellation of first and later waiters, late joiners, abandoned probe outcomes, API keys, saved tokens, login, 404 compatibility and initialization ordering. The shutdown regression suspends connection creation without network access and checks that closing the client cancels and drains the probe.
The shutdown regression fails with
06eaf76c. On Ubuntu 24.04.5 with Python 3.12.3 (Transformers 5.x and the CI pin 4.57.6) and 3.10.22, the 67 cleanup and initialization cases passed, and 57 client consumer tests passed. Changed-file pre-commit, includingxinference/types.py, passed.AI disclosure: this was prepared with AI assistance (Claude Opus 5.5 and Claude Sonnet 5.5).
🤖 Generated with Claude Code
https://claude.ai/code/session_01JfvcPZyKcKQUG9fQGYfRMw