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
6 changes: 5 additions & 1 deletion livekit-agents/livekit/agents/ipc/job_proc_lazy_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from ..telemetry import loop_monitor, trace_types, tracer, utils as trace_utils
from ..telemetry.traces import _resource
from ..utils import aio, http_context, log_exceptions, shortuuid
from ..voice.events import CloseReason
from .channel import Message
from .inference_executor import InferenceExecutor
from .proc_client import _dump_stack_traces_impl, _ProcClient
Expand Down Expand Up @@ -440,7 +441,10 @@ async def _shutdown_job(

if session := self._job_ctx._primary_agent_session:
try:
await asyncio.wait_for(session.aclose(), timeout=_SESSION_ACLOSE_TIMEOUT)
await asyncio.wait_for(
session._aclose_impl(reason=CloseReason.JOB_SHUTDOWN),
timeout=_SESSION_ACLOSE_TIMEOUT,
)
except asyncio.TimeoutError:
logger.error(
"AgentSession.aclose() timed out after %.1fs; "
Expand Down
46 changes: 45 additions & 1 deletion tests/test_ipc.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from livekit.agents import JobContext, JobProcess, ipc, job, utils
from livekit.agents.ipc.log_queue import LogQueueHandler, LogQueueListener
from livekit.agents.utils.aio import duplex_unix
from livekit.agents.voice.events import CloseReason
from livekit.protocol import agent

pytestmark = [pytest.mark.unit, pytest.mark.concurrent]
Expand Down Expand Up @@ -211,7 +212,7 @@ async def _job_shutdown() -> None:
job_ctx.add_shutdown_callback(_job_shutdown)

class _HangingSession:
async def aclose(self) -> None:
async def _aclose_impl(self, *, reason: CloseReason) -> None:
await asyncio.Event().wait() # never resolves

job_ctx._primary_agent_session = _HangingSession() # type: ignore[assignment]
Expand Down Expand Up @@ -758,6 +759,49 @@ async def _job_entrypoint_records_loop(job_ctx: JobContext) -> None:
await _job_entrypoint(job_ctx)


_close_reasons: list[CloseReason] = []


async def _job_entrypoint_records_close_reason(job_ctx: JobContext) -> None:
start_args: _StartArgs = job_ctx.proc.user_arguments

class _Session:
async def aclose(self) -> None:
await self._aclose_impl(reason=CloseReason.USER_INITIATED)

async def _aclose_impl(self, *, reason: CloseReason) -> None:
_close_reasons.append(reason)

job_ctx._primary_agent_session = _Session() # type: ignore[assignment]

async def _noop_session_end() -> None:
return None

job_ctx._on_session_end = _noop_session_end # type: ignore[method-assign]

with start_args.entrypoint_counter.get_lock():
start_args.entrypoint_counter.value += 1

job_ctx.shutdown("close the session from the job shutdown")


async def test_thread_job_shutdown_closes_session_as_job_shutdown():
"""_shutdown_job must close the session with JOB_SHUTDOWN, not aclose()'s USER_INITIATED."""
_close_reasons.clear()
proc, start_args = _create_thread_proc(
close_timeout=10.0, job_entrypoint_fnc=_job_entrypoint_records_close_reason
)
await proc.start()
await proc.initialize()

await proc.launch_job(_generate_fake_job())
await _poll_until(lambda: start_args.entrypoint_counter.value >= 1)
await asyncio.wait_for(proc.aclose(), timeout=30.0)

assert proc.status == ipc.job_executor.JobStatus.SUCCESS
assert _close_reasons == [CloseReason.JOB_SHUTDOWN]


async def test_thread_job_loop_is_closed_after_the_runner_exits():
"""The thread runner owns its job loop, so it must close it the way asyncio.run() does.
An unclosed loop keeps its selector, self-pipe sockets and scheduled handles until the
Expand Down
Loading