Skip to content

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]

fix: prevent session leaks when recreating aiohttp sessions (#15443)

Alexsander Hamir0374fe02f7eb
diff --git a/litellm/llms/custom_httpx/aiohttp_transport.py b/litellm/llms/custom_httpx/aiohttp_transport.py
index da196e2e1e8..50bbccd6a4b 100644
--- a/litellm/llms/custom_httpx/aiohttp_transport.py
+++ b/litellm/llms/custom_httpx/aiohttp_transport.py
@@ -179,14 +179,17 @@ class LiteLLMAiohttpTransport(AiohttpTransport):
or session_loop != current_loop
or session_loop.is_closed()
):
- # Clean up the old session
+ # Close old session to prevent leaks
+ old_session = self.client
try:
- # Note: not awaiting close() here as it might be from a different loop
- # The session will be garbage collected
- pass
+ if not old_session.closed:
+ try:
+ asyncio.create_task(old_session.close())
+ except RuntimeError:
+ # Different event loop - can't schedule task, rely on GC
+ verbose_logger.debug("Old session from different loop, relying on GC")
except Exception as e:
verbose_logger.debug(f"Error closing old session: {e}")
- pass
# Create a new session in the current event loop
if hasattr(self, "_client_factory") and callable(self._client_factory):