Files
livekit-agents/tests/test_loop_monitor.py

1108 lines
45 KiB
Python

"""Event loop blocking detector (``telemetry.loop_monitor``).
The monitor runs a heartbeat on the loop under test and a watchdog thread that samples the loop
thread's stack, so these tests use real wall-clock time with short thresholds. Durations are
asserted with generous tolerances to stay stable on loaded CI machines."""
from __future__ import annotations
import asyncio
import contextvars
import gc
import importlib.util
import inspect
import linecache
import os
import sys
import threading
import time
import weakref
from collections.abc import Iterator
from pathlib import Path
from types import ModuleType, SimpleNamespace
import pytest
from opentelemetry import trace
from opentelemetry.sdk.trace import ReadableSpan, TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
from livekit.agents.telemetry import loop_monitor, set_tracer_provider, trace_types, tracer
from livekit.agents.telemetry.loop_monitor import (
ENV_ERROR_THRESHOLD_MS,
ENV_WARN_THRESHOLD_MS,
SPAN_NAME,
BlockedReport,
EventLoopMonitor,
LoopMonitorThresholds,
_RateLimiter,
)
pytestmark = [pytest.mark.unit, pytest.mark.no_concurrent]
WARN = 0.03
ERROR = 0.15
TICK = 0.005
@pytest.fixture
def span_exporter() -> Iterator[InMemorySpanExporter]:
original_provider = tracer._tracer_provider
provider = TracerProvider()
exporter = InMemorySpanExporter()
provider.add_span_processor(SimpleSpanProcessor(exporter))
set_tracer_provider(provider)
try:
yield exporter
finally:
set_tracer_provider(original_provider)
provider.shutdown()
def _fake_job_context(session: object | None = None) -> SimpleNamespace:
"""The JobContext surface the monitor touches."""
return SimpleNamespace(
_primary_agent_session=session,
# read by the span processors main installs (PII stripping, job attribution)
_redaction_enabled=False,
_telemetry_state=None,
job=SimpleNamespace(id="AJ_test", room=SimpleNamespace(sid="RM_test")),
)
def _job_report_context(job_ctx: object) -> contextvars.Context:
from livekit.agents.job import _JobContextVar
token = _JobContextVar.set(job_ctx) # type: ignore[arg-type]
try:
return contextvars.copy_context()
finally:
_JobContextVar.reset(token)
@pytest.fixture
async def monitor(span_exporter: InMemorySpanExporter) -> Iterator[EventLoopMonitor]: # type: ignore[misc]
"""A monitor reporting into a fake job whose primary session is already running, so spans
are emitted immediately under a live ``agent_session`` root (the common case). Tests for
the pre-session and no-job paths replace the report context themselves."""
m = EventLoopMonitor(
asyncio.get_running_loop(), warn_threshold=WARN, error_threshold=ERROR, tick_interval=TICK
)
reports: list[BlockedReport] = []
m._on_report = reports.append
m.reports = reports # type: ignore[attr-defined]
root = tracer.start_span("agent_session")
session = SimpleNamespace(
_root_span_context=trace.set_span_in_context(root),
_record_loop_stall=lambda duration, *, timestamp_ns: None,
)
m.set_report_context(_job_report_context(_fake_job_context(session=session)))
m.session_root = root # type: ignore[attr-defined]
m.start()
# let the heartbeat arm and settle before the test blocks the loop
await asyncio.sleep(WARN)
try:
yield m
finally:
m.stop()
root.end()
def _blocked_spans(exporter: InMemorySpanExporter) -> list[ReadableSpan]:
return [s for s in exporter.get_finished_spans() if s.name == SPAN_NAME]
def _block_loop_synchronously(duration: float) -> None:
# a deliberately blocking call on the loop thread, the thing the monitor exists to catch
time.sleep(duration)
_SLEEP_CALL = "line {}, in _block_loop_synchronously".format(
_block_loop_synchronously.__code__.co_firstlineno
+ next(
i
for i, ln in enumerate(inspect.getsource(_block_loop_synchronously).splitlines())
if "time.sleep" in ln
)
)
async def _settle() -> None:
# give the late heartbeat a chance to run and report
await asyncio.sleep(TICK * 4)
def _describe(reports: list[BlockedReport]) -> str:
"""Everything a failure needs to be diagnosed from CI output alone."""
return "\n".join(
f"{r.duration * 1000:.0f}ms (gc {r.gc_time * 1000:.0f}ms, cpu {r.cpu_time * 1000:.0f}ms, "
f"watchdog gap {r.watchdog_gap * 1000:.0f}ms, task {r.task_name}) "
+ " | ".join(s.replace(chr(10), " / ") for s in r.stacks)
for r in reports
)
def _loop_blocks(monitor: EventLoopMonitor) -> list[BlockedReport]:
"""Reports attributable to this loop. A loaded CI host can deschedule the whole process
for tens of milliseconds; the monitor reports that too (the loop really did stall) but
tags it, and it is not what the negative tests here are checking for."""
return [r for r in monitor.reports if not r.process_descheduled] # type: ignore[attr-defined]
async def test_blocking_call_is_reported_as_span(
span_exporter: InMemorySpanExporter, monitor: EventLoopMonitor
) -> None:
_block_loop_synchronously(0.2)
await _settle()
spans = _blocked_spans(span_exporter)
assert len(spans) == 1
span = spans[0]
attrs = span.attributes or {}
duration = attrs[trace_types.ATTR_BLOCKING_DURATION]
assert isinstance(duration, float)
# resolution is one tick; allow scheduler slop on top
assert 0.2 - TICK - 0.01 <= duration <= 0.2 + 0.1
assert attrs[trace_types.ATTR_BLOCKING_THRESHOLD] == WARN
assert attrs[trace_types.ATTR_BLOCKING_SEVERITY] == "error"
assert span.status.status_code == trace.StatusCode.ERROR
# the span is back-dated so it covers the block on the timeline
assert span.end_time is not None and span.start_time is not None
assert abs((span.end_time - span.start_time) / 1e9 - duration) < 1e-6
# the watchdog sampled the loop thread while it was stuck in time.sleep
stack = attrs[trace_types.ATTR_BLOCKING_STACK]
assert isinstance(stack, str)
assert _SLEEP_CALL in stack
# each sample says when in the stall it was taken: it is a sample, not a profile
assert "loop thread sampled" in stack and "ms into the stall" in stack
# the sample names the task that was running
assert attrs.get(trace_types.ATTR_BLOCKING_TASK)
# time.sleep is a wait, not compute: the loop thread burned almost no CPU
cpu = attrs[trace_types.ATTR_BLOCKING_CPU_TIME]
assert isinstance(cpu, float) and cpu < duration / 2
async def test_block_between_thresholds_is_a_warning(
span_exporter: InMemorySpanExporter, monitor: EventLoopMonitor
) -> None:
_block_loop_synchronously(0.07)
await _settle()
spans = _blocked_spans(span_exporter)
assert len(spans) == 1
attrs = spans[0].attributes or {}
assert attrs[trace_types.ATTR_BLOCKING_SEVERITY] == "warning"
assert spans[0].status.status_code == trace.StatusCode.UNSET
async def test_cooperative_work_is_not_reported(
span_exporter: InMemorySpanExporter, monitor: EventLoopMonitor
) -> None:
# plenty of short callbacks, none long enough to matter
for _ in range(40):
time.sleep(0.002)
await asyncio.sleep(0)
await asyncio.sleep(WARN * 2)
assert _loop_blocks(monitor) == []
async def test_stall_before_the_session_lands_in_the_job_trace(
span_exporter: InMemorySpanExporter, monitor: EventLoopMonitor
) -> None:
"""Before session.start() there is no session root to resolve; the stall is a child of
whatever the job's reporting context holds, job_entrypoint in a job process, at its
original time."""
job_ctx = _fake_job_context(session=None)
entrypoint = tracer.start_span("job_entrypoint")
with tracer.use_span(entrypoint, end_on_exit=False):
monitor.set_report_context(_job_report_context(job_ctx))
_block_loop_synchronously(0.07)
await _settle()
entrypoint.end()
[span] = _blocked_spans(span_exporter)
assert span.parent is not None
assert span.parent.span_id == entrypoint.get_span_context().span_id
assert span.start_time is not None and span.end_time is not None
assert span.end_time <= entrypoint.end_time # type: ignore[operator]
attrs = span.attributes or {}
assert attrs[trace_types.ATTR_BLOCKING_SEVERITY] == "warning"
assert _SLEEP_CALL in attrs[trace_types.ATTR_BLOCKING_STACK]
async def test_stall_without_a_job_is_log_only(
span_exporter: InMemorySpanExporter,
monitor: EventLoopMonitor,
caplog: pytest.LogCaptureFixture,
) -> None:
# no job context in the report context: the worker process, or a bare loop. There will
# never be a session to attach a span to, so the log carries it.
monitor.set_report_context(None)
with caplog.at_level("WARNING", logger="livekit.agents"):
_block_loop_synchronously(0.07)
await _settle()
assert _blocked_spans(span_exporter) == []
assert len(monitor.reports) == 1 # type: ignore[attr-defined]
assert any("event loop blocked for" in r.getMessage() for r in caplog.records)
async def test_stall_during_a_session_lands_under_it_and_is_summarised(
span_exporter: InMemorySpanExporter, monitor: EventLoopMonitor
) -> None:
from livekit.agents import Agent
from .fake_session import FakeActions, create_session, run_session
actions = FakeActions()
actions.add_user_speech(0.5, 1.0, "Hi", stt_delay=0.1)
actions.add_llm("Hello", ttft=0.05, duration=0.1)
actions.add_tts(0.2, ttfb=0.05, duration=0.1)
session = create_session(
actions, speed_factor=20.0, extra_kwargs={"session_close_transcript_timeout": 0.05}
)
job_ctx = _fake_job_context(session=session)
monitor.set_report_context(_job_report_context(job_ctx))
blocked = False
def _block_once(ev: object) -> None:
nonlocal blocked
if not blocked:
blocked = True
_block_loop_synchronously(0.07) # synchronous work inside a session callback
session.on("agent_state_changed", _block_once)
await run_session(session, Agent(instructions="test"), drain_delay=0.1)
[root] = [s for s in span_exporter.get_finished_spans() if s.name == "agent_session"]
stalls = _blocked_spans(span_exporter)
assert stalls, "the stall during the session was not emitted"
for stall in stalls:
assert stall.parent is not None and stall.parent.span_id == root.context.span_id
# the session span carries the summary so the session list can flag it
attrs = root.attributes or {}
assert attrs[trace_types.ATTR_BLOCKING_COUNT] == len(stalls)
assert attrs[trace_types.ATTR_BLOCKING_MAX_DURATION] >= 0.05
assert (
attrs[trace_types.ATTR_BLOCKING_TOTAL_DURATION]
>= attrs[trace_types.ATTR_BLOCKING_MAX_DURATION]
)
events = [e for e in root.events if e.name == SPAN_NAME]
assert len(events) == len(stalls)
async def test_stop_is_idempotent_and_quiets_the_monitor(
span_exporter: InMemorySpanExporter, monitor: EventLoopMonitor
) -> None:
monitor.stop()
monitor.stop()
_block_loop_synchronously(0.07)
await _settle()
assert _blocked_spans(span_exporter) == []
async def test_idle_loop_is_not_reported(
span_exporter: InMemorySpanExporter, monitor: EventLoopMonitor
) -> None:
# an idle loop sits in select() until the heartbeat is due; the timer fires on time
await asyncio.sleep(WARN * 6)
assert _loop_blocks(monitor) == []
async def test_blocking_work_in_an_executor_is_not_reported(
span_exporter: InMemorySpanExporter, monitor: EventLoopMonitor
) -> None:
# the whole point of run_in_executor / to_thread: the loop keeps ticking
loop = asyncio.get_running_loop()
await loop.run_in_executor(None, time.sleep, WARN * 3)
await asyncio.to_thread(time.sleep, WARN * 3)
await _settle()
assert _loop_blocks(monitor) == []
async def test_sustained_cooperative_load_is_not_reported(
span_exporter: InMemorySpanExporter, monitor: EventLoopMonitor
) -> None:
"""Heavy but cooperative work: many callbacks each well under the threshold, timers,
executor round trips, and tasks yielding to each other, for ~0.3 s of wall time. One loop
iteration runs every ready worker back to back, so the slices are sized to keep an
iteration far under WARN even with sleep overshoot."""
loop = asyncio.get_running_loop()
fired: list[int] = []
async def worker(n: int) -> None:
for _ in range(60):
time.sleep(0.001) # a small synchronous slice, well under WARN
await asyncio.sleep(0)
fired.append(n)
handles = [loop.call_later(i * 0.01, fired.append, 1000 + i) for i in range(20)]
started = time.monotonic()
await asyncio.gather(*(worker(n) for n in range(2)))
workers_took = time.monotonic() - started
await loop.run_in_executor(None, time.sleep, 0.05)
await asyncio.sleep(0.15)
for h in handles:
h.cancel()
await _settle()
assert len([f for f in fired if f < 1000]) == 2
# the workers hold the loop for ~0.25 s of 2 ms slices; a host that starved this process
# enough to stretch that past double is not a monitor false positive, and the
# watchdog-gap tag only catches starvation concentrated in one stall
if workers_took > 0.5:
pytest.skip(f"host starved the test process: workers took {workers_took:.2f}s")
assert _loop_blocks(monitor) == [], _describe(_loop_blocks(monitor))
async def test_one_iteration_of_many_ready_callbacks_is_one_stall(
span_exporter: InMemorySpanExporter, monitor: EventLoopMonitor
) -> None:
"""A burst of ready callbacks runs within one loop iteration, so the heartbeat cannot fire
in between: that is reported as a single stall of the burst's length, and the stack sample
lands in one of the callbacks."""
loop = asyncio.get_running_loop()
for _ in range(60):
loop.call_soon(time.sleep, 0.002) # ~120 ms of back-to-back callbacks
await asyncio.sleep(0)
await _settle()
spans = _blocked_spans(span_exporter)
assert len(spans) == 1
attrs = spans[0].attributes or {}
duration = attrs[trace_types.ATTR_BLOCKING_DURATION]
assert isinstance(duration, float) and 0.09 <= duration <= 0.3
# time.sleep is a C function scheduled directly, so it has no frame of its own: the
# innermost frame is asyncio's dispatch of the callback, which must be kept
stack = attrs[trace_types.ATTR_BLOCKING_STACK]
assert isinstance(stack, str)
assert os.path.join("asyncio", "events.py") in stack and stack.rstrip().endswith("in _run")
async def test_gc_pause_is_attributed(
span_exporter: InMemorySpanExporter, monitor: EventLoopMonitor
) -> None:
import gc
# a large heap of container objects makes a full collection measurably slow
heap = [{"i": i, "l": [i]} for i in range(400_000)]
gc.collect() # baseline, outside the measurement window
await _settle()
before = len(monitor.reports) # type: ignore[attr-defined]
start = time.perf_counter()
gc.collect()
elapsed = time.perf_counter() - start
await _settle()
del heap
reports = monitor.reports[before:] # type: ignore[attr-defined]
if elapsed < WARN:
pytest.skip(f"gc.collect took only {elapsed * 1000:.0f}ms on this machine")
assert len(reports) == 1
report = reports[0]
# the pause is a GC pause, and the span says so instead of blaming the coroutine
assert report.gc_time > 0
assert report.gc_time <= report.duration + 1e-3
assert report.gc_time >= report.duration * 0.5
async def test_worker_mode_logs_and_records_the_metric_without_spans(
span_exporter: InMemorySpanExporter, caplog: pytest.LogCaptureFixture
) -> None:
m = EventLoopMonitor(
asyncio.get_running_loop(),
warn_threshold=WARN,
error_threshold=ERROR,
tick_interval=TICK,
emit_spans=False,
)
reports: list[BlockedReport] = []
m._on_report = reports.append
m.start()
await asyncio.sleep(WARN)
try:
with caplog.at_level("WARNING", logger="livekit.agents"):
_block_loop_synchronously(0.08)
await _settle()
finally:
m.stop()
assert _blocked_spans(span_exporter) == []
assert len(reports) == 1
assert any("event loop blocked for" in r.getMessage() for r in caplog.records)
def test_host_descheduling_is_not_logged_and_does_not_use_the_log_quota(
caplog: pytest.LogCaptureFixture,
) -> None:
"""A stall the host caused is not the developer's to fix: no log line, and it must not
consume one of the per-minute log slots either, or a run of them would silence the
warnings for real blocks that follow."""
loop = asyncio.new_event_loop()
try:
m = EventLoopMonitor(loop, warn_threshold=WARN, error_threshold=ERROR, tick_interval=TICK)
def _report(*, descheduled: bool) -> BlockedReport:
return BlockedReport(
duration=0.8,
started_at=time.time() - 0.8,
warn_threshold=WARN,
severity="warning" if descheduled else "error",
gc_time=0.0,
cpu_time=0.001 if descheduled else 0.8,
watchdog_gap=0.75 if descheduled else 0.0,
process_descheduled=descheduled,
task_name=None,
stacks=[],
parent_span_context=None,
)
with caplog.at_level("DEBUG", logger="livekit.agents"):
for _ in range(loop_monitor.MAX_LOGS_PER_MINUTE + 2):
m._report(_report(descheduled=True))
assert not [r for r in caplog.records if "event loop" in r.getMessage()]
m._report(_report(descheduled=False))
finally:
loop.close()
assert [r for r in caplog.records if "event loop blocked" in r.getMessage()]
def test_host_descheduling_span_is_not_an_error() -> None:
"""A stall the host caused is reported at warning severity whatever its length, so the
span keeps UNSET status; the ERROR status is reserved for code that blocked the loop."""
loop = asyncio.new_event_loop()
try:
m = EventLoopMonitor(loop, warn_threshold=WARN, error_threshold=ERROR, tick_interval=TICK)
# the loop thread and the watchdog both woke ~1s late: nothing ran in between
descheduled = m._build_report(1.0, gc_time=0.0, cpu_time=0.0, watchdog_gap=0.9, samples=[])
blocked = m._build_report(1.0, gc_time=0.0, cpu_time=0.9, watchdog_gap=0.0, samples=[])
finally:
loop.close()
assert descheduled.process_descheduled and descheduled.severity == "warning"
assert not blocked.process_descheduled and blocked.severity == "error"
def test_gil_holding_call_is_blocked_code_not_descheduling() -> None:
"""A native call that never releases the GIL also stops the watchdog thread, so the
watchdog gap alone looks like host descheduling. The loop thread's CPU time separates
them: a descheduled process burns none, a GIL-holding call burns all of it."""
loop = asyncio.new_event_loop()
try:
m = EventLoopMonitor(loop, warn_threshold=WARN, error_threshold=ERROR, tick_interval=TICK)
gil_held = m._build_report(0.4, gc_time=0.0, cpu_time=0.39, watchdog_gap=0.39, samples=[])
descheduled = m._build_report(
0.4, gc_time=0.0, cpu_time=0.01, watchdog_gap=0.39, samples=[]
)
finally:
loop.close()
assert not gil_held.process_descheduled and gil_held.severity == "error"
# the sampler could not run either; the report says why instead of staying silent
assert gil_held.stacks and "held the GIL" in gil_held.stacks[0]
assert descheduled.process_descheduled and descheduled.severity == "warning"
assert descheduled.stacks == []
async def test_gil_holding_native_call_is_reported_end_to_end(
span_exporter: InMemorySpanExporter, monitor: EventLoopMonitor
) -> None:
# one C call that holds the GIL throughout; sized well above WARN on any machine
sum_line = sys._getframe().f_lineno + 1
sum(range(20_000_000))
await _settle()
reports = _loop_blocks(monitor)
assert len(reports) == 1, _describe(monitor.reports) # type: ignore[attr-defined]
[report] = reports
assert report.cpu_time >= report.duration * 0.5
[span] = _blocked_spans(span_exporter)
# the watchdog usually cannot run at all and the report says so; it can also get the GIL
# in the instant the call returns and sample the frame still on that line. Either names
# the cause; what must not happen is a silent or host-attributed report.
stack = str((span.attributes or {})[trace_types.ATTR_BLOCKING_STACK])
sum_call = f"line {sum_line}, in test_gil_holding_native_call_is_reported_end_to_end"
assert "held the GIL" in stack or sum_call in stack, stack
def test_metric_is_recorded_for_every_stall_past_the_rate_limits(
monkeypatch: pytest.MonkeyPatch,
) -> None:
recorded: list[float] = []
monkeypatch.setattr(
loop_monitor.otel_metrics,
"record_event_loop_blocked",
lambda duration, *, severity: recorded.append(duration),
)
loop = asyncio.new_event_loop()
try:
m = EventLoopMonitor(
loop, warn_threshold=WARN, error_threshold=ERROR, tick_interval=TICK, emit_spans=False
)
emitted: list[BlockedReport] = []
m._on_report = emitted.append
for _ in range(40):
m._report(m._build_report(0.1, gc_time=0.0, cpu_time=0.1, watchdog_gap=0.0, samples=[]))
finally:
loop.close()
assert len(recorded) == 40 # every stall, the histogram is never rate limited
assert len(emitted) == 30 # spans (and the on_report callback) stop at the span limit
def test_metric_carries_the_job_even_when_rate_limited(monkeypatch: pytest.MonkeyPatch) -> None:
"""The heartbeat runs in the monitor's own context, which predates the job; the metric
must be recorded inside the saved job context or `_job_attrs` finds no job."""
from livekit.agents.job import get_job_context
seen: list[object] = []
monkeypatch.setattr(
loop_monitor.otel_metrics,
"record_event_loop_blocked",
lambda duration, *, severity: seen.append(get_job_context(required=False)),
)
loop = asyncio.new_event_loop()
try:
m = EventLoopMonitor(
loop, warn_threshold=WARN, error_threshold=ERROR, tick_interval=TICK, emit_spans=False
)
job = _fake_job_context()
m.set_report_context(_job_report_context(job))
for _ in range(40):
m._report(m._build_report(0.1, gc_time=0.0, cpu_time=0.1, watchdog_gap=0.0, samples=[]))
finally:
loop.close()
assert len(seen) == 40
assert all(ctx is job for ctx in seen), "a measurement lost its job attribution"
def test_stack_format_collapses_import_machinery() -> None:
"""A lazy import stalls through a dozen importlib frames per package level; the sample
must still show the caller that triggered it, not twenty lines of importlib."""
import traceback
def frame(filename: str, lineno: int, name: str) -> traceback.FrameSummary:
return traceback.FrameSummary(filename, lineno, name, line="")
frames = [frame("/app/agent.py", 10, "entrypoint")]
frames += [frame("/app/plugin/llm.py", 55, "prewarm")]
frames += [
frame("<frozen importlib._bootstrap>", 1360 + i, "_find_and_load") for i in range(12)
]
frames += [frame("/site-packages/sdk/resources/__init__.py", 11, "<module>")]
frames += [
frame("<frozen importlib._bootstrap_external>", 1500 + i, "find_spec") for i in range(9)
]
frames += [frame("<frozen importlib._bootstrap_external>", 152, "_path_stat")]
out = loop_monitor._format_frames(frames)
lines = out.splitlines()
assert any("agent.py" in ln for ln in lines) and any("prewarm" in ln for ln in lines)
assert "[import system: 12 frames]" in out
assert "[import system: 9 frames]" in out
assert "resources/__init__.py" in out
# the innermost frame stays real: it names the syscall the loop was stuck in
assert lines[-1].strip().startswith('File "<frozen importlib._bootstrap_external>", line 152')
assert len([ln for ln in lines if ln.strip().startswith("File ")]) == 4
# and the innermost real location is what the log message points at
assert loop_monitor._innermost_location(out) is not None
def test_rate_limiter_counts_suppressed() -> None:
limiter = _RateLimiter(2)
assert limiter.allow(100.0)
assert limiter.allow(100.1)
assert not limiter.allow(100.2)
assert not limiter.allow(100.3)
assert limiter.take_suppressed() == 2
assert limiter.take_suppressed() == 0
# the window slides
assert limiter.allow(161.0)
def test_thresholds_from_env(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv(ENV_WARN_THRESHOLD_MS, raising=False)
monkeypatch.delenv(ENV_ERROR_THRESHOLD_MS, raising=False)
t = LoopMonitorThresholds.from_env()
assert t == LoopMonitorThresholds(warn=0.1, error=0.5)
monkeypatch.setenv(ENV_WARN_THRESHOLD_MS, "100")
monkeypatch.setenv(ENV_ERROR_THRESHOLD_MS, "1000")
assert LoopMonitorThresholds.from_env() == LoopMonitorThresholds(warn=0.1, error=1.0)
# zero disables
monkeypatch.setenv(ENV_WARN_THRESHOLD_MS, "0")
assert LoopMonitorThresholds.from_env() is None
# garbage falls back to the default rather than disabling
monkeypatch.setenv(ENV_WARN_THRESHOLD_MS, "fast")
assert LoopMonitorThresholds.from_env() == LoopMonitorThresholds(warn=0.1, error=1.0)
# so do NaN and infinity: NaN compares false against every lag and would report each tick
for bad in ("NaN", "inf", "-inf"):
monkeypatch.setenv(ENV_WARN_THRESHOLD_MS, bad)
assert LoopMonitorThresholds.from_env() == LoopMonitorThresholds(warn=0.1, error=1.0)
# error below warn is clamped up to warn
monkeypatch.setenv(ENV_WARN_THRESHOLD_MS, "200")
monkeypatch.setenv(ENV_ERROR_THRESHOLD_MS, "20")
assert LoopMonitorThresholds.from_env() == LoopMonitorThresholds(warn=0.2, error=0.2)
async def test_registry_starts_once_per_loop_and_stops(monkeypatch: pytest.MonkeyPatch) -> None:
loop = asyncio.get_running_loop()
thresholds = LoopMonitorThresholds(warn=WARN, error=ERROR)
m = loop_monitor.start_monitoring(loop, thresholds=thresholds)
try:
assert m is not None
assert loop_monitor.get_monitor(loop) is m
assert loop_monitor.start_monitoring(loop, thresholds=thresholds) is None
finally:
loop_monitor.stop_monitoring(loop)
assert loop_monitor.get_monitor(loop) is None
monkeypatch.setenv(ENV_WARN_THRESHOLD_MS, "0")
assert loop_monitor.start_monitoring(loop) is None
def test_constructor_validates_thresholds() -> None:
loop = asyncio.new_event_loop()
try:
with pytest.raises(ValueError):
EventLoopMonitor(loop, warn_threshold=0)
with pytest.raises(ValueError):
EventLoopMonitor(loop, warn_threshold=0.1, error_threshold=0.05)
with pytest.raises(ValueError):
EventLoopMonitor(loop, warn_threshold=0.01, tick_interval=0.05)
finally:
loop.close()
def test_watchdog_gap_survives_the_wake_up_race() -> None:
"""When a descheduled process resumes, the loop thread may tick before the watchdog has
recorded its late wake-up. The gap must still be attributed to this stall, and a record
from an earlier stall must not leak into a later one."""
loop = asyncio.new_event_loop()
try:
m = EventLoopMonitor(loop, warn_threshold=WARN, error_threshold=ERROR, tick_interval=TICK)
now = time.monotonic()
window_start = now - 0.5 # this tick's window: a 0.5 s stall
# watchdog not yet back on the CPU: its last wake predates the window
m._watchdog_late = None
m._watchdog_last_wake = window_start - TICK
assert m._consume_watchdog_gap(now, window_start=window_start) == pytest.approx(
0.5, abs=TICK
)
# watchdog ran first and recorded the late wake inside the window
m._watchdog_late = (now - 0.001, 0.48)
m._watchdog_last_wake = now - 0.001
assert m._consume_watchdog_gap(now, window_start=window_start) == pytest.approx(0.48)
assert m._watchdog_late is None # consumed
# a stale record from before the window, and a watchdog that has run since: no gap
m._watchdog_late = (window_start - 1.0, 0.9)
m._watchdog_last_wake = now - 0.002
assert m._consume_watchdog_gap(now, window_start=window_start) == 0.0
m._watchdog_late = None
m._watchdog_last_wake = window_start + 0.01
assert m._consume_watchdog_gap(now, window_start=window_start) == pytest.approx(0.49 - TICK)
finally:
loop.close()
def test_session_summary_counts_every_stall_past_the_span_limit() -> None:
loop = asyncio.new_event_loop()
recorded: list[float] = []
try:
m = EventLoopMonitor(loop, warn_threshold=WARN, error_threshold=ERROR, tick_interval=TICK)
root = tracer.start_span("agent_session")
session = SimpleNamespace(
_root_span_context=trace.set_span_in_context(root),
_record_loop_stall=lambda duration, *, timestamp_ns: recorded.append(duration),
)
m.set_report_context(_job_report_context(_fake_job_context(session=session)))
for _ in range(40):
m._report(m._build_report(0.1, gc_time=0.0, cpu_time=0.1, watchdog_gap=0.0, samples=[]))
root.end()
finally:
loop.close()
assert len(recorded) == 40 # 30 spans were emitted, the summary saw all 40
@pytest.mark.skipif(not hasattr(asyncio.Task, "get_context"), reason="Task.get_context is 3.12+")
async def test_stall_nests_under_the_blocked_tasks_span(
span_exporter: InMemorySpanExporter, monitor: EventLoopMonitor
) -> None:
"""A stall inside an rpc handler (or a tool, or the user hook) is that span's problem: the
watchdog sample carries the blocked task's current span and the stall nests under it."""
async def handler() -> None:
with tracer.start_as_current_span("rpc_handler"):
_block_loop_synchronously(0.08)
await asyncio.create_task(handler(), name="rpc_handler_task")
await _settle()
[handler_span] = [s for s in span_exporter.get_finished_spans() if s.name == "rpc_handler"]
[stall] = _blocked_spans(span_exporter)
assert stall.parent is not None
assert stall.parent.span_id == handler_span.context.span_id
assert (stall.attributes or {})[trace_types.ATTR_BLOCKING_TASK] == "rpc_handler_task"
def test_stack_format_cuts_through_the_job_runner() -> None:
import traceback
def frame(filename: str, lineno: int, name: str) -> traceback.FrameSummary:
return traceback.FrameSummary(filename, lineno, name, line="")
ipc = "/site/livekit/agents/ipc/"
frames = [
frame("/py/multiprocessing/spawn.py", 1, "_main"),
frame(ipc + "job_proc_lazy_main.py", 100, "proc_main"),
frame(ipc + "proc_client.py", 70, "run"),
frame(ipc + "job_proc_lazy_main.py", 350, "_traceable_entrypoint"),
frame("/site/livekit/rtc/participant.py", 600, "_handle_rpc"),
frame("/app/agent.py", 147, "_test_call"),
frame("/py/time.py", 1, "sleep"),
]
out = loop_monitor._format_frames(frames)
assert "job_proc_lazy_main" not in out and "proc_client" not in out and "spawn.py" not in out
assert "participant.py" in out and "agent.py" in out and out.rstrip().endswith("in sleep")
# a stall entirely inside the runner keeps its frames: there is nothing else to show
inside = frames[:4]
assert "job_proc_lazy_main" in loop_monitor._format_frames(inside)
def test_a_block_the_watchdog_missed_is_still_blocking_code() -> None:
"""No stack sample and no CPU is not host descheduling by itself: a blocking wait that
released the GIL (a socket read, time.sleep) can finish before the watchdog looks. Only a
watchdog that was itself starved says the process was not running."""
loop = asyncio.new_event_loop()
try:
m = EventLoopMonitor(loop, warn_threshold=WARN, error_threshold=ERROR, tick_interval=TICK)
missed = m._build_report(0.11, gc_time=0.0, cpu_time=0.0001, watchdog_gap=0.0, samples=[])
assert not missed.process_descheduled
assert missed.stacks and "before the watchdog looked" in missed.stacks[0]
sample = loop_monitor._StackSample(
lag=0.06,
task_name="t",
frames=[__import__("traceback").FrameSummary("/app/a.py", 1, "f")],
)
waited = m._build_report(
0.11, gc_time=0.0, cpu_time=0.0001, watchdog_gap=0.0, samples=[sample]
)
assert not waited.process_descheduled and "/app/a.py" in waited.stacks[0]
finally:
loop.close()
def test_formatting_a_report_reads_no_source_file(tmp_path: Path) -> None:
"""The heartbeat formats the report on the loop thread right after the block ends, so it
must not read source files: linecache reads and splits each file in Python, holding up
the loop it is reporting on."""
module_path = tmp_path / "blocking_module.py"
module_path.write_text("x = 1\n" * 1000 + "def call(block):\n return block()\n")
spec = importlib.util.spec_from_file_location("blocking_module", module_path)
assert spec is not None and spec.loader is not None
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
loop = asyncio.new_event_loop()
try:
m = EventLoopMonitor(loop, warn_threshold=WARN, error_threshold=ERROR, tick_interval=TICK)
m._loop_thread_ident = threading.get_ident()
def sampled_by_watchdog() -> loop_monitor._StackSample:
samples: list[loop_monitor._StackSample] = []
t = threading.Thread(target=lambda: samples.append(m._sample_loop_thread(0.06)))
t.start()
t.join()
return samples[0]
sample = module.call(sampled_by_watchdog)
linecache.clearcache()
report = m._build_report(
0.11, gc_time=0.0, cpu_time=0.0, watchdog_gap=0.0, samples=[sample]
)
assert f'File "{module_path}", line 1002, in call' in report.stacks[0]
read = [name for name, entry in linecache.cache.items() if len(entry) != 1]
assert read == []
finally:
loop.close()
@pytest.mark.parametrize("limit", [0, 1, 3])
def test_a_stack_sample_obeys_the_traceback_limit(
monkeypatch: pytest.MonkeyPatch, limit: int
) -> None:
monkeypatch.setattr(sys, "tracebacklimit", limit, raising=False)
loop = asyncio.new_event_loop()
try:
m = EventLoopMonitor(loop, warn_threshold=WARN, error_threshold=ERROR, tick_interval=TICK)
m._loop_thread_ident = threading.get_ident()
samples: list[loop_monitor._StackSample] = []
t = threading.Thread(target=lambda: samples.append(m._sample_loop_thread(0.06)))
t.start()
t.join()
assert len(samples[0].frames) == limit
finally:
loop.close()
def test_no_thread_reads_source_for_a_report(tmp_path: Path) -> None:
"""A source read can take as long as a file read under GIL contention. On the loop thread
it stalls the loop again. On the watchdog thread it delays the next wake-up, which the
heartbeat reads as the process being descheduled. Sampling and formatting read none."""
readers: list[int] = []
entered_hold = threading.Event()
stalled = threading.Lock()
stalled.acquire()
source = "def hold(lock, entered):\n entered.set()\n lock.acquire(True, 10)\n"
class RecordingLoader:
def get_source(self, name: str) -> str:
readers.append(threading.get_ident())
return source
module = ModuleType("loader_source")
module.__loader__ = RecordingLoader() # type: ignore[assignment]
exec(compile(source, str(tmp_path / "missing" / "loader_source.py"), "exec"), module.__dict__)
loop = asyncio.new_event_loop()
try:
m = EventLoopMonitor(
loop, warn_threshold=WARN, error_threshold=ERROR, tick_interval=TICK, emit_spans=False
)
reports: list[BlockedReport] = []
m._on_report = reports.append
loop_thread = m._loop_thread_ident = threading.get_ident()
m._last_tick_at = time.monotonic() - 1.0 # the loop has been stalled for a second
def watchdog_check_then_end_the_stall() -> None:
entered_hold.wait(10)
while sys._current_frames()[loop_thread].f_code is not module.hold.__code__:
pass # until Event.set has returned into hold
m._watchdog_check()
stalled.release()
watchdog = threading.Thread(target=watchdog_check_then_end_the_stall)
watchdog.start()
module.hold(stalled, entered_hold)
watchdog.join()
m._on_tick() # the heartbeat that ends the stall
finally:
loop.close()
assert readers == []
[report] = reports
assert report.stacks[0].rstrip().endswith("line 3, in hold")
def test_watchdog_samples_one_tick_before_the_threshold() -> None:
"""The first sample is taken once the lag is within one heartbeat of the warn threshold,
so a block that only just crosses the threshold has a stack instead of ending before the
watchdog looks; a smaller lag (ordinary jitter) is never sampled."""
loop = asyncio.new_event_loop()
try:
m = EventLoopMonitor(loop, warn_threshold=WARN, error_threshold=ERROR, tick_interval=TICK)
m._last_tick_at = time.monotonic() - TICK - (WARN - TICK) * 0.6
m._watchdog_check()
assert m._incident is None
m._last_tick_at = time.monotonic() - TICK - (WARN - TICK) - TICK * 0.2
m._watchdog_check()
assert m._incident is not None and len(m._incident.samples) == 1
m._watchdog_check() # the same incident is not sampled twice before the late sample
assert len(m._incident.samples) == 1
finally:
loop.close()
async def test_sampler_finds_the_loop_thread_by_the_blocked_task(
span_exporter: InMemorySpanExporter, monitor: EventLoopMonitor
) -> None:
"""A wrong thread ident (the loop run by another thread than the one that armed the
monitor) must not lose the stack: the blocked task's coroutine frame identifies the thread."""
monitor._loop_thread_ident = -1
async def handler() -> None:
_block_loop_synchronously(0.08)
await asyncio.create_task(handler(), name="blocked_task")
await _settle()
[stall] = _blocked_spans(span_exporter)
attrs = stall.attributes or {}
assert _SLEEP_CALL in attrs[trace_types.ATTR_BLOCKING_STACK]
assert attrs[trace_types.ATTR_BLOCKING_TASK] == "blocked_task"
assert monitor._loop_thread_ident == __import__("threading").get_ident()
def test_a_stack_sample_frees_the_sampled_threads_frames_on_return() -> None:
"""A stack sample must not leave a reference cycle behind. Through one, it would keep the
frame of every thread it saw, with all of its locals, alive until a cyclic collection.
The automatic collector is off, so only reference counting can free the sampled local."""
class Payload:
pass
held = threading.Event()
release = threading.Event()
payload_ref: list[weakref.ref[Payload]] = []
def hold_a_local() -> None:
payload = Payload()
payload_ref.append(weakref.ref(payload))
held.set()
release.wait(5)
loop = asyncio.new_event_loop()
thread = threading.Thread(target=hold_a_local)
gc_was_enabled = gc.isenabled()
gc.disable()
try:
m = EventLoopMonitor(loop, warn_threshold=WARN, error_threshold=ERROR, tick_interval=TICK)
thread.start()
assert held.wait(5)
m._loop_thread_ident = thread.ident
sample = m._sample_loop_thread(lag=WARN)
assert any(f.name == "hold_a_local" for f in sample.frames)
release.set()
thread.join(5)
assert payload_ref[0]() is None, "the sample keeps the sampled thread's frame alive"
finally:
release.set()
thread.join(5)
if gc_was_enabled:
gc.enable()
loop.close()
def test_a_stack_sample_leaves_the_linecache_alone() -> None:
"""The report uses only the file, line and function of each frame. The sample must not
add a linecache entry for a new file, and must not stat a file that is already cached."""
held = threading.Event()
release = threading.Event()
def hold() -> None:
held.set()
release.wait(5)
threading_file = threading.Event.wait.__code__.co_filename
test_file = hold.__code__.co_filename
saved = {name: linecache.cache.get(name) for name in (threading_file, test_file)}
linecache.cache.pop(threading_file, None)
# a stale entry: a stat of the file would show the mismatch and drop it
stale_entry = (0, -1.0, [], test_file)
linecache.cache[test_file] = stale_entry
loop = asyncio.new_event_loop()
thread = threading.Thread(target=hold)
try:
m = EventLoopMonitor(loop, warn_threshold=WARN, error_threshold=ERROR, tick_interval=TICK)
thread.start()
assert held.wait(5)
m._loop_thread_ident = thread.ident
sample = m._sample_loop_thread(lag=WARN)
assert any(f.name == "hold" and f.filename == test_file for f in sample.frames)
assert any(f.filename == threading_file for f in sample.frames)
assert threading_file not in linecache.cache, "the sample added a linecache entry"
assert linecache.cache.get(test_file) is stale_entry, "the sample stat-ed a cached file"
finally:
release.set()
thread.join(5)
loop.close()
for name, entry in saved.items():
if entry is None:
linecache.cache.pop(name, None)
else:
linecache.cache[name] = entry
def test_import_block_names_the_module_and_the_caller(
tmp_path: pytest.TempPathFactory, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A lazy import blocking the loop is reported by the module being imported and by the
frame that triggered it; the import machinery's own frames say nothing actionable."""
import importlib
import traceback
(tmp_path / "slowmod.py").write_text( # type: ignore[operator]
"import sys, threading\n"
"from livekit.agents.telemetry import loop_monitor\n"
"frame = sys._current_frames()[threading.get_ident()]\n"
"importing = loop_monitor._module_being_imported(frame)\n"
)
monkeypatch.syspath_prepend(str(tmp_path))
mod = importlib.import_module("slowmod")
assert mod.importing == "slowmod"
frames = [
traceback.FrameSummary("/app/agent.py", 10, "on_enter"),
traceback.FrameSummary("<frozen importlib._bootstrap>", 1, "_find_and_load"),
traceback.FrameSummary("<frozen importlib._bootstrap>", 2, "_load_unlocked"),
traceback.FrameSummary("<frozen importlib._bootstrap_external>", 3, "get_code"),
]
text = loop_monitor._format_frames(frames, importing="slowmod")
assert "importing slowmod" in text
assert "on_enter" in text
# the log names the caller, not the import machinery
assert "agent.py" in (loop_monitor._innermost_location(text) or "")
def test_tick_interval_is_a_fifth_of_the_warn_threshold() -> None:
"""Blocks are measured to within one tick, so detection just over the threshold needs
several ticks per threshold; more than that only costs wake-ups."""
assert loop_monitor._tick_interval_for(0.1) == pytest.approx(0.02)
assert loop_monitor._tick_interval_for(0.25) == pytest.approx(0.05)
assert loop_monitor._tick_interval_for(1.0) == pytest.approx(0.05) # capped
assert loop_monitor._tick_interval_for(0.05) == pytest.approx(0.02) # floored
loop = asyncio.new_event_loop()
try:
m = loop_monitor.start_monitoring(
loop, thresholds=LoopMonitorThresholds(warn=0.1, error=0.5), emit_spans=False
)
assert m is not None and m._tick == pytest.approx(0.02)
finally:
loop_monitor.stop_monitoring(loop)
loop.close()
def test_a_warn_threshold_below_the_tick_floor_is_raised_to_it(
caplog: pytest.LogCaptureFixture,
) -> None:
"""The monitor never ticks faster than 20 ms, so it cannot measure a smaller threshold;
rather than fail to start (the tick would exceed the threshold), the threshold is raised
and said so."""
loop = asyncio.new_event_loop()
try:
with caplog.at_level("WARNING", logger="livekit.agents"):
m = loop_monitor.start_monitoring(
loop, thresholds=LoopMonitorThresholds(warn=0.005, error=0.01), emit_spans=False
)
assert m is not None
assert m.warn_threshold == pytest.approx(0.02) and m.error_threshold == pytest.approx(0.02)
assert m._tick == pytest.approx(0.02)
assert any("raised to 20ms" in r.getMessage() for r in caplog.records)
finally:
loop_monitor.stop_monitoring(loop)
loop.close()