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
12 changes: 9 additions & 3 deletions litellm/llms/custom_httpx/aiohttp_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,21 @@ async def aclose(self) -> None:


class AiohttpTransport(httpx.AsyncBaseTransport):
def __init__(self, client: Union[ClientSession, Callable[[], ClientSession]]) -> None:
def __init__(
self,
client: Union[ClientSession, Callable[[], ClientSession]],
owns_session: bool = True,
) -> None:
self.client = client
self._owns_session = owns_session

#########################################################
# Class variables for proxy settings
#########################################################
self.proxy_cache: Dict[str, Optional[str]] = {}

async def aclose(self) -> None:
if isinstance(self.client, ClientSession):
if self._owns_session and isinstance(self.client, ClientSession):
await self.client.close()


Expand All @@ -144,10 +149,11 @@ def __init__(
self,
client: Union[ClientSession, Callable[[], ClientSession]],
ssl_verify: Optional[Union[bool, ssl.SSLContext]] = None,
owns_session: bool = True,
):
self.client = client
self._ssl_verify = ssl_verify # Store for per-request SSL override
super().__init__(client=client)
super().__init__(client=client, owns_session=owns_session)
# Store the client factory for recreating sessions when needed
if callable(client):
self._client_factory = client
Expand Down
1 change: 1 addition & 0 deletions litellm/llms/custom_httpx/http_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,7 @@ def _create_aiohttp_transport(
return LiteLLMAiohttpTransport(
client=shared_session,
ssl_verify=ssl_for_transport,
owns_session=False,
)

# Create new session only if none provided or existing one is invalid
Expand Down
32 changes: 32 additions & 0 deletions tests/test_litellm/llms/custom_httpx/test_aiohttp_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,42 @@

from litellm.llms.custom_httpx.aiohttp_transport import (
AiohttpResponseStream,
AiohttpTransport,
LiteLLMAiohttpTransport,
)


@pytest.mark.asyncio
async def test_aclose_does_not_close_shared_session():
"""Test that aclose() does not close a session it does not own (shared session)."""
session = aiohttp.ClientSession()
try:
transport = LiteLLMAiohttpTransport(client=session, owns_session=False)
await transport.aclose()
assert not session.closed, "Shared session should not be closed by transport"
finally:
await session.close()


@pytest.mark.asyncio
async def test_aclose_closes_owned_session():
"""Test that aclose() closes a session it owns."""
session = aiohttp.ClientSession()
transport = LiteLLMAiohttpTransport(client=session, owns_session=True)
await transport.aclose()
assert session.closed, "Owned session should be closed by transport"


@pytest.mark.asyncio
async def test_owns_session_defaults_to_true():
"""Test that owns_session defaults to True for backwards compatibility."""
session = aiohttp.ClientSession()
transport = AiohttpTransport(client=session)
assert transport._owns_session is True
await transport.aclose()
assert session.closed


class MockAiohttpResponse:
"""Mock aiohttp ClientResponse for testing"""

Expand Down
Loading