litellm/tests/e2e_harness/perf/setup/test_event_loop.py
Kerry Lu 0a051efba7
test(e2e): event_loop_stalls perf case fails on a gateway event-loop hold over 100 ms that is not on its allowlist (#46974)
* test(e2e): event_loop_stalls perf case reads the gateway's asyncio debug log and fails on a loop hold over 100 ms that is not on its allowlist

The gateway runs the stdlib loop in debug mode (new LITELLM_EVENT_LOOP=asyncio knob next to
PYTHONASYNCIODEBUG=1), the case replays the mixed body mix at 1 rps and groups asyncio's
'Executing <Task ...> took X seconds' lines by coroutine and location. Known holds are listed with
the count they show today; anything else, or a known one past its count, fails the case. Opt-in
through E2E_PERF_EVENT_LOOP=1 so the nightly build skips it

* test(e2e): event_loop_stalls reads the pod list and pod logs through e2e_http instead of a raw httpx client

* test(e2e): event_loop_stalls charges a hold to garbage collection when the gateway's debug-mode gc log accounts for it

The proxy registers a gc.callbacks hook only when the event loop runs in debug mode and logs every
collection over the loop's slow-callback threshold (generation, collected count, seconds). The case
parser pairs such a pass with the hold line that follows it and reports the hold under a bounded
garbage collection bucket instead of the task asyncio happened to blame, which made 7 of 10 local
runs fail on a different task name each time. Holds the collector does not account for keep the
strict allowlist, which gains the prisma first-query warm-up and the second pub/sub subscriber

* test(e2e): event_loop_stalls allowlist caps set at twice the ten-run maximum

---------

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-17 06:55:53 +00:00

87 lines
3.9 KiB
Python

from __future__ import annotations
from typing import Final
from setup.event_loop import EventLoopStalls, KnownStall, offenders, parse_stalls, summarize
_HANDLER: Final = (
"Executing <Task pending name='Task-9' coro=<RequestResponseCycle.run_asgi() running at "
"/usr/local/lib/python3.12/site-packages/uvicorn/protocols/http/h11_impl.py:416> wait_for=<Future pending "
"cb=[Task.task_wakeup()]> created at /usr/local/lib/python3.12/asyncio/base_events.py:448> took 0.731 seconds"
)
_STREAM: Final = (
"Executing <Task pending name='Task-31' coro=<TaskHandle._run_coro() running at "
"/usr/local/lib/python3.12/site-packages/anyio/_core/_tasks.py:275> created at "
"/usr/local/lib/python3.12/asyncio/tasks.py:420> took 0.412 seconds"
)
_STARTUP: Final = (
"Executing <Task pending name='Task-2' coro=<LifespanOn.main() running at "
"/usr/local/lib/python3.12/site-packages/uvicorn/lifespan/on.py:86> created at "
"/usr/local/lib/python3.12/asyncio/base_events.py:448> took 0.302 seconds"
)
_GC_PASS: Final = (
"2026-10-17 04:46:02,101 - LiteLLM Proxy - WARNING - Garbage collection gen=2 collected=98608 took 0.711 seconds"
)
_SPEC: Final = EventLoopStalls(
allowlist=(
KnownStall("LifespanOn.main", max_count=3),
KnownStall("RequestResponseCycle.run_asgi", max_count=2),
KnownStall("garbage collection", max_count=1),
)
)
def test_parse_keeps_the_coroutine_its_location_and_the_hold() -> None:
(stall,) = parse_stalls(("INFO: plain log line", _HANDLER))
assert stall.coroutine == "RequestResponseCycle.run_asgi()"
assert stall.where == "uvicorn/protocols/http/h11_impl.py:416"
assert stall.seconds == 0.731
def test_a_location_off_the_allowlist_offends_once_however_often_it_appears() -> None:
rows: Final = summarize(_SPEC, parse_stalls((_STARTUP, _STREAM, _STREAM, _HANDLER)))
assert [(row.coroutine, row.count, row.offending) for row in rows] == [
("TaskHandle._run_coro()", 2, True),
("RequestResponseCycle.run_asgi()", 1, False),
("LifespanOn.main()", 1, False),
]
assert offenders(rows) == (
"TaskHandle._run_coro() anyio/_core/_tasks.py:275: 2x, max 0.41 s (not on the allowlist)",
)
def test_a_known_location_offends_only_past_its_count() -> None:
within: Final = summarize(_SPEC, parse_stalls((_HANDLER, _HANDLER)))
assert offenders(within) == ()
past: Final = summarize(_SPEC, parse_stalls((_HANDLER, _HANDLER, _HANDLER)))
assert offenders(past) == (
"RequestResponseCycle.run_asgi() uvicorn/protocols/http/h11_impl.py:416: 3x, max 0.73 s "
"(allowlist RequestResponseCycle.run_asgi permits 2)",
)
def test_holds_under_the_threshold_are_not_rows() -> None:
short: Final = _HANDLER.replace("took 0.731 seconds", "took 0.099 seconds")
assert summarize(_SPEC, parse_stalls((short,))) == ()
def test_a_hold_the_collector_pass_before_it_accounts_for_is_the_collectors_not_the_tasks() -> None:
rows: Final = summarize(_SPEC, parse_stalls((_GC_PASS, _STREAM.replace("0.412", "0.731"))))
assert [(row.coroutine, row.where, row.max_seconds, row.offending) for row in rows] == [
("garbage collection", "gen=2", 0.731, False)
]
def test_a_hold_well_past_its_collector_pass_stays_on_the_task() -> None:
short_pass: Final = _GC_PASS.replace("0.711", "0.200")
rows: Final = summarize(_SPEC, parse_stalls((short_pass, _STREAM.replace("0.412", "0.731"))))
assert [(row.coroutine, row.offending) for row in rows] == [("TaskHandle._run_coro()", True)]
def test_a_collector_pass_explains_only_the_next_hold_and_only_one_at_least_as_long() -> None:
rows: Final = summarize(_SPEC, parse_stalls((_GC_PASS, _STARTUP, _HANDLER)))
assert sorted((row.coroutine, row.offending) for row in rows) == [
("LifespanOn.main()", False),
("RequestResponseCycle.run_asgi()", False),
]
assert summarize(_SPEC, parse_stalls((_GC_PASS,))) == ()