Repository navigation
fix(auth): synchronize AuthAPNSTokenManager state across concurrent callers - #16902
Conversation
…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.
Using Gemini Code AssistThe 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
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 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. |
|
/gemini review |
There was a problem hiding this comment.
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.
Summary
Synchronize access to
tokenStore,pendingCallbacks, andpendingRequestGenerationinAuthAPNSTokenManagerwith anNSLock, 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 wrotependingCallbackswithout synchronization, and since #11557 theasyncgetToken()wrapper has let callers reach it from any thread.PhoneAuthProvidercallsgetToken()from async code, not onkAuthGlobalWorkQueue, wheresetAPNSToken,cancel(withError:), and the timeout run. #16665 fixed a separate timeout bug, and itstestGetTokenTimeoutWithMultipleWaitersis the first test to callgetToken()from two threads at once:Because
AuthAPNSTokenManagerTestsis not@MainActor-isolated, the twoasync letchild tasks run concurrently on the cooperative thread pool and callgetTokenInternal(callback:)simultaneously from different threads.AuthAPNSTokenManagerhad no synchronization aroundpendingCallbacksandpendingRequestGeneration:getTokenInternalbefore either reachedpendingCallbacks = [callback], both observedpendingCallbacks.count > 0asfalse.pendingRequestGeneration = 1andpendingCallbacks = [callback1].pendingRequestGeneration = 2and overwrotependingCallbacks = [callback2], droppingcallback1.kAuthGlobalWorkQueue,callback1'swithUnsafeThrowingContinuationwas never resumed, causingawait [firstResult, secondResult]to hang indefinitely until the 15-minute CI job timeout killedxcodebuild.Thread Sanitizer
With
-enableThreadSanitizer YES, a test that makes 100 concurrentgetTokenInternalcalls withDispatchQueue.concurrentPerformand then setstokenreports a Swift access race ingetTokenInternal(callback:)with both the implementation before #16665 (7aa8dfb^) and currentmain, 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):
Changes
_tokenStore,pendingCallbacks, andpendingRequestGenerationinAuthAPNSTokenManagerwith anNSLock.requestGeneration == pendingRequestGenerationand drainpendingCallbacksunder the lock when a timeout fires, when a token is set, or whencancel(withError:)is called, then invoke callbacks outside the lock.testConcurrentGetTokenInternalCallbacksAllCompleteOnTimeoutusingDispatchQueue.concurrentPerformwith a boundedwaitForExpectations(timeout: 2)to verify concurrent callers do not drop callbacks.