Skip to content

fix(websocket-demo): resolve Firefox audio stream hang and prevent AudioContext leaks - #29

Merged
thorwebdev merged 2 commits into
google-gemini:mainfrom
regeter:fix/firefox-audio-lockup
Jul 7, 2026
Merged

thorwebdev merged 2 commits into
google-gemini:mainfrom
regeter:fix/firefox-audio-lockup

Conversation

@regeter

@regeter regeter commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

This PR fixes two issues in the gemini-live-ephemeral-tokens-websocket example:

  1. Firefox Text-only Hang: Toggling the microphone off ([Microphone off]) in Firefox caused the model to stop responding to subsequent text inputs. This occurred because abruptly closing the AudioContext while media tracks were active terminated the audio stream in an unclean state. The Gemini server's Voice Activity Detection (VAD) was left waiting for the stream to finish, keeping the turn open. Any subsequent text was appended to this open turn, preventing a response.
    • Fix: Refactored AudioStreamer to suspend/resume a single AudioContext instead of closing and recreating it, allowing the stream to pause cleanly.
  2. AudioContext Leak: Reconnecting leaked a 24kHz AudioContext from the AudioPlayer every time.
    • Fix: Properly destroy the AudioPlayer and close its context on disconnect.

Proof of the Issue (Unresponsive text after Mic Off)

As shown below, text messages sent after turning the microphone off received no response. Once the microphone was turned back on and activated, the server finally committed the turn and responded to the queued text query.

Firefox VAD Hang

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the audio handling in AudioStreamer and AudioPlayer to suspend and resume the AudioContext instead of closing and recreating it, and adds cleanup for the audio player on disconnect. The review feedback highlights several potential resource leaks and race conditions: (1) a concurrent initialization race condition in AudioPlayer that can leak contexts, (2) an AudioContext leak in AudioStreamer upon reconnection because suspended contexts are never closed, and (3) a potential leak if AudioStreamer.start() is called while already active, overwriting this.audioSource without disconnecting the previous stream.

@thorwebdev

Copy link
Copy Markdown
Collaborator

Ah nice one, thanks @regeter . Could you take a look at the comments from gemini-code-assist and incorporate suggestions? Thanks!

@regeter

regeter commented Jul 2, 2026

Copy link
Copy Markdown
Contributor Author

Done

@thorwebdev thorwebdev left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, thank you!

@thorwebdev
thorwebdev merged commit b0fcd19 into google-gemini:main Jul 7, 2026
7 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.

2 participants