The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix: handle closed aiohttp sessions with detection and retry (#15442)
Alexsander Hamir5f5a7c917af9
diff --git a/litellm/llms/custom_httpx/aiohttp_transport.py b/litellm/llms/custom_httpx/aiohttp_transport.pyindex ab69ea1f8c3..da196e2e1e8 100644--- a/litellm/llms/custom_httpx/aiohttp_transport.py+++ b/litellm/llms/custom_httpx/aiohttp_transport.py@@ -152,6 +152,16 @@ class LiteLLMAiohttpTransport(AiohttpTransport):# If we don't have a client or it's not a ClientSession, create oneif not isinstance(self.client, ClientSession):+ if hasattr(self, "_client_factory") and callable(self._client_factory):+ self.client = self._client_factory()+ else:+ self.client = ClientSession()+ # Don't return yet - check if the newly created session is valid++ # Check if the session itself is closed+ if self.client.closed:+ verbose_logger.debug("Session is closed, creating new session")+ # Create a new sessionif hasattr(self, "_client_factory") and callable(self._client_factory):self.client = self._client_factory()else:@@ -193,13 +203,58 @@ class LiteLLMAiohttpTransport(AiohttpTransport):return self.client- async def handle_async_request(+ async def _make_aiohttp_request(self,+ client_session: ClientSession,request: httpx.Request,- ) -> httpx.Response:+ timeout: dict,+ proxy: Optional[str],+ sni_hostname: Optional[str],+ ) -> ClientResponse:+ """+ Helper function to make an aiohttp request with the given parameters.++ Args:+ client_session: The aiohttp ClientSession to use+ request: The httpx Request to send+ timeout: Timeout settings dict with 'connect', 'read', 'pool' keys+ proxy: Optional proxy URL+ sni_hostname: Optional SNI hostname for SSL++ Returns:+ ClientResponse from aiohttp+ """from aiohttp import ClientTimeoutfrom yarl import URL as YarlURL-++ try:+ data = request.content+ except httpx.RequestNotRead:+ data = request.stream # type: ignore+ request.headers.pop("transfer-encoding", None) # handled by aiohttp++ response = await client_session.request(+ method=request.method,+ url=YarlURL(str(request.url), encoded=True),+ headers=request.headers,+ data=data,+ allow_redirects=False,+ auto_decompress=False,+ timeout=ClientTimeout(+ sock_connect=timeout.get("connect"),+ sock_read=timeout.get("read"),+ connect=timeout.get("pool"),+ ),+ proxy=proxy,+ server_hostname=sni_hostname,+ ).__aenter__()++ return response++ async def handle_async_request(+ self,+ request: httpx.Request,+ ) -> httpx.Response:timeout = request.extensions.get("timeout", {})sni_hostname = request.extensions.get("sni_hostname")@@ -209,28 +264,38 @@ class LiteLLMAiohttpTransport(AiohttpTransport):# Resolve proxy settings from environment variablesproxy = await self._get_proxy_settings(request)- with map_aiohttp_exceptions():- try:- data = request.content- except httpx.RequestNotRead:- data = request.stream # type: ignore- request.headers.pop("transfer-encoding", None) # handled by aiohttp-- response = await client_session.request(- method=request.method,- url=YarlURL(str(request.url), encoded=True),- headers=request.headers,- data=data,- allow_redirects=False,- auto_decompress=False,- timeout=ClientTimeout(- sock_connect=timeout.get("connect"),- sock_read=timeout.get("read"),- connect=timeout.get("pool"),- ),- proxy=proxy,- server_hostname=sni_hostname,- ).__aenter__()+ try:+ with map_aiohttp_exceptions():+ response = await self._make_aiohttp_request(+ client_session=client_session,+ request=request,+ timeout=timeout,+ proxy=proxy,+ sni_hostname=sni_hostname,+ )+ except RuntimeError as e:+ # Handle the case where session was closed between our check and actual use+ if "Session is closed" in str(e):+ verbose_logger.debug(f"Session closed during request, retrying with new session: {e}")+ # Force creation of a new session+ if hasattr(self, "_client_factory") and callable(self._client_factory):+ self.client = self._client_factory()+ else:+ self.client = ClientSession()+ client_session = self.client++ # Retry the request with the new session+ with map_aiohttp_exceptions():+ response = await self._make_aiohttp_request(+ client_session=client_session,+ request=request,+ timeout=timeout,+ proxy=proxy,+ sni_hostname=sni_hostname,+ )+ else:+ # Re-raise if it's a different RuntimeError+ raisereturn httpx.Response(status_code=response.status,diff --git a/tests/test_litellm/llms/custom_httpx/test_aiohttp_transport.py b/tests/test_litellm/llms/custom_httpx/test_aiohttp_transport.pyindex f296f076178..4059b5b2e74 100644--- a/tests/test_litellm/llms/custom_httpx/test_aiohttp_transport.py+++ b/tests/test_litellm/llms/custom_httpx/test_aiohttp_transport.py@@ -181,6 +181,7 @@ async def test_timeout_exception_gets_mapped():@pytest.mark.asyncioasync def test_handle_async_request_uses_env_proxy(monkeypatch):"""Aiohttp transport should honor HTTP(S)_PROXY env vars"""+ import asyncioproxy_url = "http://proxy.local:3128"monkeypatch.setenv("HTTP_PROXY", proxy_url)monkeypatch.setenv("http_proxy", proxy_url)@@ -191,6 +192,13 @@ async def test_handle_async_request_uses_env_proxy(monkeypatch):captured = {}class FakeSession:+ def __init__(self):+ self.closed = False+ try:+ self._loop = asyncio.get_running_loop()+ except RuntimeError:+ self._loop = None+def request(self, *args, **kwargs):captured["proxy"] = kwargs.get("proxy")@@ -214,8 +222,97 @@ async def test_handle_async_request_uses_env_proxy(monkeypatch):return Resp()- transport = LiteLLMAiohttpTransport(client=lambda: FakeSession())+ transport = LiteLLMAiohttpTransport(client=lambda: FakeSession()) # type: ignorerequest = httpx.Request("GET", "http://example.com")await transport.handle_async_request(request)assert captured["proxy"] == proxy_url+++def _make_mock_response(should_fail=False, fail_count={"count": 0}):+ """Helper to create a mock aiohttp response"""+ class MockResp:+ status = 200+ headers = {}++ async def __aenter__(self):+ if should_fail and fail_count["count"] < 1:+ fail_count["count"] += 1+ raise RuntimeError("Session is closed")+ return self++ async def __aexit__(self, *args):+ pass++ @property+ def content(self):+ class C:+ async def iter_chunked(self, size):+ yield b"test"+ return C()++ return MockResp()+++def _make_mock_session(closed=False):+ """Helper to create a mock aiohttp session"""+ import asyncio++ class MockSession:+ def __init__(self):+ self.closed = closed+ try:+ self._loop = asyncio.get_running_loop()+ except RuntimeError:+ self._loop = None++ def request(self, *args, **kwargs):+ return _make_mock_response()++ return MockSession()+++async def test_handle_closed_session_before_request():+ """Test that closed sessions are detected and recreated"""+ counts = {"sessions": 0}++ def factory():+ counts["sessions"] += 1+ return _make_mock_session(closed=counts["sessions"] == 1)++ transport = LiteLLMAiohttpTransport(client=factory) # type: ignore+ response = await transport.handle_async_request(httpx.Request("GET", "http://example.com"))++ assert counts["sessions"] == 2 # Created 2 sessions: closed one, then open one+ assert response.status_code == 200+++async def test_handle_session_closed_during_request():+ """Test that sessions closed during request are handled with retry"""+ counts = {"sessions": 0, "requests": 0}+ fail_count = {"count": 0}++ class MockSession:+ def __init__(self):+ self.closed = False+ try:+ self._loop = __import__("asyncio").get_running_loop()+ except RuntimeError:+ self._loop = None++ def request(self, *args, **kwargs):+ counts["requests"] += 1+ return _make_mock_response(should_fail=True, fail_count=fail_count)++ def factory():+ counts["sessions"] += 1+ return MockSession()++ transport = LiteLLMAiohttpTransport(client=factory) # type: ignore+ response = await transport.handle_async_request(httpx.Request("GET", "http://example.com"))++ assert counts["requests"] == 2 # First request failed, second succeeded+ assert counts["sessions"] == 2 # Created 2 sessions for retry+ assert response.status_code == 200