Skip to content

Fix the memory leak of Google STT when no audio input.#5591

Merged
longcw merged 4 commits intolivekit:mainfrom
jmuk:google-stt-memory-leak-fix
May 7, 2026
Merged

Fix the memory leak of Google STT when no audio input.#5591
longcw merged 4 commits intolivekit:mainfrom
jmuk:google-stt-memory-leak-fix

Conversation

@jmuk
Copy link
Copy Markdown
Contributor

@jmuk jmuk commented Apr 28, 2026

When the RTC session is idle without any audio input, Google STT re-create the client object and re-connect periodically. However some old resources actually remain as they're not cancelled properly, causing memory leak.

This PR adds the clean-up of such scenarios. With this change, I confirmed my experience of the memory leak is gone.

Fixes #5590

When the RTC session is idle without any audio input, Google STT
re-create the client object and re-connect periodically. However
some old resources actually remain as they're not cancelled
properly, causing memory leak.

This PR adds the clean-up of such scenarios.  With this change,
I confirmed my experience of the memory leak is gone.

Fixes livekit#5590
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 28, 2026

CLA assistant check
All committers have signed the CLA.

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Open in Devin Review

Copy link
Copy Markdown
Contributor

@longcw longcw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the pr! overall looks good to me, my only concern is about the await asyncio.sleep(5.0) after timeout

Comment thread livekit-plugins/livekit-plugins-google/livekit/plugins/google/stt.py Outdated
return
try:
frame = frame_task.result()
except StopAsyncIteration:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use ChanClosed if using the input_ch.recv on above

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

else:
# No audio has been pushed — back off so we don't tight-loop
# against Google's server-side 5-minute idle timeout.
await asyncio.sleep(5.0)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we await sleep here? what if user audio comes in during the sleep?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, didn't think much about such cases; removed.

jmuk and others added 2 commits May 6, 2026 11:57
Co-authored-by: Long Chen <longch1024@gmail.com>
devin-ai-integration[bot]

This comment was marked as resolved.

Copy link
Copy Markdown
Contributor Author

@jmuk jmuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the reviews!

else:
# No audio has been pushed — back off so we don't tight-loop
# against Google's server-side 5-minute idle timeout.
await asyncio.sleep(5.0)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, didn't think much about such cases; removed.

return
try:
frame = frame_task.result()
except StopAsyncIteration:
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 new potential issue.

View 6 additional findings in Devin Review.

Open in Devin Review

@longcw longcw merged commit ac03947 into livekit:main May 7, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Google STT has a memory leak when an agent session is running with no audio input from the user

4 participants