To address the multiple token refresh issue in Alamofire's AuthenticationInterceptor, you might need to ensure thread-safety and avoid concurrent refresh attempts. Would you like assistance with a potential solution or debugging steps?
When working with Alamofire's AuthenticationInterceptor, multiple concurrent API requests trigger simultaneous token refresh calls. This leads to redundant refresh attempts and can cause race conditions or inconsistencies in token handling. Steps to Reproduce: Configure AuthenticationInterceptor for token-based authentication. Initiate multiple concurrent API requests with an expired access token. Observe that the refreshToken method is called multiple times simultaneously. Expected Behavior: The AuthenticationInterceptor should manage a single token refresh request and queue subsequent API requests until the new token is available. Observed Behavior: The refreshToken method is invoked multiple times, leading to: Potential conflicts with the token refresh endpoint. Inefficient network usage. Risk of overwriting the refreshed token with an outdated one. Environment: Alamofire Version: [Specify] iOS Version: [Specify] Swift Version: [Specify] Suggested Fix: Implement a mechanism in AuthenticationInterceptor to ensure only one token refresh request is made at a time, while queuing or waiting for other requests.
This should already be the behavior of AuthenticationInterceptor, if you've implemented the various protocols correctly. If you can provide a small test app that triggers the behavior you're seeing we can investigate.
https://cloud.mail.ru/privacy/#lang-en
https://x.com/aspmedeiros?s=21
Having investigated this for another reason, I should clarify that this is the current behavior of AuthenticationInterceptor, but only if you use the same instance for all requests. The deduplication done relies on one instance of the interceptor, either passed to the Session when you create it, or passed to every Request you create. In that case it should properly deduplicate, but separate instances do separate tracking, so a single instance is key.