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(async_client_cleanup): stop cleanup from resurrecting healed clients

mateo-berri34f87a1a91d4
diff --git a/litellm/llms/custom_httpx/async_client_cleanup.py b/litellm/llms/custom_httpx/async_client_cleanup.py
index d5984e457eb..6ef61b6f58f 100644
--- a/litellm/llms/custom_httpx/async_client_cleanup.py
+++ b/litellm/llms/custom_httpx/async_client_cleanup.py
@@ -30,8 +30,8 @@ async def close_litellm_async_clients():
pass
# Handle AsyncHTTPHandler instances (used by Gemini and other providers)
- elif hasattr(handler, "client"):
- client = handler.client
+ elif hasattr(handler, "_client") or hasattr(handler, "client"):
+ client = handler._client if hasattr(handler, "_client") else handler.client
# Check if the httpx client has an aiohttp transport
if hasattr(client, "_transport") and hasattr(client._transport, "aclose"):
try:
diff --git a/tests/test_litellm/llms/custom_httpx/test_async_client_cleanup.py b/tests/test_litellm/llms/custom_httpx/test_async_client_cleanup.py
new file mode 100644
index 00000000000..e8fb0808019
--- /dev/null
+++ b/tests/test_litellm/llms/custom_httpx/test_async_client_cleanup.py
@@ -0,0 +1,21 @@
+import pytest
+
+import litellm
+from litellm.llms.custom_httpx.async_client_cleanup import close_litellm_async_clients
+from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler
+
+
+async def test_second_cleanup_pass_does_not_resurrect_owned_client():
+ handler = AsyncHTTPHandler()
+ original_client = handler._client
+ cache_key = "test-cleanup-no-resurrect"
+ litellm.in_memory_llm_clients_cache.cache_dict[cache_key] = handler
+ try:
+ await close_litellm_async_clients()
+ assert original_client.is_closed
+ await close_litellm_async_clients()
+ finally:
+ litellm.in_memory_llm_clients_cache.cache_dict.pop(cache_key, None)
+
+ assert handler._client is original_client