-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(simplismart): close the streams the STT collects #7012
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
Merged
tinalenguyen
merged 1 commit into
livekit:main
from
Rehansanjay:fix/stt-aclose-tracked-streams
Aug 31, 2026
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| import gc | ||
|
|
||
| import pytest | ||
|
|
||
| from livekit.plugins.simplismart import stt as simplismart_stt | ||
|
|
||
| pytestmark = pytest.mark.unit | ||
|
|
||
|
|
||
| async def _idle_run(self: object) -> None: | ||
| del self | ||
| await asyncio.Event().wait() # cancelled by aclose() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_simplismart_stt_aclose_closes_tracked_stream_sessions( | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| ) -> None: | ||
| monkeypatch.setattr(simplismart_stt.SpeechStream, "_run", _idle_run) | ||
|
|
||
| stt = simplismart_stt.STT(api_key="sk_test") | ||
| stream_a = stt.stream() | ||
| stream_b = stt.stream() | ||
| sessions = [stream_a._session, stream_b._session] | ||
|
|
||
| assert all(not s.closed for s in sessions) | ||
| assert len(stt._streams) == 2 | ||
|
|
||
| await stt.aclose() | ||
|
|
||
| assert all(s.closed for s in sessions), ( | ||
| "STT.aclose() must close every per-stream aiohttp session" | ||
| ) | ||
| assert len(stt._streams) == 0 | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_simplismart_stt_async_context_closes_stream_sessions( | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| ) -> None: | ||
| monkeypatch.setattr(simplismart_stt.SpeechStream, "_run", _idle_run) | ||
|
|
||
| async with simplismart_stt.STT(api_key="sk_test") as stt: | ||
| stream = stt.stream() | ||
| session = stream._session | ||
| assert not session.closed | ||
|
|
||
| assert session.closed, "exiting `async with` must close per-stream sessions" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_simplismart_stt_aclose_tolerates_already_closed_streams( | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| ) -> None: | ||
| monkeypatch.setattr(simplismart_stt.SpeechStream, "_run", _idle_run) | ||
|
|
||
| stt = simplismart_stt.STT(api_key="sk_test") | ||
| stream = stt.stream() | ||
| session = stream._session | ||
|
|
||
| await stream.aclose() | ||
| await stt.aclose() # must not raise on an already-closed stream | ||
|
|
||
| assert session.closed | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_simplismart_stt_aclose_closes_session_of_dropped_stream( | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| ) -> None: | ||
| """A finished stream the caller dropped must not take its session to the GC. | ||
|
|
||
| With weak tracking, a completed SpeechStream can be collected before | ||
| STT.aclose() snapshots the set, leaving its per-stream ClientSession | ||
| unreachable and unclosed. Strong ownership keeps it reachable until | ||
| aclose closes it. | ||
| """ | ||
|
|
||
| async def _immediate_run(self: object) -> None: | ||
| del self # return immediately: the stream task finishes on its own | ||
|
|
||
| monkeypatch.setattr(simplismart_stt.SpeechStream, "_run", _immediate_run) | ||
|
|
||
| stt = simplismart_stt.STT(api_key="sk_test") | ||
| stream = stt.stream() | ||
| session = stream._session | ||
|
|
||
| del stream | ||
| gc.collect() | ||
|
|
||
| assert len(stt._streams) == 1, "a dropped stream must stay tracked until aclose" | ||
|
|
||
| await stt.aclose() | ||
|
|
||
| assert session.closed, "aclose() must close the session of a dropped stream" | ||
| assert len(stt._streams) == 0 |
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.
🔴 Sarvam stream sessions still leak
The cleanup omits
STT.aclosefor Sarvam'sstream, which creates a client session per stream. Closing that recognizer leaves collected sessions open.Prompt for agents
Was this helpful? React with 👍 or 👎 to provide feedback.
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.
Sarvam is covered by #7058, which fixes the identical bug in that plugin with the same ownership model. This PR originally covered both and was rescoped to simplismart so the two do not overlap — see the discussion on #7058.
If #7058 does not land, I am happy to bring the sarvam half back here.