fix(agents): bound AgentSession close cleanup#5602
Open
shizhigu wants to merge 2 commits intolivekit:mainfrom
Open
fix(agents): bound AgentSession close cleanup#5602shizhigu wants to merge 2 commits intolivekit:mainfrom
shizhigu wants to merge 2 commits intolivekit:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This bounds
AgentSessionshutdown cleanup so a stuck close step does not prevent the session from emittingCloseEvent.The motivating case is #5497: a participant can disconnect while the agent is still speaking, then cleanup can hang before downstream shutdown callbacks ever run. This PR does not try to prove the exact native/audio playout root cause. It makes the close path defensive: once session close starts, cleanup is best effort, but the close event still gets emitted within a bounded deadline.
Fixes #5497.
Invariant
aclose()emitsCloseEventwithin_AGENT_SESSION_CLOSE_TIMEOUTregardless of which cleanup step blocks. Steps that still need to release resources after the shared deadline are started in the background with their own bounded timeout and logged failures.What changed
AgentSessioncleanup.current_speechis awaited throughasyncio.shield(...)so the close timeout does not cancel the speech future beforeactivity.aclose()owns speech teardown. Late-result callbacks avoid retaining theAgentSessioninstance after close.Testing
uv run pytest tests/test_agent_session_close.py9 passeduv run ruff check livekit-agents/livekit/agents/voice/agent_session.py tests/test_agent_session_close.pymake checkPYTHONPATH="$PWD" uv run pytest tests/test_agent_session.py tests/test_agent_session_close.py -q27 passedPYTHONPATH="$PWD" uv run pytest tests/test_ipc.py -q8 passedmake unit-tests:618 passed, 2 skipped;tests/test_room.pysetup failed locally because this machine does not have thelivekit-serverbinary on PATH.