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: conditionally pass enable_cleanup_closed to aiohttp TCPConnector (#17367)
Jonathan Yang17faea96bb75
diff --git a/litellm/constants.py b/litellm/constants.pyindex 6a67a9a0e18..db617a2e475 100644--- a/litellm/constants.py+++ b/litellm/constants.py@@ -1,4 +1,5 @@import os+import sysfrom typing import List, LiteralDEFAULT_HEALTH_CHECK_PROMPT = str(@@ -103,6 +104,12 @@ _DEFAULT_TTL_FOR_HTTPX_CLIENTS = 3600 # 1 hour, re-use the same httpx client foAIOHTTP_CONNECTOR_LIMIT = int(os.getenv("AIOHTTP_CONNECTOR_LIMIT", 0))AIOHTTP_KEEPALIVE_TIMEOUT = int(os.getenv("AIOHTTP_KEEPALIVE_TIMEOUT", 120))AIOHTTP_TTL_DNS_CACHE = int(os.getenv("AIOHTTP_TTL_DNS_CACHE", 300))+# enable_cleanup_closed is only needed for Python versions with the SSL leak bug+# Fixed in Python 3.12.7+ and 3.13.1+ (see https://github.com/python/cpython/pull/118960)+# Reference: https://github.com/aio-libs/aiohttp/blob/master/aiohttp/connector.py#L74-L78+AIOHTTP_NEEDS_CLEANUP_CLOSED = (+ (3, 13, 0) <= sys.version_info < (3, 13, 1) or sys.version_info < (3, 12, 7)+)# WebSocket constants# Default to None (unlimited) to match OpenAI's official agents SDK behaviordiff --git a/litellm/llms/custom_httpx/http_handler.py b/litellm/llms/custom_httpx/http_handler.pyindex c35e910ab08..b06e8463abd 100644--- a/litellm/llms/custom_httpx/http_handler.py+++ b/litellm/llms/custom_httpx/http_handler.py@@ -17,6 +17,7 @@ from litellm.constants import (_DEFAULT_TTL_FOR_HTTPX_CLIENTS,AIOHTTP_CONNECTOR_LIMIT,AIOHTTP_KEEPALIVE_TIMEOUT,+ AIOHTTP_NEEDS_CLEANUP_CLOSED,AIOHTTP_TTL_DNS_CACHE,DEFAULT_SSL_CIPHERS,)@@ -798,7 +799,7 @@ class AsyncHTTPHandler:limit=AIOHTTP_CONNECTOR_LIMIT,keepalive_timeout=AIOHTTP_KEEPALIVE_TIMEOUT,ttl_dns_cache=AIOHTTP_TTL_DNS_CACHE,- enable_cleanup_closed=True,+ enable_cleanup_closed=AIOHTTP_NEEDS_CLEANUP_CLOSED,**connector_kwargs,),trust_env=trust_env,diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.pyindex 99789162318..a1e01caddc1 100644--- a/litellm/proxy/proxy_server.py+++ b/litellm/proxy/proxy_server.py@@ -32,6 +32,7 @@ from litellm._uuid import uuidfrom litellm.constants import (AIOHTTP_CONNECTOR_LIMIT,AIOHTTP_KEEPALIVE_TIMEOUT,+ AIOHTTP_NEEDS_CLEANUP_CLOSED,AIOHTTP_TTL_DNS_CACHE,AUDIO_SPEECH_CHUNK_SIZE,BASE_MCP_ROUTE,@@ -635,7 +636,7 @@ async def _initialize_shared_aiohttp_session():limit=AIOHTTP_CONNECTOR_LIMIT,keepalive_timeout=AIOHTTP_KEEPALIVE_TIMEOUT,ttl_dns_cache=AIOHTTP_TTL_DNS_CACHE,- enable_cleanup_closed=True,+ enable_cleanup_closed=AIOHTTP_NEEDS_CLEANUP_CLOSED,)session = ClientSession(connector=connector)