fix(http_handler): defer finalizer client close while requests are in flight - #38242
Open
aminsaedi-behavox wants to merge 1 commit into
Conversation
Contributor
Greptile SummaryThis PR changes asynchronous HTTP-handler finalization so active clients are handed to the eviction closer instead of being closed beneath in-flight streams.
Confidence Score: 5/5The PR appears safe to merge. No blocking failure remains.
|
| Filename | Overview |
|---|---|
| litellm/caching/evicted_client_closer.py | Adds a close-or-defer entry point that preserves active connections until the existing reap lifecycle can close them. |
| litellm/llms/custom_httpx/http_handler.py | Replaces direct finalizer-triggered client closure with a scheduled handoff to the shared eviction closer. |
| tests/test_litellm/caching/test_evicted_client_closer.py | Adds behavioral coverage for immediate idle closure and deferred closure while a request is active. |
| tests/test_litellm/llms/custom_httpx/test_http_handler.py | Updates finalizer bookkeeping assertions and adds regression coverage for preserving an in-flight stream. |
Reviews (3): Last reviewed commit: "fix(http_handler): defer finalizer clien..." | Re-trigger Greptile
aminsaedi-behavox
force-pushed
the
litellm_evicted_handler_del_stream_kill
branch
from
August 25, 2026 20:55
05685c0 to
b04a7b1
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
aminsaedi-behavox
force-pushed
the
litellm_evicted_handler_del_stream_kill
branch
from
August 25, 2026 21:39
b04a7b1 to
5cb8b9b
Compare
Author
|
Both failures look unrelated: the together_ai params test fails from cross-test pollution (passes in isolation on staging), and auth-checks hit an Actions download outage. Rerun appreciated. |
Contributor
Merging this PR will not alter performance
Comparing |
… flight A cache-evicted AsyncHTTPHandler is finalized the moment the cache drops it. The finalizer's sole-referrer refcount guard proves nothing else holds the client object, but a request in flight references only the pooled connection, so the guard cannot see it: closing there tears the pool down under every live SSE stream, one batch per handler-cache TTL per process. The finalizer now defers to EvictedClientCloser.close_or_defer, which closes an idle client immediately and queues a busy one until it reports no connection in flight and the grace window has passed. The handoff runs via loop.call_soon so no lock is taken in GC context. Resolves the hourly batched mid-stream stream deaths observed in a production proxy deployment (streams died in same-second batches at a fixed phase after process start, matching the client-cache TTL).
aminsaedi-behavox
force-pushed
the
litellm_evicted_handler_del_stream_kill
branch
from
August 30, 2026 05:12
5cb8b9b to
3610495
Compare
1 task
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TLDR
Problem this solves:
__del__closes the owned clientHow it solves it:
__del__now defers to a newEvictedClientCloser.close_or_defervialoop.call_soon(no lock taken in GC context)close_or_defercloses an idle client immediately, preserving the reclamation the finalizer used to do, and queues a busy one so the existing reap closes it once it reports no connection in flight and the grace window has passedUser Flow
A proxy operator runs streaming traffic through any provider for more than the client-cache TTL. Before: some in-flight streams die mid-turn each hour with no terminal event. After: streams complete; idle evicted clients still get closed.
Relevant issues
Fixes #38957
Same failure family as the truncated-stream reports in anthropics/claude-code#67766 style symptoms observed by proxy users (client sees a 200 SSE that ends without message_stop).
Linear ticket
Pre-Submission checklist
uv run pytest tests/test_litellm/caching/test_evicted_client_closer.py tests/test_litellm/llms/custom_httpx/test_http_handler.py -v(75 passed; the pre-existing cookie test fails on a clean checkout in my sandbox and is unrelated)Screenshots / Proof of Fix
A finalizer race cannot be triggered from curl on demand, so the proof is the production signature plus a deterministic in-repo reproduction that fails on the merge base and passes on the tip.
Before (31a6756)
response.aiter_bytes()uv run pytest tests/test_litellm/llms/custom_httpx/test_http_handler.py::test_collected_handler_never_kills_a_stream_in_flight -q(test cherry-picked onto the merge base) fails: the stream raises ReadError mid-body afterdel handler; gc.collect()After (3610495)
test_close_or_defer_defers_a_client_with_a_request_on_the_wire)test_close_or_defer_closes_an_idle_client_immediatelypins that idle reclamation is unchanged, and the pre-existingtest_exclusively_owned_async_client_pool_is_closed_when_handler_is_collectedstill passesType
🐛 Bug Fix
Caveats (if any)
Changes
litellm/caching/evicted_client_closer.py: newEvictedClientCloser.close_or_deferlitellm/llms/custom_httpx/http_handler.py:AsyncHTTPHandler.__del__defers viacall_soonto the closer instead ofcreate_task(client.aclose())close_or_defer