Skip to content

Firestore (Desktop): remove redundant IsSignedIn checks in GetToken() - #1994

Open
AustinBenoit wants to merge 3 commits into
mainfrom
fix/firestore-credentials-signout-race
Open

AustinBenoit wants to merge 3 commits into
mainfrom
fix/firestore-credentials-signout-race

Conversation

@AustinBenoit

@AustinBenoit AustinBenoit commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Description

FirebaseCppCredentialsProvider::GetToken() checked IsSignedIn() and then called RequestToken(), which hard-asserted IsSignedIn() a second time. Both checks ran under Firestore's contents_->mutex, but Auth::SignOut() mutates the signed-in user under Auth's own mutex. If SignOut() ran between the two checks, the assert aborted the process.

Why drop IsSignedIn() instead of locking across Auth and Firestore
Adding a shared lock between Auth and Firestore risks lock-order deadlocks (for example, Auth notifying listeners while Firestore calls into Auth). Checking IsSignedIn() before calling into Auth is also redundant: Auth::GetAuthTokenAsyncForRegistry() already checks Auth::FindAuth(app) and auth->current_user().is_valid() before allocating a Future, so signed-out calls still do a single registry hop and do not allocate.

Changes

  • Remove IsSignedIn() and the redundant SIMPLE_HARD_ASSERT(IsSignedIn(), ...) in RequestToken().
  • Have GetAuthTokenAsync() return a default-constructed invalid Future<std::string>() (kFutureStatusInvalid) instead of SuccessfulFuture<std::string>("") when Auth is unavailable or signed out.
  • In RequestToken(), check future.status() == kFutureStatusInvalid and invoke listener(AuthToken::Unauthenticated()), matching RequestToken()'s documented contract.
  • Read and clear force_refresh_ atomically with force_refresh_.exchange(false).

Why concurrent Auth::SignOut() is now safe at every step

  • Before Auth allocates a future, or if User::GetTokenInternal() releases it mid-call (user_desktop.cc): GetAuthTokenAsync() returns an invalid Future, and RequestToken() immediately returns AuthToken::Unauthenticated(). (Previously, a released future silently dropped the OnCompletion() callback and left Firestore waiting.)
  • During ValidateCurrentUser(): Auth returns a completed failed Future with kAuthErrorNoSignedInUser, which ConvertToken() maps to kErrorUnauthenticated.
  • While an async refresh is in flight: OnAuthStateChanged() increments token_generation under contents_->mutex, so OnToken() rejects the stale result with kErrorAborted and Firestore retries cleanly.

Testing

  • Compiled credentials_provider_desktop.cc and create_credentials_provider_desktop.cc with clang++-19 -Wall -Wextra (0 warnings in touched files).
  • Passed clang-format --style=file --dry-run --Werror and ./scripts/gha/check_copyright.sh -g.
  • Relying on CI for cross-platform builds and Firestore integration tests.

Type of Change

FirebaseCppCredentialsProvider::GetToken() checked IsSignedIn() and then
called RequestToken(), which hard-asserted IsSignedIn() a second time
before calling FnAuthGetTokenAsync (which itself checks
`auth->current_user().is_valid()`). Because Auth::SignOut() clears the user
without holding `contents_->mutex`, a concurrent sign-out landing between
the two checks triggered SIMPLE_HARD_ASSERT and aborted the process.

Inline RequestToken() into GetToken(), remove the redundant IsSignedIn()
pre-checks, and have GetAuthTokenAsync() return an invalid Future
(`kFutureStatusInvalid`) when FnAuthGetTokenAsync returns false (Auth
unavailable or no signed-in user). Checking `kFutureStatusInvalid` in
GetToken() handles both unauthenticated calls and an invalid Future from
Auth without allocating a dummy SuccessfulFuture or letting OnCompletion()
silently no-op, and returns `AuthToken::Unauthenticated()` immediately.
Also guard ConvertToken() so a sign-out racing completion returns
`AuthToken::Unauthenticated()` instead of a token with an unauthenticated
user, and consume `force_refresh_` atomically via `exchange(false)`.

References:
- Previous `RequestToken()` contract (`credentials_provider_desktop.h:71-77`):
  https://github.com/firebase/firebase-cpp-sdk/blob/70aa1bc0e0e088f274c3198c4f1d356091e237cf/firestore/src/main/credentials_provider_desktop.h#L71-L77
- `Auth::GetAuthTokenAsyncForRegistry()` (`auth_desktop.cc:183-206`):
  https://github.com/firebase/firebase-cpp-sdk/blob/70aa1bc0e0e088f274c3198c4f1d356091e237cf/auth/src/desktop/auth_desktop.cc#L183-L206

Fixes #1974

@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 fixes an assertion abort on Firestore Desktop that occurs when Auth::SignOut() races with a Firestore token request. It refactors FirebaseCppCredentialsProvider by removing the IsSignedIn() check and RequestToken() helper, and instead directly calling GetAuthTokenAsync(). If the returned future is invalid, it immediately returns an unauthenticated token; otherwise, it registers a completion callback to handle the token asynchronously. Additionally, ConvertToken is updated to return AuthToken::Unauthenticated() if the current user is not authenticated. There are no review comments, so I have no feedback to provide.

@AustinBenoit AustinBenoit added the tests-requested: full Trigger a FULL set of integration tests (uses expanded test matrix). label Nov 27, 2026
@github-actions github-actions Bot added tests: in-progress This PR's integration tests are in progress. and removed tests-requested: full Trigger a FULL set of integration tests (uses expanded test matrix). labels Nov 27, 2026
@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

❌  Integration test FAILED

Requested by @AustinBenoit on commit fdb9028
Last updated: Fri Nov 27 10:14 PST 2026
View integration test log & download artifacts

Failures Configs
app_check [TEST] [FAILURE] [iOS] [macos] [1/2 ios_device: simulator_target]
(1 failed tests)  FirebaseAppCheckTest.TestSignIn
firestore [TEST] [FLAKINESS] [Android] [1/3 os: ubuntu] [1/4 android_device: emulator_ftl_latest]
(1 failed tests)  TaskTest.IsCompleteShouldReturnTrueForCanceledTask
installations [TEST] [FLAKINESS] [Android] [1/3 os: ubuntu] [1/4 android_device: android_latest]
(1 failed tests)  CRASH/TIMEOUT
messaging [TEST] [FLAKINESS] [Android] [1/3 os: windows] [1/4 android_device: android_latest]
(1 failed tests)  CRASH/TIMEOUT
remote_config [TEST] [FLAKINESS] [Android] [1/3 os: windows] [1/4 android_device: android_latest]
(1 failed tests)  FirebaseRemoteConfigTest.TestAddOnConfigUpdateListener

Add flaky tests to go/fpl-cpp-flake-tracker

@github-actions github-actions Bot added the tests: failed This PR's integration tests failed. label Nov 27, 2026
@firebase-workflow-trigger firebase-workflow-trigger Bot removed the tests: in-progress This PR's integration tests are in progress. label Nov 27, 2026
Treat an empty UID from FnAuthGetCurrentUserUid as unauthenticated so a
concurrent Auth::SignOut() between Auth's is_valid() and uid() reads cannot
trigger HARD_ASSERT(!uid_.empty()) in User(uid). Also update the
Contents::mutex comment in credentials_provider_desktop.h.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tests: failed This PR's integration tests failed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Firestore aborts on "Cannot get token when there is no signed-in user" when a sign-out races a token request

1 participant