Repository navigation
Firestore (Desktop): remove redundant IsSignedIn checks in GetToken() - #1994
AustinBenoit wants to merge 3 commits into
Conversation
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
There was a problem hiding this comment.
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.
❌ Integration test FAILEDRequested by @AustinBenoit on commit fdb9028
Add flaky tests to go/fpl-cpp-flake-tracker |
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.
Description
FirebaseCppCredentialsProvider::GetToken()checkedIsSignedIn()and then calledRequestToken(), which hard-assertedIsSignedIn()a second time. Both checks ran under Firestore'scontents_->mutex, butAuth::SignOut()mutates the signed-in user under Auth's own mutex. IfSignOut()ran between the two checks, the assert aborted the process.Why drop
IsSignedIn()instead of locking across Auth and FirestoreAdding 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 checksAuth::FindAuth(app)andauth->current_user().is_valid()before allocating aFuture, so signed-out calls still do a single registry hop and do not allocate.Changes
IsSignedIn()and the redundantSIMPLE_HARD_ASSERT(IsSignedIn(), ...)inRequestToken().GetAuthTokenAsync()return a default-constructed invalidFuture<std::string>()(kFutureStatusInvalid) instead ofSuccessfulFuture<std::string>("")when Auth is unavailable or signed out.RequestToken(), checkfuture.status() == kFutureStatusInvalidand invokelistener(AuthToken::Unauthenticated()), matchingRequestToken()'s documented contract.force_refresh_atomically withforce_refresh_.exchange(false).Why concurrent
Auth::SignOut()is now safe at every stepUser::GetTokenInternal()releases it mid-call (user_desktop.cc):GetAuthTokenAsync()returns an invalidFuture, andRequestToken()immediately returnsAuthToken::Unauthenticated(). (Previously, a released future silently dropped theOnCompletion()callback and left Firestore waiting.)ValidateCurrentUser(): Auth returns a completed failedFuturewithkAuthErrorNoSignedInUser, whichConvertToken()maps tokErrorUnauthenticated.OnAuthStateChanged()incrementstoken_generationundercontents_->mutex, soOnToken()rejects the stale result withkErrorAbortedand Firestore retries cleanly.Testing
credentials_provider_desktop.ccandcreate_credentials_provider_desktop.ccwithclang++-19 -Wall -Wextra(0 warnings in touched files).clang-format --style=file --dry-run --Werrorand./scripts/gha/check_copyright.sh -g.Type of Change