Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion litellm/llms/custom_httpx/http_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
AIOHTTP_CONNECTOR_LIMIT,
AIOHTTP_CONNECTOR_LIMIT_PER_HOST,
AIOHTTP_KEEPALIVE_TIMEOUT,
AIOHTTP_NEEDS_CLEANUP_CLOSED,
AIOHTTP_TTL_DNS_CACHE,
DEFAULT_SSL_CIPHERS,
)
Expand Down Expand Up @@ -876,9 +877,10 @@ def _create_aiohttp_transport(
transport_connector_kwargs = {
"keepalive_timeout": AIOHTTP_KEEPALIVE_TIMEOUT,
"ttl_dns_cache": AIOHTTP_TTL_DNS_CACHE,
"enable_cleanup_closed": True,
**connector_kwargs,
}
if AIOHTTP_NEEDS_CLEANUP_CLOSED:
transport_connector_kwargs["enable_cleanup_closed"] = True
if AIOHTTP_CONNECTOR_LIMIT > 0:
transport_connector_kwargs["limit"] = AIOHTTP_CONNECTOR_LIMIT
if AIOHTTP_CONNECTOR_LIMIT_PER_HOST > 0:
Expand Down
3 changes: 2 additions & 1 deletion litellm/proxy/proxy_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,8 +714,9 @@ async def _initialize_shared_aiohttp_session():
connector_kwargs = {
"keepalive_timeout": AIOHTTP_KEEPALIVE_TIMEOUT,
"ttl_dns_cache": AIOHTTP_TTL_DNS_CACHE,
"enable_cleanup_closed": True,
}
if AIOHTTP_NEEDS_CLEANUP_CLOSED:
connector_kwargs["enable_cleanup_closed"] = True
if AIOHTTP_CONNECTOR_LIMIT > 0:
connector_kwargs["limit"] = AIOHTTP_CONNECTOR_LIMIT
if AIOHTTP_CONNECTOR_LIMIT_PER_HOST > 0:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from unittest.mock import MagicMock, patch


def test_create_aiohttp_transport_sets_enable_cleanup_closed_when_needed(monkeypatch):
from litellm.llms.custom_httpx import http_handler as http_handler_module

connector_mock = MagicMock(name="connector")
session_mock = MagicMock(name="session")
monkeypatch.setattr(http_handler_module, "AIOHTTP_NEEDS_CLEANUP_CLOSED", True)

with patch.object(http_handler_module, "TCPConnector", return_value=connector_mock) as mock_tcp_connector:
with patch.object(http_handler_module, "ClientSession", return_value=session_mock):
transport = http_handler_module.AsyncHTTPHandler._create_aiohttp_transport(shared_session=None)
transport._get_valid_client_session()

assert mock_tcp_connector.call_args.kwargs["enable_cleanup_closed"] is True


def test_create_aiohttp_transport_omits_enable_cleanup_closed_when_not_needed(monkeypatch):
from litellm.llms.custom_httpx import http_handler as http_handler_module

connector_mock = MagicMock(name="connector")
session_mock = MagicMock(name="session")
monkeypatch.setattr(http_handler_module, "AIOHTTP_NEEDS_CLEANUP_CLOSED", False)

with patch.object(http_handler_module, "TCPConnector", return_value=connector_mock) as mock_tcp_connector:
with patch.object(http_handler_module, "ClientSession", return_value=session_mock):
transport = http_handler_module.AsyncHTTPHandler._create_aiohttp_transport(shared_session=None)
transport._get_valid_client_session()

assert "enable_cleanup_closed" not in mock_tcp_connector.call_args.kwargs
34 changes: 34 additions & 0 deletions tests/test_litellm/proxy/test_aiohttp_cleanup_closed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import asyncio
from unittest.mock import MagicMock, patch


def test_initialize_shared_aiohttp_session_sets_enable_cleanup_closed_when_needed(
monkeypatch,
):
from litellm.proxy import proxy_server as proxy_server_module

connector_mock = MagicMock(name="connector")
session_mock = MagicMock(name="session")
monkeypatch.setattr(proxy_server_module, "AIOHTTP_NEEDS_CLEANUP_CLOSED", True)

with patch("aiohttp.TCPConnector", return_value=connector_mock) as mock_tcp_connector:
with patch("aiohttp.ClientSession", return_value=session_mock):
asyncio.run(proxy_server_module._initialize_shared_aiohttp_session())

assert mock_tcp_connector.call_args.kwargs["enable_cleanup_closed"] is True


def test_initialize_shared_aiohttp_session_omits_enable_cleanup_closed_when_not_needed(
monkeypatch,
):
from litellm.proxy import proxy_server as proxy_server_module

connector_mock = MagicMock(name="connector")
session_mock = MagicMock(name="session")
monkeypatch.setattr(proxy_server_module, "AIOHTTP_NEEDS_CLEANUP_CLOSED", False)

with patch("aiohttp.TCPConnector", return_value=connector_mock) as mock_tcp_connector:
with patch("aiohttp.ClientSession", return_value=session_mock):
asyncio.run(proxy_server_module._initialize_shared_aiohttp_session())

assert "enable_cleanup_closed" not in mock_tcp_connector.call_args.kwargs
Loading