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.

Copy link
Collaborator

@emilypgoogle emilypgoogle left a comment

Choose a reason for hiding this comment

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

Hi! Thanks for the PR, I've left some comments about changes we'd like to see and open questions for the contents and implementation choices made. Overall, once these are addressed, this general implementation seems well done to merge.

* convert this to a [kotlin.time.Duration].
*/
@PublicPreviewAPI
public class LiveServerGoAway(public val timeLeft: String?) : LiveServerMessage {
Copy link
Collaborator

Choose a reason for hiding this comment

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

The SDK should convert this string into a public facing duration object and not expose the String.

*
* @return The parsed duration, or null if [timeLeft] is null or cannot be parsed.
*/
public fun parseTimeLeft(): kotlin.time.Duration? {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Re the previous comment, this method should be removed and called implicitly on toPublic


// Remove 's' suffix and parse as double
val secondsStr = trimmed.dropLast(1)
val seconds = secondsStr.toDoubleOrNull() ?: return null
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could be rewritten as
secondsStr.toDoubleOrNull()?.seconds removing the lines below

val seconds = secondsStr.toDoubleOrNull() ?: return null

seconds.seconds
} catch (e: Exception) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

What could be throwing an exception here? Is there a more scoped type possible, or is this vestigial?

*
* Logs the exception and attempts to clean up resources to prevent app crashes.
*/
private val exceptionHandler = CoroutineExceptionHandler { _, throwable ->
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this necessary for this PR? Ideally PRs should be well scoped.

)
} catch (e: SerializationException) {
Log.w(TAG, "Failed to deserialize server message: ${e.message}")
null // Skip unknown messages instead of crashing
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a notable behavior change, is avoiding crashes and skipping server messages actually the ideal behavior?

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

2 participants