fix(android): prevent ANR from concurrent token refreshes - #2516
Open
sentry[bot] wants to merge 1 commit into
Open
sentry[bot] wants to merge 1 commit into
sentry[bot] wants to merge 1 commit into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
Contributor
|
@sentry review |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR addresses an Application Not Responding (ANR) issue on Android devices, where the main thread was observed to block in JNI calls during vsync dispatch.
Root Cause:
The ANR was traced to a burst of JavaScript activity, specifically when multiple API calls simultaneously encountered a 401 (Unauthorized) error. This led to:
useActivitySSEhook had its own token refresh logic, separate from the sharedwithRefreshTokenutility. This meant that an SSE 401 and other API 401s could trigger two independent token refresh HTTP requests.withRefreshToken'srefreshTokenPromiseresolved, every concurrent caller waiting on it would independently callsaveNewTokens(). This resulted in N redundant Zustand store updates and N React re-render schedules occurring simultaneously.Solution:
This fix implements three key changes to mitigate the JS microtask burst:
ensureTokenRefreshed()inlib/utils/utils.tsto centralize the token refresh mechanism. This function now manages therefreshTokenPromiseand ensures that only one HTTP token refresh request is ever in flight, even if multiple parts of the app simultaneously encounter 401s.saveNewTokens()call has been moved inside theensureTokenRefreshed()promise chain. This guarantees that tokens are saved to the store exactly once after a successful refresh, eliminating redundant Zustand updates.withRefreshToken, non-originating callers (those that didn't initiate the token refresh) now yield for one event-loop tick (await new Promise(r => setTimeout(r, 0))) before retrying their original API call. This spreads the fan-out of concurrent retries across multiple event-loop ticks, preventing a single, large microtask avalanche that could trigger GC pressure and ANRs.useActivitySSEhook (hooks/useActivitySSE.ts) has been updated to use the newensureTokenRefreshed()utility, routing its token refresh needs through the shared mechanism.Fixes SOLID-TS