Skip to content

dataconnect(fix): Refresh expired Auth and App Check tokens in realtime query subscriptions#8346

Merged
dconeybe merged 4 commits into
mainfrom
dconeybe/dataconnect/RealtimeAuthUid7
Jun 23, 2026
Merged

dataconnect(fix): Refresh expired Auth and App Check tokens in realtime query subscriptions#8346
dconeybe merged 4 commits into
mainfrom
dconeybe/dataconnect/RealtimeAuthUid7

Conversation

@dconeybe

@dconeybe dconeybe commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Fixes an issue in data connect where expired Firebase Auth and App Check tokens were not automatically refreshed and retried when rejected with an UNAUTHENTICATED error by the server during realtime query subscriptions.

Highlights

  • Automatic Token Refresh: Introduced ConnectionTokenManager to coordinate the retrieval, force-refreshing, and user-change validation of Auth and App Check tokens during bidirectional stream lifecycles.
  • Stream Retry Strategy: Refactored the connection retry logic in DataConnectBidiConnectStream to accept a dynamic shouldRetry callback, enabling seamless immediate retries upon token refreshes and proper permanent failures when credentials cannot be updated.
  • Robust Integration Testing: Added new integration tests to verify App Check token validation, failure on placeholder tokens, and automatic refresh/reconnection for realtime query subscriptions.
  • Changelog Entry: Documented the fix in CHANGELOG.md.

Changelog

View Affected Files
  • CHANGELOG.md: Added entry under fixed section documenting the automatic token refresh fix.
  • AppCheckIntegrationTest.kt: Added new integration tests and helper utilities to verify App Check token validation, placeholder failure, and automatic refresh for realtime subscriptions.
  • RealtimeConnector.kt: Added helper methods (execute and mutationRef) to simplify query/mutation execution in tests.
  • DataConnectBidiConnectStream.kt: Refactored connection flow to use a dynamic shouldRetry callback and a simplified RetryStrategy enum, removing custom exception wrappers.
  • DataConnectCredentialsTokenManager.kt: Clarified the KDoc description of the token flow.
  • DataConnectGrpcRPCs.kt: Implemented ConnectionTokenManager to manage token lifecycles and provided the stream retry callback implementing the token refresh strategy.
  • RealtimeQueryManager.kt: Updated reference to use the renamed isPermanentlyFailed stream property.
  • QuerySubscriptionImplUnitTest.kt: Adjusted unit tests to exclude UNAUTHENTICATED from generic retryable codes, reflecting its new specialized refresh logic.

@gemini-code-assist

Copy link
Copy Markdown
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
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the 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 counterproductive. 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.

@dconeybe

Copy link
Copy Markdown
Contributor Author

/gemini review

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

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.

Code Review

This pull request addresses an issue where expired Auth and App Check tokens were not automatically refreshed upon server rejection. It introduces a ConnectionTokenManager to handle token lifecycle and updates DataConnectBidiConnectStream to support immediate retries on UNAUTHENTICATED errors. Additionally, several integration tests are updated and expanded to cover these scenarios. The reviewer feedback highlights three key improvements: marking shared mutable properties in ConnectionTokenManager as @Volatile to ensure thread safety, explicitly rethrowing CancellationException to prevent incorrect permanent failure states, and avoiding Duration.INFINITE in test timeouts to prevent CI/CD deadlocks.

@github-actions

github-actions Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

📝 PRs merging into main branch

Our main branch should always be in a releasable state. If you are working on a larger change, or if you don't want this change to see the light of the day just yet, consider using a feature branch first, and only merge into the main branch when the code complete and ready to be released.

@dconeybe dconeybe marked this pull request as ready for review June 23, 2026 00:13
@dconeybe

Copy link
Copy Markdown
Contributor Author

NOTE: The "Metalava SemVer Check" github actions failure is unrelated, and is fixed upstream by #8345

@dconeybe dconeybe merged commit d97ea6a into main Jun 23, 2026
50 checks passed
@dconeybe dconeybe deleted the dconeybe/dataconnect/RealtimeAuthUid7 branch June 23, 2026 04:29
@github-actions github-actions Bot mentioned this pull request Jun 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants