-
Notifications
You must be signed in to change notification settings - Fork 3.1k
fix(voice): wait_for_playout resolves on interrupt instead of deadlocking #5700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MacFall7
wants to merge
1
commit into
livekit:main
Choose a base branch
from
MacFall7:fix/5359-wait-for-playout-interruption
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| """Regression tests for SpeechHandle / RunContext playout-wait interruption. | ||
|
|
||
| Guards against the deadlock described in livekit/agents#5359, where | ||
| ``SpeechHandle.wait_for_playout()`` and ``RunContext.wait_for_playout()`` | ||
| awaited only on the playout-completion future and ignored the interrupt | ||
| future. When ``interrupt()`` fired, callers blocked until the | ||
| ``INTERRUPTION_TIMEOUT`` (~5s) hard-killed the surrounding tasks. | ||
|
|
||
| Both methods now race the playout future against ``_interrupt_fut`` using | ||
| ``asyncio.wait(FIRST_COMPLETED)`` (the same primitive already used by | ||
| ``SpeechHandle.wait_if_not_interrupted``). The wait returns promptly on | ||
| interrupt; callers can inspect ``speech_handle.interrupted`` to decide how | ||
| to proceed. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| from unittest.mock import MagicMock | ||
|
|
||
| from livekit.agents.llm import FunctionCall | ||
| from livekit.agents.voice.events import RunContext | ||
| from livekit.agents.voice.speech_handle import SpeechHandle | ||
|
|
||
|
|
||
| async def test_speech_handle_wait_for_playout_returns_on_interrupt() -> None: | ||
| """wait_for_playout must unblock when interrupted, not hang on _done_fut. | ||
|
|
||
| Regression test for https://github.com/livekit/agents/issues/5359. | ||
| Pre-fix this hung on ``_done_fut`` for INTERRUPTION_TIMEOUT (~5s); the | ||
| 1.0s deadline below would fire and the test would raise TimeoutError. | ||
| """ | ||
| sh = SpeechHandle.create() | ||
|
|
||
| async def _interrupt_after_delay() -> None: | ||
| await asyncio.sleep(0.05) | ||
| sh._cancel() | ||
|
|
||
| interrupt_task = asyncio.create_task(_interrupt_after_delay()) | ||
| try: | ||
| await asyncio.wait_for(sh.wait_for_playout(), timeout=1.0) | ||
| finally: | ||
| await interrupt_task | ||
|
|
||
| assert sh.interrupted | ||
| assert not sh.done() | ||
|
|
||
|
|
||
| async def test_run_context_wait_for_playout_returns_on_interrupt() -> None: | ||
| """RunContext.wait_for_playout must unblock when the speech is interrupted. | ||
|
|
||
| Regression test for https://github.com/livekit/agents/issues/5359. | ||
| """ | ||
| sh = SpeechHandle.create() | ||
| sh._authorize_generation() | ||
|
|
||
| fc = FunctionCall(call_id="call_test", arguments="{}", name="noop") | ||
| ctx = RunContext(session=MagicMock(), speech_handle=sh, function_call=fc) | ||
|
|
||
| async def _interrupt_after_delay() -> None: | ||
| await asyncio.sleep(0.05) | ||
| sh._cancel() | ||
|
|
||
| interrupt_task = asyncio.create_task(_interrupt_after_delay()) | ||
| try: | ||
| await asyncio.wait_for(ctx.wait_for_playout(), timeout=1.0) | ||
| finally: | ||
| await interrupt_task | ||
| sh._mark_done() | ||
|
|
||
| assert sh.interrupted | ||
|
|
||
|
|
||
| async def test_speech_handle_wait_for_playout_returns_normally_on_completion() -> None: | ||
| """Sanity check: a non-interrupted playout still resolves on _done_fut.""" | ||
| sh = SpeechHandle.create() | ||
|
|
||
| async def _complete_after_delay() -> None: | ||
| await asyncio.sleep(0.05) | ||
| sh._mark_done() | ||
|
|
||
| completion_task = asyncio.create_task(_complete_after_delay()) | ||
| try: | ||
| await asyncio.wait_for(sh.wait_for_playout(), timeout=1.0) | ||
| finally: | ||
| await completion_task | ||
|
|
||
| assert not sh.interrupted | ||
| assert sh.done() |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we cannot resolve the
done_futon interrupt because the audio output may not stopped when interrupt is set, especially for avatar use case. there is already atimeoutin _cancel that will resolve the done after a timeout and cancel the tasks arbitrarily.