Repository navigation
Description
Python -VV
Python 3.13.13 (main, May 3 2026, 21:46:01) [Clang 21.0.0 (clang-2100.0.123.102)]Pip Freeze
mistral-common==1.11.0
mistralai==2.3.2Reproduction Steps
Fire ~20+ concurrent chat.stream_async calls on a single shared Mistral
client against an endpoint that returns errors (e.g. 429 rate limits, or any
4xx/5xx). After enough leaked connections accumulate, subsequent calls fail with
httpx.PoolTimeout. Creating a fresh client per call works around it (the leaked
connections are reclaimed when the whole client is garbage-collected).
Expected Behavior
The httpx pool is not exhausted.
Additional Context
Summary
When a streaming request (chat.stream_async, stream_async, etc.) receives an
error HTTP status, the SDK raises an exception (or retries) without closing
the underlying streaming httpx.Response. The pooled connection stays checked
out forever. Under concurrency with a shared client, hitting enough 429s exhausts
the httpx connection pool and subsequent calls fail with httpx.PoolTimeout.
Affected versions
Confirmed identical from 2.3.2 through 2.4.5 — the Speakeasy-generated HTTP
layer is unchanged across these releases.
Root cause
For a streaming request, client.send(req, stream=True) returns a Response
that holds a connection from the pool until the body is fully consumed or
aclose() is called. Two code paths leak it on an error status:
-
src/mistralai/client/basesdk.py—do_request_async: when the response
status matcheserror_status_codes, it invokes the error hook and raises
errors.SDKError("Unexpected error occurred", http_res). Withstream=True,
http_resis a live streaming response that is neveraclose()d. -
src/mistralai/client/utils/retries.py—retry_async/
retry_with_backoff_async: for a retryable status (429/500/502/503/504),
the innerdo_requestraisesTemporaryError(res);retry_with_backoff_async
then sleeps and retries, droppingres(the open streaming response) without
closing it. Each retried attempt leaks one connection.
The synchronous variants (retry / retry_with_backoff, do_request) have the
same problem.
Non-streaming requests are unaffected: with stream=False, httpx reads the
full body and releases the connection back to the pool.
Suggested Solutions
Close the streaming response before raising or retrying on an error status:
- In
do_request_async/do_request: whenstream=True,await http_res.aclose()
(resp.http_res.close()) before raisingSDKError. - In
retry_async/retry: closeresbefore raisingTemporaryError, or in
the retry loop (retry_with_backoff_async/retry_with_backoff) before the
next attempt.
Activity
ronnix commentedon May 20, 2026
Related : #509
ferhimedamine commentedon Jun 16, 2026
Unclosed streaming requests on error are a common resource leak in HTTP client libraries — the connection stays open until the server closes it (or the process exits), which exhausts connection pools under load.
The standard fix: in the streaming path, wrap the response body iteration in a try/finally that calls response.close() (or uses a context manager) even on error exit. This ensures the connection is returned to the pool regardless of how the streaming ended.
For agent systems using Mistral for inference where the agent's memory layer makes concurrent requests alongside the streaming LLM call, connection pool exhaustion from this leak can cause memory API requests to queue up behind stuck streaming connections. Worth fixing before scaling to concurrent agents. For the memory side, dakera-deploy uses connection pooling internally so its REST API is resilient to this kind of upstream leak.
tonydzi commentedon Jul 28, 2026
Still present on
2.8.0(checked againstmainat 7749c84, today's SDK regen). Both paths @ronnix identified are unchanged:src/mistralai/client/basesdk.py:305(sync) and:387(async) —raise errors.SDKError("Unexpected error occurred", http_res)with noclose()/aclose(), andhttp_rescame fromclient.send(req, stream=stream).src/mistralai/client/utils/retries.py:142,147(sync) and:186,191(async) —raise TemporaryError(res)drops the open response; each retried attempt leaks one connection.One detail worth adding, because I think it explains why this has been hard to move rather than merely unattended: both files are Speakeasy-generated and carry
DO NOT EDIT. A PR against either gets overwritten by the nextchore: 🐝 Update SDK— one landed today — so the fix can't just live where the bug is.For the first path there's a way around that inside the hand-maintained zone.
basesdk.pycallshooks.after_error(...)before it raises, and the hook receives the livehttpx.Response._hooks/registration.pyis explicitly "generated once and then free to be modified", and the same mechanism already carriesWorkflowStreamErrorHookfrom #596. So anafter_errorhook that closes a streaming response before the SDKError propagates would survive regeneration and needs no generator change.The retry path can't be reached that way:
TemporaryErroris raised insideutils.retry's owndo_request, so intermediate responses never reachafter_error— only the final one does. That half looks like it genuinely needs the generator template (orretry_connection_errorssemantics) changed upstream, which is presumably your call and not something an outside PR can carry.Happy to put up the hook for the first path — it's small and testable, roughly in the shape of
_hooks/stream_error_hook.py. I'd rather not send it uninvited into a repo where most of the tree is generated, so: useful, or would you rather fix both halves upstream in one go? Either answer is fine — just say which and I'll act on it or drop it.Nelson-PROIA commentedon Sep 10, 2026
Fixed in
mistralai2.10.0.The retry path now closes the response before backing off, so a retried 429/500/502/503/504 on a streaming call no longer strands a pooled connection:
The sync variant calls
close()in the same place. This came in with a code generator upgrade, so it applies across the whole generated surface rather than one method.Checked against a local server with
max_connections=2. On 2.8.0, a streaming call retried on 429 holds one connection per attempt and the pool is exhausted by the fifth call withPoolTimeout. On 2.10.0 the same test leaves every connection available.On the second path in the original report:
basesdk.pystill has noclose(), but itsraise errors.SDKError("Unexpected error occurred", http_res)is not reachable in this SDK. Anafter_errorhook is registered that returns the response, sodo_requesttakes theif result is not Nonebranch and falls through to the generated operation, which callsstream_to_text_async(http_res)before raising. Reading the body releases the connection. Verified with a non-retryable 400, which also leaves the pool clean.