Memory leak in Google STT when no audio input exists

I was seeing a strange memory increase during the agent is idle – there were webRTC sessions but no audio input, not actively used (i.e. the client opens up a webrtc session with text-chat only, stays the window open forever). Eventually I got alert logs for the memory use for the agent process.

Here is the graph of memory increase at that time:

I fortunately succeeded to reproduce the memory increase on my local machine, and checked different code paths and configurations to track down where the source of the memory leak, then found out that the memory leak is due to Google STT in livekit SDK/plugin. If I disabled STT, or switched to another one (I tried Elevenlabs), the memory increase is just gone.

Seemingly it is due to lack of the cleanup of the resources for the STT session timeout/recreation, this is also reported as Google STT has a memory leak when an agent session is running with no audio input from the user · Issue #5590 · livekit/agents · GitHub and a PR was made as Fix the memory leak of Google STT when no audio input. by jmuk · Pull Request #5591 · livekit/agents · GitHub. Can anyone take a look at this?

What version are you using? pip freeze |grep livekit

It’s 1.5.4 basically

$ pip freeze | grep livekit
livekit==1.1.5
livekit-agents==1.5.4
livekit-api==1.1.0
livekit-blingfire==1.1.0
livekit-plugins-cambai==1.5.4
livekit-plugins-cartesia==1.5.4
livekit-plugins-deepgram==1.5.4
livekit-plugins-elevenlabs==1.5.4
livekit-plugins-google==1.5.4
livekit-plugins-mistralai==1.5.4
livekit-plugins-noise-cancellation==0.2.5
livekit-plugins-openai==1.5.4
livekit-plugins-silero==1.5.4
livekit-plugins-turn-detector==1.5.4
livekit-protocol==1.1.5

Can someone also check the github issue and the PR I created (linked in the original post)? Thanks!

I will highlight it to the team.