← Back to Pull requests

fix(client): avoid blocking the event loop when constructing AsyncClient #6009

Closed
unknown closed this pull request without merging on Nov 04, 2026 16:11 UTC
View on GitHub
unknown unknown commented on Nov 04, 2026 02:59 UTC
Author

What happens

AsyncClient performs 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 existing RuntimeError helper. 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() returns None until 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:

python -m pytest -q -p no:cacheprovider -o addopts="" xinference/client/tests --ignore=xinference/client/tests/test_client.py --ignore=xinference/client/tests/test_async_client.py

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, including xinference/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

Diff: 426 lines (+319 / -107) across 5 files Reviewer: Jackmeson1 Review duration: 792m
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
0.45
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
56%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
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
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 56.8% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 45.1% (MEDIUM) · Higher = larger blast radius
45.1%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 56.8% · Lower = rushed or shallow review
56.8%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 19.5% · Safe to merge
19.5%