Skip to content

fix(auth): synchronize AuthAPNSTokenManager state across concurrent callers - #16902

Merged
paulb777 merged 3 commits into
mainfrom
pb-fix-apns-token-manager-race
Oct 14, 2026
Merged

paulb777 merged 3 commits into
mainfrom
pb-fix-apns-token-manager-race

Conversation

@paulb777

@paulb777 paulb777 commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

Summary

Synchronize access to tokenStore, pendingCallbacks, and pendingRequestGeneration in AuthAPNSTokenManager with an NSLock, and invoke callbacks outside the lock.

Why

This race predates #16665; that PR's new test exposed it. Before #16665, getTokenInternal(callback:) already checked and then wrote pendingCallbacks without synchronization, and since #11557 the async getToken() wrapper has let callers reach it from any thread. PhoneAuthProvider calls getToken() from async code, not on kAuthGlobalWorkQueue, where setAPNSToken, cancel(withError:), and the timeout run. #16665 fixed a separate timeout bug, and its testGetTokenTimeoutWithMultipleWaiters is the first test to call getToken() from two threads at once:

async let firstResult = getTokenResult(from: manager)
async let secondResult = getTokenResult(from: manager)
let results = await [firstResult, secondResult]

Because AuthAPNSTokenManagerTests is not @MainActor-isolated, the two async let child tasks run concurrently on the cooperative thread pool and call getTokenInternal(callback:) simultaneously from different threads. AuthAPNSTokenManager had no synchronization around pendingCallbacks and pendingRequestGeneration:

  • When both tasks entered getTokenInternal before either reached pendingCallbacks = [callback], both observed pendingCallbacks.count > 0 as false.
  • The first task set pendingRequestGeneration = 1 and pendingCallbacks = [callback1].
  • The second task set pendingRequestGeneration = 2 and overwrote pendingCallbacks = [callback2], dropping callback1.
  • When the timeout fired on kAuthGlobalWorkQueue, callback1's withUnsafeThrowingContinuation was never resumed, causing await [firstResult, secondResult] to hang indefinitely until the 15-minute CI job timeout killed xcodebuild.

Thread Sanitizer

With -enableThreadSanitizer YES, a test that makes 100 concurrent getTokenInternal calls with DispatchQueue.concurrentPerform and then sets token reports a Swift access race in getTokenInternal(callback:) with both the implementation before #16665 (7aa8dfb^) and current main, and no race with this change. That test only uses the token path, so it doesn't depend on the timeout logic #16665 changed. It was a local experiment and isn't part of this PR.

Impact in shipping code

From reading the code (not reproduced in an app):

  • Two phone verifications started at the same time can lose one caller's callback, so that verification never completes.
  • If the APNs token arrives just as a verification starts, the caller can miss it. It then waits for the 5-second timeout and falls back to reCAPTCHA.

Changes

  • Protect _tokenStore, pendingCallbacks, and pendingRequestGeneration in AuthAPNSTokenManager with an NSLock.
  • Atomically check requestGeneration == pendingRequestGeneration and drain pendingCallbacks under the lock when a timeout fires, when a token is set, or when cancel(withError:) is called, then invoke callbacks outside the lock.
  • Add testConcurrentGetTokenInternalCallbacksAllCompleteOnTimeout using DispatchQueue.concurrentPerform with a bounded waitForExpectations(timeout: 2) to verify concurrent callers do not drop callbacks.
  • Add a CHANGELOG entry.
…allers

Protect tokenStore, pendingCallbacks, and pendingRequestGeneration with an NSLock in AuthAPNSTokenManager and invoke callbacks outside the lock.

When concurrent async callers (such as async let in testGetTokenTimeoutWithMultipleWaiters added in #16665) entered getTokenInternal simultaneously on different cooperative pool threads, both could observe pendingCallbacks as empty, increment pendingRequestGeneration, and overwrite pendingCallbacks = [callback]. The overwritten callback's continuation was never resumed, causing AuthUnit CI runs to hang until the 15-minute timeout.
@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.

@paulb777

Copy link
Copy Markdown
Member 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 resolves a data race in the APNs token manager (AuthAPNSTokenManager) by introducing thread safety using NSLock to synchronize access to the token store, pending callbacks, and request generation. It also adds a concurrent unit test to verify that callbacks are not dropped during timeouts. The review feedback suggests capturing self weakly in the asynchronous timeout closure to prevent potential retain cycles or unnecessary lifetime extension of the manager.

…leased

Add a test that a pending getTokenInternal callback still gets the timeout
error after the caller releases its reference to the manager, and document
why the timeout closure captures self strongly. With a weak capture, the
pending callbacks would be dropped without being called, and getToken()
would never return.
@paulb777
paulb777 merged commit 656a625 into main Oct 14, 2026
93 checks passed
@paulb777
paulb777 deleted the pb-fix-apns-token-manager-race branch October 14, 2026 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

3 participants