litellm/tests/unit/test_streaming_connection_cleanup.py
yuneng-jiang f6882246d4
test: move tests/test_litellm root and small trees into tests/unit (#43186)
* ci: run the unit_selection.sh shard files on every event instead of only fork pull requests

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* ci: rename fork-flag to unit-flag now that it applies on every event

* test: move tests/test_litellm root and small trees into tests/unit

Pure renames, no content changes. Follow-up commits in this PR fix
references, merge the three files that already existed in tests/unit,
keep live-provider tests in tests/test_litellm and wire CI.

* test: carry tests/test_litellm conftest isolation into tests/unit

Callback lists, routing fallbacks, cached HTTP clients, logger state, AWS,
proxy-URL and keychain env, and session-end client cleanup now reset for
unit tests too. The environment isolation owns its MonkeyPatch so a test's
own monkeypatch is undone before the model-cost teardown runs.

* test: merge, split and prune the moved root and small-tree tests

Merge batches/test_batch_utils.py and the chat_completions and messages
dispatch tests into the files that already existed in tests/unit. Keep
the live Gemini interactions tests, the async image-fetch format test and
the OpenAI embedding scorer test in tests/test_litellm since they need
real network or keys. Put test_router.py under tests/unit/test_router so
the existing package no longer shadows it. Delete eight tests the audit
found superseded by stronger ones kept in this move.

* ci: run the moved root and small-tree tests under their legacy flags

Add the misc and responses-caching-types flags to unit_selection.sh and
CircleCI, extend enterprise-routing and mcp-integration, and point the
legacy GHA shards, Makefile, redis-compat workflow, merge smoke manifest
and change classifier at the new paths.

* test: make the new tests/unit directories packages

tests/unit/test_package_layout.py requires every directory to carry an
__init__.py, and without one the moved and retained
test_litellm_responses_bridge.py modules collide on import.

* test: scope the unit socket block to tests/unit in shared sessions

The GHA shards collect the legacy test-path and the unit selection in one
pytest session. The unit conftest's loopback-only block leaked into legacy
modules that reach the network at import. The legacy conftest now lifts the
restriction at collect and setup time, and the unit conftest re-applies it
when collecting its own modules.

* test: give the shard-script tests their own GITHUB_OUTPUT

They only passed where the runner set it. The CircleCI unit job's env
allowlist drops it, so the script's redirect failed there.

* test: point the router and module-deletion checks at tests/unit

router_code_coverage and code_qa_check_tests only searched tests/test_litellm,
so the moved router tests no longer counted. The two silent-experiment tests
the audit deleted were the only direct callers of those methods; they are
replaced with tests that assert the forwarded shadow request and the
recursion guard.

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-09-25 11:30:43 -07:00

390 lines
11 KiB
Python

"""
Regression tests for streaming connection pool leak fix.
"""
import asyncio
from unittest.mock import MagicMock, patch
import anyio
import httpx
import pytest
from litellm.litellm_core_utils.streaming_handler import CustomStreamWrapper
from litellm.llms.custom_httpx.aiohttp_transport import (
AiohttpResponseStream,
LiteLLMAiohttpTransport,
)
# ── aiohttp transport layer tests ──────────────────────────────
@pytest.mark.asyncio
async def test_aiohttp_transport_response_uses_stream_not_content():
"""handle_async_request must use stream= so aclose() propagates to AiohttpResponseStream."""
class FakeSession:
closed = False
def __init__(self):
try:
self._loop = asyncio.get_running_loop()
except RuntimeError:
self._loop = None
def request(self, **kwargs):
class Resp:
status = 200
headers = {}
async def __aenter__(self):
return self
async def __aexit__(self, *args):
pass
@property
def content(self):
class C:
async def iter_chunked(self, size):
yield b"data"
return C()
return Resp()
transport = LiteLLMAiohttpTransport(client=lambda: FakeSession()) # type: ignore
response = await transport.handle_async_request(
httpx.Request("GET", "http://example.com")
)
assert isinstance(response.stream, AiohttpResponseStream)
@pytest.mark.asyncio
async def test_aiohttp_response_stream_aclose_releases_connection():
"""AiohttpResponseStream.aclose() must call __aexit__ on the aiohttp response."""
aexit_called = False
class MockResponse:
status = 200
headers = {}
@property
def content(self):
class C:
async def iter_chunked(self, size):
yield b"data"
return C()
async def __aexit__(self, *args):
nonlocal aexit_called
aexit_called = True
stream = AiohttpResponseStream(MockResponse()) # type: ignore
await stream.aclose()
assert aexit_called
# ── CustomStreamWrapper.aclose() tests ─────────────────────────
@pytest.mark.asyncio
async def test_aclose_falls_back_to_close():
"""OpenAI's AsyncStream has close() but not aclose(). Must fall back."""
close_called = False
class FakeAsyncStream:
async def close(self):
nonlocal close_called
close_called = True
wrapper = CustomStreamWrapper(
completion_stream=FakeAsyncStream(),
model=None,
logging_obj=MagicMock(),
custom_llm_provider=None,
)
await wrapper.aclose()
assert close_called
@pytest.mark.asyncio
async def test_aclose_prefers_aclose_over_close():
"""When both aclose() and close() exist, aclose() should be preferred."""
aclose_called = False
close_called = False
class FakeStream:
async def aclose(self):
nonlocal aclose_called
aclose_called = True
async def close(self):
nonlocal close_called
close_called = True
wrapper = CustomStreamWrapper(
completion_stream=FakeStream(),
model=None,
logging_obj=MagicMock(),
custom_llm_provider=None,
)
await wrapper.aclose()
assert aclose_called
assert not close_called
@pytest.mark.asyncio
async def test_aclose_completes_under_cancellation():
"""aclose() must shield cleanup from CancelledError so streams actually close."""
aclose_completed = False
class SlowCloseStream:
async def aclose(self):
await anyio.sleep(0)
nonlocal aclose_completed
aclose_completed = True
wrapper = CustomStreamWrapper(
completion_stream=SlowCloseStream(),
model=None,
logging_obj=MagicMock(),
custom_llm_provider=None,
)
with anyio.CancelScope() as scope:
scope.cancel()
await wrapper.aclose()
assert aclose_completed
# ── Router stream_with_fallbacks cleanup tests ──────────────────
@pytest.mark.asyncio
async def test_stream_with_fallbacks_closes_stream_on_generator_close():
"""Closing the FallbackStreamWrapper must aclose() the underlying model_response
via stream_with_fallbacks' finally block."""
from litellm.router import Router
stream_closed = False
class FakeStream(CustomStreamWrapper):
def __init__(self):
super().__init__(
completion_stream=None,
model="test-model",
logging_obj=MagicMock(),
custom_llm_provider="openai",
)
self._items = ["chunk1", "chunk2", "chunk3"]
self._index = 0
def __aiter__(self):
return self
async def __anext__(self):
if self._index >= len(self._items):
raise StopAsyncIteration
item = self._items[self._index]
self._index += 1
return item
async def aclose(self):
nonlocal stream_closed
stream_closed = True
router = Router(
model_list=[
{
"model_name": "test-model",
"litellm_params": {
"model": "openai/test",
"api_key": "fake",
},
}
]
)
fake_stream = FakeStream()
# Call _acompletion_streaming_iterator directly so we go through
# stream_with_fallbacks and its finally block
result = await router._acompletion_streaming_iterator(
model_response=fake_stream,
messages=[{"role": "user", "content": "hi"}],
initial_kwargs={"model": "test-model"},
)
# Consume one chunk then close (simulates client disconnect)
async for _ in result:
break
await result.aclose()
assert (
stream_closed
), "model_response stream was not closed by stream_with_fallbacks finally block"
@pytest.mark.asyncio
async def test_stream_with_fallbacks_closes_stream_on_normal_completion():
"""stream_with_fallbacks must aclose() model_response even on normal completion."""
from litellm.router import Router
stream_closed = False
class FakeStream(CustomStreamWrapper):
def __init__(self):
super().__init__(
completion_stream=None,
model="test-model",
logging_obj=MagicMock(),
custom_llm_provider="openai",
)
self._items = ["chunk1"]
self._index = 0
def __aiter__(self):
return self
async def __anext__(self):
if self._index >= len(self._items):
raise StopAsyncIteration
item = self._items[self._index]
self._index += 1
return item
async def aclose(self):
nonlocal stream_closed
stream_closed = True
router = Router(
model_list=[
{
"model_name": "test-model",
"litellm_params": {
"model": "openai/test",
"api_key": "fake",
},
}
]
)
fake_stream = FakeStream()
result = await router._acompletion_streaming_iterator(
model_response=fake_stream,
messages=[{"role": "user", "content": "hi"}],
initial_kwargs={"model": "test-model"},
)
# Exhaust the stream fully
async for _ in result:
pass
await result.aclose()
assert stream_closed, "model_response stream was not closed after normal completion"
@pytest.mark.asyncio
async def test_stream_with_fallbacks_closes_both_on_fallback_disconnect():
"""When a fallback is triggered and the client disconnects during fallback
iteration, both model_response and fallback_response must be closed."""
from litellm.exceptions import MidStreamFallbackError
from litellm.router import Router
model_closed = False
fallback_closed = False
class FakeModelStream(CustomStreamWrapper):
"""Stream that raises MidStreamFallbackError immediately to trigger fallback."""
def __init__(self):
super().__init__(
completion_stream=None,
model="test-model",
logging_obj=MagicMock(),
custom_llm_provider="openai",
)
self.chunks = []
def __aiter__(self):
return self
async def __anext__(self):
raise MidStreamFallbackError(
message="test mid-stream error",
model="test-model",
llm_provider="openai",
generated_content="",
)
async def aclose(self):
nonlocal model_closed
model_closed = True
class FakeFallbackStream:
"""Fallback stream that yields chunks."""
def __init__(self):
self._items = ["fb1", "fb2", "fb3"]
self._index = 0
def __aiter__(self):
return self
async def __anext__(self):
if self._index >= len(self._items):
raise StopAsyncIteration
item = self._items[self._index]
self._index += 1
return item
async def aclose(self):
nonlocal fallback_closed
fallback_closed = True
router = Router(
model_list=[
{
"model_name": "test-model",
"litellm_params": {
"model": "openai/test",
"api_key": "fake",
},
}
]
)
fake_model_stream = FakeModelStream()
fake_fallback_stream = FakeFallbackStream()
# Mock async_function_with_fallbacks_common_utils to return the fallback stream
# instead of actually calling through the full fallback machinery
with patch.object(
router,
"async_function_with_fallbacks_common_utils",
return_value=fake_fallback_stream,
):
result = await router._acompletion_streaming_iterator(
model_response=fake_model_stream,
messages=[{"role": "user", "content": "hi"}],
initial_kwargs={
"model": "test-model",
"fallbacks": ["other-model"],
},
)
# Consume one fallback chunk then close (simulates client disconnect)
async for _ in result:
break
await result.aclose()
assert model_closed, "model_response stream was not closed"
assert fallback_closed, "fallback_response stream was not closed"