Files
livekit-agents/python/tests/conformance/test_worker_ipc_resources.py
T

588 lines
22 KiB
Python

"""Real multiprocessing launch, native health supervision and independent reaping receipts."""
from __future__ import annotations
import asyncio
import contextlib
import gc
import json
import logging
import multiprocessing as mp
import os
import pickle
import signal
import socket
import sys
import time
import weakref
from collections.abc import AsyncIterator, Callable
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from typing import Any
import psutil
import pytest
from livekit.agents import _core
from livekit.agents._generated.worker_ipc import (
IpcFailureIo,
IpcFailureMalformed,
IpcFailureTooLarge,
JobCompletion,
StartJob,
)
from livekit.agents.ipc import JobLauncher, JobRunnerOptions
from livekit.protocol import agent
from tests.conformance.ipc_processes import running_job
pytestmark = pytest.mark.e2e
_METHODS = ["spawn", *(["forkserver"] if sys.platform == "linux" else [])]
_CONCURRENT_JOBS = 3
_PROCESS_INITIALIZE_TIMEOUT = 10.0
_PROCESS_CLOSE_TIMEOUT = 10.0
# Tokio's blocking pool retires idle workers after ten seconds when no override is set.
_NATIVE_CACHE_IDLE_TIMEOUT = 10.0
def _kill_pid(pid: int) -> None:
if os.name == "nt":
psutil.Process(pid).kill()
else:
os.kill(pid, signal.SIGKILL)
class _ChildHost:
async def _core_start_job(self, job: StartJob) -> None:
# The marker proves the host callback entered before its deliberate event-loop stall.
Path(job.info.accept_arguments.metadata).write_text("entered")
time.sleep(60)
async def _core_ping(self) -> None:
pass
def _ipc_child(endpoint: socket.socket, behavior: str) -> None:
if behavior.startswith("wire_"):
# The parent's channel preamble, which a child of the same version echoes, then the
# length of its Hello: the parent admitted Initialize.
preamble = b""
while len(preamble) < 8:
preamble += endpoint.recv(8 - len(preamble))
endpoint.recv(4)
frames = {
"wire_malformed": bytes([0, 0, 0, 1, 99]),
"wire_truncated": bytes([0, 0, 0, 5, 1]),
"wire_oversized_prefix": (2**32 - 1).to_bytes(4, "big"),
"wire_eof": b"",
}
if behavior != "wire_eof":
endpoint.sendall(preamble + frames[behavior])
if behavior == "wire_oversized_prefix":
time.sleep(60) # no payload: refusal must follow the prefix alone
endpoint.close()
os._exit(0)
_core.init(_core.Role.Job)
def initialize(proxy: str | None) -> None:
if behavior == "failed_initialize":
raise ValueError("fixture initialization failed")
if behavior == "blocked_initialize":
time.sleep(60)
client = _core.IpcClient(endpoint.detach(), initialize, process=True)
async def serve() -> None:
try:
await client.serve(_ChildHost())
finally:
await client.aclose()
asyncio.run(serve())
os._exit(0)
async def _unused_entrypoint(context: Any) -> None:
assert context.job.metadata == f"wire-{context.job.id}"
context.shutdown("stress complete")
class _Launcher(JobLauncher):
def __init__(self, method: str, behavior: str) -> None:
super().__init__(
job_entrypoint_fnc=_unused_entrypoint,
session_end_fnc=None,
simulation_end_fnc=None,
session_end_timeout=0.25,
mp_ctx=mp.get_context(method),
loop=asyncio.get_running_loop(),
)
self.method = method
self.behavior = behavior
self.launch_gate: asyncio.Event | None = None
self.launch_entered = asyncio.Event()
self.process_spawned = asyncio.Event()
self.receipt_gate: asyncio.Event | None = None
self.completion: JobCompletion | None = None
async def _core_completed(self, completion: JobCompletion) -> None:
self.completion = completion
await super()._core_completed(completion)
def _create_process(self, mp_ctx: Any, endpoint: Any) -> Any:
if self.behavior == "real_job":
return super()._create_process(mp_ctx, endpoint)
return mp_ctx.Process(target=_ipc_child, args=(endpoint, self.behavior))
async def _core_launch(self, endpoint: int, launch: _core.JobLaunch) -> None:
self.launch_entered.set()
if self.launch_gate is not None:
await self.launch_gate.wait()
await super()._core_launch(endpoint, launch)
self.process_spawned.set()
if self.receipt_gate is not None:
await self.receipt_gate.wait()
def _options(behavior: str) -> JobRunnerOptions:
production_deadlines = behavior in {"real_job", "watchdog"}
return JobRunnerOptions(
initialize_timeout=2 if behavior == "blocked_initialize" else _PROCESS_INITIALIZE_TIMEOUT,
close_timeout=_PROCESS_CLOSE_TIMEOUT if production_deadlines else 0.25,
ping_interval=0.05,
ping_timeout=60 if production_deadlines else 0.2,
high_ping_threshold=0.1,
)
class _Executor:
"""A job runner and its launcher, as these tests drive and observe them."""
def __init__(self, method: str, behavior: str = "normal") -> None:
self.launcher = _Launcher(method, behavior)
self.sup = _core.JobSupervisor(self.launcher, _options(behavior))
def __getattr__(self, name: str) -> Any:
return getattr(self.sup, name)
async def aclose(self) -> None:
await self.sup.aclose()
@pytest.fixture
async def executor_factory(
own_executor: Callable[[Any], Any],
) -> AsyncIterator[Callable[[str, str], _Executor]]:
owned: list[_Executor] = []
def create(method: str, behavior: str = "normal") -> _Executor:
executor = _Executor(method, behavior)
own_executor(executor)
owned.append(executor)
return executor
try:
yield create
finally:
for executor in owned:
if executor.launcher.launch_gate is not None:
executor.launcher.launch_gate.set()
if executor.launcher.receipt_gate is not None:
executor.launcher.receipt_gate.set()
async def _assert_reaped(executor: Any, method: str) -> None:
await asyncio.wait_for(executor.join(), timeout=5)
assert executor.exitcode is not None, "native owner must deliver the start method's receipt"
pid = executor.pid
assert pid is not None
assert not psutil.pid_exists(pid), "a zombie is not a reaped process"
if os.name == "posix" and method == "spawn":
with pytest.raises(ChildProcessError):
os.waitpid(pid, os.WNOHANG)
@pytest.mark.parametrize("method", _METHODS)
async def test_process_resource_lifecycle_matrix(
method: str, executor_factory: Callable[[str, str], _Executor]
) -> None:
executor = executor_factory(method, "normal")
await executor.start()
await executor.initialize()
await asyncio.wait_for(executor.aclose(), timeout=3)
await _assert_reaped(executor, method)
assert executor.exitcode == 0
@pytest.mark.parametrize("method", _METHODS)
@pytest.mark.parametrize("behavior", ["failed_initialize", "blocked_initialize"])
async def test_failed_initialization_retains_native_cleanup_ownership(
method: str, behavior: str, executor_factory: Callable[[str, str], _Executor]
) -> None:
executor = executor_factory(method, behavior)
await executor.start()
with pytest.raises((RuntimeError, TimeoutError, ConnectionError)):
await executor.initialize()
await asyncio.wait_for(executor.aclose(), timeout=3)
await _assert_reaped(executor, method)
assert executor.exitcode != 0
@pytest.mark.parametrize("method", _METHODS)
@pytest.mark.parametrize("after_launch", [False, True])
async def test_cancelled_start_does_not_abandon_a_launched_process(
method: str, after_launch: bool, executor_factory: Callable[[str, str], _Executor]
) -> None:
executor = executor_factory(method, "normal")
executor.launcher.launch_gate = asyncio.Event()
executor.launcher.receipt_gate = asyncio.Event()
starting = asyncio.ensure_future(executor.start())
await executor.launcher.launch_entered.wait()
if after_launch:
executor.launcher.launch_gate.set()
await asyncio.wait_for(executor.launcher.process_spawned.wait(), timeout=5)
starting.cancel()
with contextlib.suppress(asyncio.CancelledError):
await starting
executor.launcher.launch_gate.set()
# In the postlaunch case, keep the host launch result blocked while native close reaps.
if not after_launch:
executor.launcher.receipt_gate.set()
await asyncio.wait_for(executor.aclose(), timeout=5)
await _assert_reaped(executor, method)
executor.launcher.receipt_gate.set()
@pytest.mark.parametrize("method", _METHODS)
async def test_blocked_python_health_kill(
method: str, executor_factory: Callable[[str, str], _Executor], tmp_path: Path
) -> None:
executor = executor_factory(method, "normal")
await executor.start()
await executor.initialize()
entered = tmp_path / "blocked-callback.entered"
job = running_job(agent.Job(id="blocked-python"))
job.accept_arguments.metadata = str(entered)
started = time.monotonic()
with pytest.raises((RuntimeError, ConnectionError)):
await executor.launch_job(job)
await _assert_reaped(executor, method)
assert entered.exists(), "the real host loop must enter the blocking callback"
# The configured 200 ms health deadline and bounded receipt wait
# must finish well before the intentionally blocked 60-second host callback.
assert time.monotonic() - started < 3
assert executor.killed
assert executor.exitcode != 0
@pytest.mark.parametrize("method", _METHODS)
async def test_launch_failure_keeps_its_cause_and_settles_close(
method: str,
executor_factory: Callable[[str, str], _Executor],
monkeypatch: pytest.MonkeyPatch,
) -> None:
executor = executor_factory(method, "normal")
# Multiprocessing must reject the unpicklable target before creating a child.
monkeypatch.setattr(
executor.launcher,
"_create_process",
lambda mp_ctx, endpoint: mp_ctx.Process(target=lambda: None),
)
# Python 3.14 raises PicklingError where earlier versions raise AttributeError.
with pytest.raises((AttributeError, pickle.PicklingError), match="local object"):
await asyncio.wait_for(executor.start(), timeout=5)
await asyncio.wait_for(executor.aclose(), timeout=3)
await asyncio.wait_for(executor.join(), timeout=3)
assert executor.pid is None
@pytest.mark.parametrize("method", _METHODS)
async def test_post_spawn_failure_keeps_native_owner_and_original_cause(
method: str,
executor_factory: Callable[[str, str], _Executor],
monkeypatch: pytest.MonkeyPatch,
caplog: pytest.LogCaptureFixture,
) -> None:
import importlib
suffix = (
"spawn_win32"
if os.name == "nt"
else "forkserver"
if method == "forkserver"
else "spawn_posix"
)
popen = importlib.import_module(f"multiprocessing.popen_{suffix}").Popen
initialize = popen.__init__
def fail_after_spawn(self: Any, process: Any) -> None:
initialize(self, process)
raise OSError("fixture bootstrap failed after OS launch")
monkeypatch.setattr(popen, "__init__", fail_after_spawn)
executor = executor_factory(method, "normal")
with caplog.at_level(logging.ERROR, logger="livekit.agents"):
# the process exists: the native owner took it, the launch's cause is logged
await asyncio.wait_for(executor.start(), timeout=5)
assert executor.pid is not None, "partial Popen failure must still publish native ownership"
async with asyncio.timeout(5):
while "fixture bootstrap failed after OS launch" not in caplog.text:
await asyncio.sleep(0.01)
await asyncio.wait_for(executor.aclose(), timeout=3)
await _assert_reaped(executor, method)
assert executor.exitcode != 0
@pytest.mark.parametrize("method", _METHODS)
async def test_ready_pool_processes_use_native_exit_and_reaping_handles(method: str) -> None:
async def launcher() -> _Launcher:
return _Launcher(method, "normal")
pool = _core.ProcPool(
launcher,
num_idle_processes=1,
options=JobRunnerOptions(
initialize_timeout=_PROCESS_INITIALIZE_TIMEOUT,
close_timeout=0.25,
ping_interval=0.05,
ping_timeout=0.2,
high_ping_threshold=0.1,
),
)
try:
await asyncio.wait_for(pool.start(), timeout=_PROCESS_INITIALIZE_TIMEOUT + 5)
executor = pool.processes[0]
pid = executor.pid
assert pid is not None
_kill_pid(pid)
# Native ownership/eligibility keeps progressing while host callbacks cannot run.
time.sleep(1)
await _assert_reaped(executor, method)
def replacements() -> list[_core.JobSupervisor]:
return [p for p in pool.processes if p.id != executor.id and p.initialized]
async def replacement_ready() -> None:
while not replacements():
await asyncio.sleep(0.01)
await asyncio.wait_for(replacement_ready(), timeout=_PROCESS_INITIALIZE_TIMEOUT + 5)
assert executor.id not in {process.id for process in pool.processes}
assert any(replacement.pid != pid for replacement in replacements())
finally:
await asyncio.wait_for(pool.aclose(), timeout=5)
@pytest.mark.parametrize("method", _METHODS)
@pytest.mark.parametrize(
"fault", ["wire_malformed", "wire_truncated", "wire_oversized_prefix", "wire_eof"]
)
async def test_real_ipc_protocol_failure_vectors(
method: str, fault: str, executor_factory: Callable[[str, str], _Executor]
) -> None:
executor = executor_factory(method, fault)
await executor.start()
with pytest.raises((RuntimeError, ConnectionError)):
await asyncio.wait_for(
executor.initialize(), timeout=_PROCESS_INITIALIZE_TIMEOUT + _PROCESS_CLOSE_TIMEOUT
)
await _assert_reaped(executor, method)
assert executor.status is None, "no job ran"
assert executor.launcher.completion is not None
failure = executor.launcher.completion.failure
if fault == "wire_oversized_prefix":
assert isinstance(failure, IpcFailureTooLarge)
assert failure.size == 2**32 - 1
elif fault == "wire_malformed":
assert isinstance(failure, IpcFailureMalformed)
else:
assert isinstance(failure, IpcFailureIo)
assert failure.kind == "UnexpectedEof"
# The peer deliberately returned zero after sending malformed/truncated data.
if fault != "wire_oversized_prefix":
assert executor.exitcode == 0
@pytest.mark.parametrize("method", _METHODS)
async def test_real_ipc_stress_matrix(method: str, tmp_path: Path) -> None:
from tests.conformance.ipc_processes import close_owned_executor, process_snapshot
parent = psutil.Process()
retained: list[weakref.ReferenceType[_Launcher]] = []
async def cycle(index: int) -> None:
executor = _Executor(method, "real_job")
retained.append(weakref.ref(executor.launcher))
started_at = time.monotonic()
phases: dict[str, float] = {}
try:
await executor.start()
phases["launch"] = time.monotonic() - started_at
await executor.initialize()
phases["ready"] = time.monotonic() - started_at
info = running_job(agent.Job(id=f"stress-{index}", metadata=f"wire-stress-{index}"))
await executor.launch_job(info)
phases["job_accepted"] = time.monotonic() - started_at
await _assert_reaped(executor, method)
assert executor.launcher.completion is not None
assert executor.launcher.completion.failure is None
assert executor.exitcode == 0
finally:
await close_owned_executor(executor.sup, tmp_path / "ipc-cleanup")
phases["closed"] = time.monotonic() - started_at
(tmp_path / f"ipc-{method}-{index}-phases.json").write_text(
json.dumps(phases, indent=2)
)
async def batch(index: int) -> None:
async with asyncio.TaskGroup() as jobs:
for offset in range(_CONCURRENT_JOBS):
jobs.create_task(cycle(index * _CONCURRENT_JOBS + offset))
deadline = time.monotonic() + 5
while True:
await asyncio.sleep(0)
gc.collect()
if not any(reference() is not None for reference in retained):
break
assert time.monotonic() < deadline, "settled jobs retained their host references"
await asyncio.sleep(0.01)
# Include the existing executor's declared cache capacity, not only today's idle occupancy.
import threading
loop = asyncio.get_running_loop()
await loop.run_in_executor(None, lambda: None)
executor = loop._default_executor
assert isinstance(executor, ThreadPoolExecutor)
executor_capacity = executor._max_workers
launch_workers = threading.Barrier(executor_capacity)
await asyncio.gather(
*[
loop.run_in_executor(None, launch_workers.wait, _PROCESS_INITIALIZE_TIMEOUT)
for _ in range(executor_capacity)
]
)
await batch(0)
await asyncio.sleep(_NATIVE_CACHE_IDLE_TIMEOUT)
baseline = process_snapshot(parent)
baseline_children = {child.pid for child in parent.children(recursive=True)}
baseline_tasks = len(asyncio.all_tasks())
observations: list[dict[str, Any]] = []
batches = int(os.environ.get("LIVEKIT_IPC_SOAK_BATCHES", "3"))
assert batches > 0
for index in range(1, batches + 1):
await batch(index)
peak_after_close = process_snapshot(parent)
settling_started = time.monotonic()
deadline = settling_started + _PROCESS_CLOSE_TIMEOUT + _NATIVE_CACHE_IDLE_TIMEOUT
while parent.num_threads() > baseline["threads"] and time.monotonic() < deadline:
await asyncio.sleep(0.01)
current = process_snapshot(parent)
children = {child.pid for child in parent.children(recursive=True)}
observed = {
"batch": index,
"resources": current,
"peak_after_close": peak_after_close,
"cache_settlement_seconds": time.monotonic() - settling_started,
"children": sorted(children),
"tasks": len(asyncio.all_tasks()),
"host_references": sum(ref() is not None for ref in retained),
}
observations.append(observed)
artifact = tmp_path / f"ipc-stress-{method}.json"
artifact.write_text(
json.dumps(
{
"method": method,
"concurrent_jobs": _CONCURRENT_JOBS,
"cache_idle_seconds": _NATIVE_CACHE_IDLE_TIMEOUT,
"python_executor_capacity": executor_capacity,
"baseline": baseline,
"batches": observations,
},
indent=2,
)
)
assert not any(ref() is not None for ref in retained), (
f"host references retained: {artifact}"
)
assert children == baseline_children, f"process/helper retention: {artifact}"
assert current["handles"] <= baseline["handles"], f"descriptor retention: {artifact}"
assert current["threads"] <= baseline["threads"], f"thread retention: {artifact}"
assert observed["tasks"] == baseline_tasks, f"async task retention: {artifact}"
async def test_ipc_fixture_watchdog_failure(
own_executor: Callable[[Any], Any],
tmp_path: Path,
) -> None:
if os.environ.get("LIVEKIT_IPC_WATCHDOG_CHILD") == "1":
class StuckCloseExecutor(_Executor):
async def aclose(self) -> None:
await asyncio.sleep(60)
executor = StuckCloseExecutor("spawn", "watchdog")
own_executor(executor)
await executor.start()
await executor.initialize()
assert executor.pid is not None
assert psutil.Process(executor.pid).is_running()
return # teardown must fail, capture evidence, kill, and reap the real child
from tests.conformance.ipc_processes import run_owned_process
artifact_dir = tmp_path / "watchdog-child"
result = await asyncio.to_thread(
run_owned_process,
[
sys.executable,
"-m",
"pytest",
str(Path(__file__).resolve()) + "::test_ipc_fixture_watchdog_failure",
"--e2e",
"--basetemp",
str(artifact_dir),
"-q",
],
tmp_path / "watchdog-run",
{**os.environ, "LIVEKIT_IPC_WATCHDOG_CHILD": "1"},
30,
)
assert result.returncode != 0, result.stdout + (result.stderr or "")
assert "missed cleanup deadline" in result.stdout + (result.stderr or "")
artifacts = list(artifact_dir.rglob("executor-*.json"))
assert artifacts, result.stdout + (result.stderr or "")
for artifact in artifacts:
evidence = json.loads(artifact.read_text())
assert evidence["failure"] is not None
assert evidence["before"], "capture the owned tree before killing it"
assert evidence["after"] == [], "emergency cleanup must reap rather than accept zombies"
def test_process_watchdog_preserves_failure_after_cleaning_the_owned_tree(tmp_path: Path) -> None:
import subprocess
from tests.conformance.ipc_processes import run_owned_process
result = run_owned_process(
[sys.executable, "-c", "raise SystemExit(7)"],
tmp_path / "nonzero",
dict(os.environ),
5,
)
assert result.returncode == 7
script = (
"import subprocess,sys,time; "
"subprocess.Popen([sys.executable,'-c','import time; time.sleep(60)']); "
"time.sleep(60)"
)
artifact_dir = tmp_path / "timed-out"
with pytest.raises(AssertionError, match="watchdog failed") as failed:
run_owned_process([sys.executable, "-c", script], artifact_dir, dict(os.environ), 0.25)
assert isinstance(failed.value.__cause__, subprocess.TimeoutExpired)
evidence = json.loads((artifact_dir / "processes.json").read_text())
assert len(evidence["before"]) == 2, "capture both the parent and its inherited child"
assert evidence["after"] == [], "cleanup must not accept zombies as reaped"