Repository navigation
fix(stt): notice a half-open websocket in the openai, elevenlabs and inworld streams #7913
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
Rehansanjay
wants to merge
2
commits into
livekit:main
Choose a base branch
from
Rehansanjay:fix/stt-halfopen-heartbeat
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
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,84 @@ | ||
| """Unit tests for the Inworld STT websocket connection.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from types import SimpleNamespace | ||
| from typing import Any | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import aiohttp | ||
| import pytest | ||
|
|
||
| from livekit.agents import APIConnectionError | ||
|
|
||
| pytestmark = pytest.mark.unit | ||
|
|
||
|
|
||
| def _make_stream(): | ||
| """Construct an Inworld `SpeechStream` without spawning its real `_main_task`.""" | ||
| from livekit.agents.types import DEFAULT_API_CONNECT_OPTIONS | ||
| from livekit.plugins.inworld import STT | ||
| from livekit.plugins.inworld.stt import SpeechStream | ||
|
|
||
| def _fake_create_task(coro, *args, **kwargs): | ||
| coro.close() | ||
| return MagicMock() | ||
|
|
||
| stt_instance = STT(api_key="test-key") | ||
| with patch("livekit.agents.stt.stt.asyncio.create_task", side_effect=_fake_create_task): | ||
| return SpeechStream(stt=stt_instance, conn_options=DEFAULT_API_CONNECT_OPTIONS) | ||
|
|
||
|
|
||
| async def test_socket_is_opened_with_a_heartbeat() -> None: | ||
| """aiohttp defaults `heartbeat` to None, so a socket that dies without a | ||
| FIN/RST leaves the receive side parked forever: the stream never raises, and | ||
| neither the reconnect nor a FallbackAdapter switch is ever triggered.""" | ||
| stream = _make_stream() | ||
| captured: dict[str, Any] = {} | ||
|
|
||
| class _FakeWebSocket: | ||
| async def send_str(self, message: str) -> None: | ||
| pass | ||
|
|
||
| class _FakeSession: | ||
| async def ws_connect(self, url: str, **kwargs: Any) -> _FakeWebSocket: | ||
| captured.update(kwargs) | ||
| return _FakeWebSocket() | ||
|
|
||
| stream._ensure_session = lambda: _FakeSession() # type: ignore[method-assign] | ||
|
|
||
| await stream._connect_ws() | ||
|
|
||
| assert captured.get("heartbeat") == 30.0 | ||
|
|
||
|
|
||
| async def test_an_error_frame_raises_so_the_stream_can_fail_over() -> None: | ||
| """A heartbeat timeout arrives as an ERROR frame, not as a close frame. | ||
|
|
||
| Returning normally from it ended the stream silently: `_run` breaks out of its | ||
| loop instead of reconnecting, and `stt.SpeechStream` only reports a failure | ||
| when `_run` raises -- so a FallbackAdapter had nothing to react to and the | ||
| agent simply stopped hearing the user. | ||
| """ | ||
| stream = _make_stream() | ||
|
|
||
| class _ErrorWebSocket: | ||
| def __init__(self) -> None: | ||
| self._sent = False | ||
|
|
||
| def __aiter__(self) -> _ErrorWebSocket: | ||
| return self | ||
|
|
||
| async def __anext__(self) -> Any: | ||
| if self._sent: | ||
| raise StopAsyncIteration | ||
| self._sent = True | ||
| return SimpleNamespace(type=aiohttp.WSMsgType.ERROR, data=None, extra=None) | ||
|
|
||
| def exception(self) -> Exception: | ||
| return RuntimeError("no PONG received") | ||
|
|
||
| stream._ws = _ErrorWebSocket() # type: ignore[assignment] | ||
|
|
||
| with pytest.raises(APIConnectionError): | ||
| await stream._recv_messages_task() |
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
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.
Uh oh!
There was an error while loading. Please reload this page.