Skip to content

Conversation

@Kick-snare
Copy link

Description

Fixes #7648

This PR adds support for the goAway server message type in LiveSession to prevent crashes when the server initiates a disconnect. Previously, when the server sent a goAway message, the app would crash with a SerializationException because the message type was not recognized by LiveServerMessageSerializer.

Changes

1. Add LiveServerGoAway message type

  • Added LiveServerGoAway class to represent server-initiated disconnection
  • Includes timeLeft: String? field containing duration in protobuf format (e.g., "57s", "1.5s")
  • Added parseTimeLeft() helper method to convert timeLeft string to kotlin.time.Duration
  • Registered goAway message in LiveServerMessageSerializer

2. Robust Handling & API Update

  • Crash Prevention: Added CoroutineExceptionHandler to networkScope and audioScope to act as a safety net against unhandled exceptions.
  • Graceful Shutdown: Implemented logic in processModelResponses() to recognize GoAway signals and automatically close the session, preventing zombie connections or further crashes.
  • Public API: Added goAwayHandler to LiveAudioConversationConfig.
    • Rationale: While internal handling prevents the crash, apps often need to inform the user that the session ended due to server maintenance or timeouts. This handler exposes the timeLeft information to the application layer.

3. Comprehensive unit tests

  • Created LiveServerMessageTests.kt with 18 test cases covering:
    • Duration parsing (integer seconds, fractional seconds, edge cases)
    • LiveServerGoAway deserialization (with/without timeLeft)
    • Polymorphic serializer recognition
  • Added serialization schema test in SerializationTests.kt
  • All 164 tests pass

Usage Example

Apps can now handle server disconnections gracefully using the goAwayHandler:

val config = LiveAudioConversationConfig.Builder()
  .setGoAwayHandler { goAway ->
    val timeLeft = goAway.parseTimeLeft()
    Log.i(TAG, "Server disconnecting in ${timeLeft?.inWholeSeconds} seconds")
    // Clean up resources, show UI notification, etc.
  }
  .build()

liveGenerativeModel.startAudioConversation(config)

Testing

  • ✅ All 164 unit tests pass
  • ✅ Tested with real app and confirmed graceful handling of goAway messages (Published to local Maven)
  • ✅ Spotless formatting applied

Related Issues

Servers send goAway messages to gracefully disconnect sessions.
The timeLeft field uses protobuf Duration string format.
Add exception handling and goAwayHandler support:
- Add CoroutineExceptionHandler to prevent crashes
- Handle LiveServerGoAway in processModelResponses
- Add goAwayHandler to LiveAudioConversationConfig
Add unit tests for LiveServerGoAway message handling:
- Duration parsing tests (protobuf format)
- Deserialization tests
- Serialization schema test
@gemini-code-assist
Copy link
Contributor

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

@google-cla
Copy link

google-cla bot commented Jan 16, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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.

SerializationException on "goAway" message in LiveSession

1 participant