Repository navigation
Auth (Desktop): avoid listeners_mutex in current_user() after cache load - #2001
AustinBenoit wants to merge 2 commits into
Conversation
Fixes #1972. Makes AuthData::persistent_cache_load_pending a std::atomic<bool> so desktop Auth::current_user() skips registering a temporary CurrentUserBlockListener (and acquiring listeners_mutex) once the initial persistent cache load has completed, breaking the lock-order deadlock between Auth::SignOut() and concurrent Firestore token requests. Also updates Auth::GetAuthTokenForRegistry() (FnAuthGetCurrentToken) to read user->id_token directly via UserView::TryRead after ensuring persistent cache load, avoiding a startup race before NotifyIdTokenListeners() populates IdTokenRefreshListener.
There was a problem hiding this comment.
Code Review
This pull request resolves a deadlock issue in the Desktop Auth implementation by making persistent_cache_load_pending atomic. This allows Auth::current_user() to bypass acquiring listeners_mutex once the persistent cache has loaded, preventing deadlocks when called concurrently with operations like Auth::SignOut(). Additionally, GetAuthTokenForRegistry has been updated to retrieve the ID token using UserView::TryRead instead of locking token_listener_mutex. Two regression tests have been added to verify these changes. I have no feedback to provide as there are no review comments.
Integration test with FLAKINESS (succeeded after retry)Requested by @AustinBenoit on commit e7925f2
Add flaky tests to go/fpl-cpp-flake-tracker |
Description
Fixes #1972.
On desktop,
Auth::SignOut()callsNotifyAuthStateListeners(), which holdsAuthData::listeners_mutexwhile invoking listener callbacks (including Firestore'sFirebaseCppCredentialsProvider::OnAuthStateChanged(), which acquirescontents_->mutex). Concurrently, Firestore token requests holdcontents_->mutexwhile calling into Auth (GetAuthTokenAsyncForRegistry()/GetCurrentUserUidForRegistry()), which callAuth::current_user(). BecauseAuth::current_user()unconditionally registered a temporaryCurrentUserBlockListenerviaAddAuthStateListener()on every call, it acquiredlisteners_mutexand deadlocked withSignOut().Changes
auth/src/data.h&auth/src/auth.cc: ChangedAuthData::persistent_cache_load_pendingtostd::atomic<bool>, storedfalsewithstd::memory_order_releaseinAUTH_NOTIFY_LISTENERS(still underlisteners_mutex), and loaded withstd::memory_order_acquireinAddAuthStateListener()/AddIdTokenListener().auth/src/desktop/auth_desktop.cc:Auth::current_user()to skip registeringCurrentUserBlockListener(and acquiringlisteners_mutex) oncepersistent_cache_load_pendinghas been cleared.Auth::GetAuthTokenForRegistry()(FnAuthGetCurrentToken, used by Database, Functions, and Storage) to guardout, clear*result, and readuser->id_tokendirectly fromUserView::TryReadafter(void)auth->current_user(). Previously it relied oncurrent_user()blocking onlisteners_mutexuntilLoadFinishTriggerListeners()also finishedNotifyIdTokenListeners()and populatedIdTokenRefreshListener::current_token_.auth/tests/desktop/auth_desktop_test.cc: Added two deterministic regression tests:SignOutDoesNotDeadlockWithConcurrentCurrentUser(reproduces the exact AB-BA lock order betweenSignOut()'sAuthStateListenercallback and a concurrent thread callingcurrent_user()).GetAuthTokenForRegistryReturnsPersistedTokenBeforeIdTokenListenersRun(verifiesFnAuthGetCurrentTokenreturns the persisted ID token duringLoadFinishTriggerListeners() -> NotifyAuthStateListeners()beforeNotifyIdTokenListeners()runs, and clears it afterSignOut()).release_build_files/readme.md: Added anUpcomingrelease note entry.Testing
firebase_authand all Auth desktop unit tests on Linux (GCC 15.2.0, Debug, system OpenSSL, fake secure storage).firebase_auth_desktop_teston both new regression tests (2/2 passed) and fullctest --output-on-failure -R auth(17/17 Auth test suites passed).python3 scripts/format_code.py --git_diff,clang-format --style=file --dry-run --Werror, and./scripts/gha/check_copyright.sh -g.Type of Change