Repository navigation
fix(aiohttp): keep keep-alive connector config when a session is rebuilt - #34962
yassin-berriai merged 1 commit into
Conversation
|
|
Greptile SummaryThe PR preserves configured aiohttp connector behavior when sessions are rebuilt.
Confidence Score: 5/5The PR appears safe to merge. The previously reported cleanup defect is fixed: replacement sessions become transport-owned and are closed by transport cleanup, while the original shared session remains externally owned and is not closed during rebuilding.
|
| Filename | Overview |
|---|---|
| litellm/llms/custom_httpx/aiohttp_transport.py | Centralizes session replacement through the configured factory and correctly updates ownership while preserving externally owned sessions. |
| litellm/llms/custom_httpx/http_handler.py | Builds one configured session factory and supplies it to both shared-session and newly created transports. |
| tests/test_litellm/llms/custom_httpx/test_aiohttp_so_keepalive.py | Verifies that rebuilding a shared session retains the keepalive socket factory and timeout. |
| tests/test_litellm/llms/custom_httpx/test_aiohttp_transport.py | Covers factory-based rebuilding, replacement ownership, cleanup, and preservation of unowned shared sessions. |
Reviews (2): Last reviewed commit: "fix(aiohttp): keep keep-alive connector ..." | Re-trigger Greptile
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
LiteLLMAiohttpTransport only stored a session factory when it was constructed with a callable. A transport built around an already-created ClientSession (the proxy's shared session) had no factory, so every session-rebuild site fell back to a bare aiohttp.ClientSession() with aiohttp's default connector. That silently dropped the keep-alive socket factory behind AIOHTTP_SO_KEEPALIVE, plus keepalive_timeout (120s -> 15s), ttl_dns_cache, the connector limits and trust_env. The rebuilt session is cached on the transport, so one rebuild disabled stale-socket protection for every later request on it. Build the configured session factory once in _create_aiohttp_transport and inject it into both the shared-session and new-session transports, and route every rebuild through it. A rebuilt session is reachable only from the transport, so the transport takes ownership of it: aclose() now closes the replacement instead of leaking it, and a rebuild no longer closes a session the transport does not own. Resolves LIT-4674
6df8bd9 to
d2b7341
Compare
|
Good catch on the ownership hole, that was real and is fixed in Rebuilding now goes through a single Two regression tests cover both directions, and both fail on the previous commit: Please re-review the current head d2b7341 |
|
@greptileai please review the current head d2b7341 - the previous review is pinned to 6df8bd9bb5, which was replaced by the ownership fix described above |
TLDR
Problem this solves:
AIOHTTP_SO_KEEPALIVEstopped applying after an aiohttp session rebuildHow it solves it:
Relevant issues
Fixes #33567
Linear ticket
Resolves LIT-4674
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
@greptileaito re-request a review after pushing changes)Delays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
The observable is a kernel socket option on the connection litellm actually holds open to the provider, so the proof reads it off the pooled socket after a real, paid OpenAI call rather than off a mock. The script drives the production functions directly (
_initialize_shared_aiohttp_session->_create_aiohttp_transport->httpx.AsyncClient), because a session rebuild has no HTTP surface a curl could showBefore, at
d91fd084f7(currentlitellm_internal_staging), withAIOHTTP_SO_KEEPALIVE=trueandAIOHTTP_TCP_KEEPIDLE=45exported:The socket carrying real traffic has keepalive off and the kernel's 2-hour idle default, so the env var bought nothing
After, at
d2b7341137(this PR), same command and same env:Keepalive is on and the idle-before-first-probe is the configured 45s, which is what makes the kernel reset a middlebox idle timer before the flow is reaped
The same build serving normal traffic end to end, live proxy at
d2b7341137on port 4674 withAIOHTTP_SO_KEEPALIVE=true:Type
🐛 Bug Fix
Changes
LiteLLMAiohttpTransportonly remembered a session factory when it was constructed with a callable._create_aiohttp_transportpasses a concreteClientSessionon the shared-session path, so that transport had no factory, and each of the five session-rebuild sites inaiohttp_transport.pyfell back to a bareaiohttp.ClientSession()carrying aiohttp's default connector. That dropped the keepalive socket factory behindAIOHTTP_SO_KEEPALIVE/AIOHTTP_TCP_KEEPIDLE, along withkeepalive_timeout(120s -> aiohttp's 15s),ttl_dns_cache,limit/limit_per_host,enable_cleanup_closedandtrust_env. A rebuild fires whenever the session is closed or belongs to a different or closed event loop, and the replacement is cached on the transport, so a single rebuild disabled stale-socket protection for every later request that transport servedThe fix builds the session factory once in
_create_aiohttp_transportand injects it into both the shared-session and new-session transports through a newsession_factoryargument, then routes every rebuild throughself._client_factory. The factory now always exists, which also removes the repeatedhasattr(self, "_client_factory") and callable(...)branching and a redundant in-functionClientSessionimport. Behavior is unchanged for a transport built with no factory at all: it still falls back toClientSessionThe rebuild also fixes an ownership hole that Greptile caught. A replacement session is reachable only from the transport, yet
owns_sessionstayedFalseon the shared-session path, soaclose()left the replacement and its connector open. Rebuilding now transfers ownership to the transport, and the reverse case is handled too: a rebuild no longer closes a session the transport does not own, so one transport hitting a stale event loop cannot close the proxy-wide shared session out from under every other transport holding itWorth noting for the linked issue: the reporter's reading that the factory is never wired into the Azure/OpenAI path is not quite right, the first session on every path does get it (
http_handler.py); what was broken is that every rebuild threw it away. The reporter's second ask, an env-tunable keepalive timeout, already exists asAIOHTTP_KEEPALIVE_TIMEOUTand is left at its 120s default here since lowering it changes pooling for everyonelitellm/proxy/proxy_server.py::_initialize_shared_aiohttp_sessionstill rebuilds the same connector kwargs independently, and it has already drifted: it never passestrust_env, so the proxy's shared session ignoreslitellm.aiohttp_trust_env/AIOHTTP_TRUST_ENV, and it never calls_get_ssl_connector_kwargs, solitellm.force_ipv4'slocal_addrnever reaches that connector either. Both are silent. Collapsing the two builders into one helper is the right fix for that, but it is a separate change and is not needed here, since the transport now rebuilds from its own configured factoryFinal Attestation