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 __main__ exit bug, close litellm_async_client, restore global state
yuneng-jiang460f6208c5d9
diff --git a/tests/test_litellm/llms/custom_httpx/test_gemini_session_leak.py b/tests/test_litellm/llms/custom_httpx/test_gemini_session_leak.pyindex 6930b5488e9..9a4c6164db6 100755--- a/tests/test_litellm/llms/custom_httpx/test_gemini_session_leak.py+++ b/tests/test_litellm/llms/custom_httpx/test_gemini_session_leak.py@@ -81,5 +81,7 @@ def test_new_event_loop_atexit():if __name__ == "__main__":- success = asyncio.run(test_aiohttp_handler_cleanup())- sys.exit(0 if success else 1)+ asyncio.run(test_aiohttp_handler_cleanup())+ # If the assertion inside the test fails, asyncio.run raises;+ # reaching here means success.+ sys.exit(0)diff --git a/tests/test_litellm/llms/custom_httpx/test_http_handler.py b/tests/test_litellm/llms/custom_httpx/test_http_handler.pyindex 893c8a34ecf..0a3f0fe5e67 100644--- a/tests/test_litellm/llms/custom_httpx/test_http_handler.py+++ b/tests/test_litellm/llms/custom_httpx/test_http_handler.py@@ -59,13 +59,19 @@ async def test_ssl_security_level(monkeypatch):@pytest.mark.asyncioasync def test_force_ipv4_transport():"""Test transport creation with force_ipv4 enabled"""+ original_force_ipv4 = litellm.force_ipv4+ original_disable = litellm.disable_aiohttp_transportlitellm.force_ipv4 = Truelitellm.disable_aiohttp_transport = True- transport = AsyncHTTPHandler._create_async_transport()+ try:+ transport = AsyncHTTPHandler._create_async_transport()- # Should get an AsyncHTTPTransport (no real HTTP call — avoids CI hangs)- assert isinstance(transport, httpx.AsyncHTTPTransport)+ # Should get an AsyncHTTPTransport (no real HTTP call — avoids CI hangs)+ assert isinstance(transport, httpx.AsyncHTTPTransport)+ finally:+ litellm.force_ipv4 = original_force_ipv4+ litellm.disable_aiohttp_transport = original_disable@pytest.mark.asyncio@@ -93,13 +99,19 @@ async def test_ssl_context_transport():@pytest.mark.asyncioasync def test_aiohttp_disabled_transport():"""Test transport creation with aiohttp disabled"""+ original_disable = litellm.disable_aiohttp_transport+ original_force_ipv4 = litellm.force_ipv4litellm.disable_aiohttp_transport = Truelitellm.force_ipv4 = False- transport = AsyncHTTPHandler._create_async_transport()+ try:+ transport = AsyncHTTPHandler._create_async_transport()- # Should get None when both aiohttp is disabled and force_ipv4 is False- assert transport is None+ # Should get None when both aiohttp is disabled and force_ipv4 is False+ assert transport is None+ finally:+ litellm.disable_aiohttp_transport = original_disable+ litellm.force_ipv4 = original_force_ipv4@pytest.mark.asyncio@@ -117,25 +129,27 @@ async def test_ssl_verification_with_aiohttp_transport():litellm.disable_aiohttp_transport = Falsetry:- # Create a test SSL contextlitellm_async_client = AsyncHTTPHandler(ssl_verify=False)- transport = litellm_async_client.client._transport- assert isinstance(transport, LiteLLMAiohttpTransport)- transport_connector = transport._get_valid_client_session().connector- assert isinstance(transport_connector, TCPConnector)-- aiohttp_session = aiohttp.ClientSession(- connector=aiohttp.TCPConnector(ssl=False)- )try:- aiohttp_connector = aiohttp_session.connector- assert isinstance(aiohttp_connector, aiohttp.TCPConnector)+ transport = litellm_async_client.client._transport+ assert isinstance(transport, LiteLLMAiohttpTransport)+ transport_connector = transport._get_valid_client_session().connector+ assert isinstance(transport_connector, TCPConnector)- # assert both litellm transport and aiohttp session have ssl_verify=False- assert transport_connector._ssl == aiohttp_connector._ssl+ aiohttp_session = aiohttp.ClientSession(+ connector=aiohttp.TCPConnector(ssl=False)+ )+ try:+ aiohttp_connector = aiohttp_session.connector+ assert isinstance(aiohttp_connector, aiohttp.TCPConnector)++ # assert both litellm transport and aiohttp session have ssl_verify=False+ assert transport_connector._ssl == aiohttp_connector._ssl+ finally:+ await aiohttp_session.close()finally:- await aiohttp_session.close()+ await litellm_async_client.close()finally:# Restore original settinglitellm.disable_aiohttp_transport = original_disable