Repository navigation
fix(auth): avoid reusing a stale refresh token in recoverSession - #1391
Merged
Merged
Conversation
On cold start both `recoverSession` and the auto-refresh tick (fired when the app resumes) can refresh the same persisted, expired session. If one refreshes first, the refresh token serialized in the session string passed to `recoverSession` becomes stale. Reusing it made the server respond with `refresh_token_already_used`, which removed the session and signed the user out. `recoverSession` now returns the already valid in-memory session instead of refreshing the stale token again. The check runs synchronously before `_callRefreshToken`, so combined with the existing in-flight refresh guard it closes the race entirely. Fixes #1158
The short-circuit added in recoverSession was too broad: it returned any valid in-memory session, which broke the "Sign out on wrong refresh token" test where a valid session exists but a different, bad session is recovered. Restrict the short-circuit to when the current valid session belongs to the same user as the one being recovered, which is exactly the cold-start race this guards against. Also remove the explanatory comment.
Adds a reactive guard in _callRefreshToken: when a refresh fails with `refresh_token_already_used` but the current session is still valid, another refresh (a concurrent tick or another process sharing storage) already rotated the token, so return the valid session instead of removing it and emitting signedOut. This complements the recoverSession fix: recoverSession avoids sending a stale token preemptively, while this handles the case where the token is consumed elsewhere between reading it and refreshing. A genuinely unknown refresh token still returns a different error code (refresh_token_not_found) and signs out as before. Adds refresh_token_race_test.dart covering the cold-start race, concurrent recovery, and graceful already-used handling.
spydon
force-pushed
the
fix/recover-session-stale-refresh-token
branch
from
June 10, 2026 12:19
49d9e58 to
10b9d94
Compare
grdsdev
approved these changes
Jun 10, 2026
Contributor
Author
|
I would love to get your thoughts on this @Vinzent03 (if you have time of course) since you were involved in reviewing the previous PR :) |
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.
Fixes #452
Fixes #906
Fixes #1158
Problem
On cold start, users are intermittently signed out with:
The stack trace consistently points to
recoverSession→_callRefreshToken→_refreshAccessToken. Reporters note it happens on real devices but not emulators, and is intermittent, both classic signs of a race.Root cause
On startup
supabase_flutterruns two independent refreshes against the same persisted refresh token (RT1):SupabaseAuth.recoverSession()(run as a non-awaitedCancelableOperation) →auth.recoverSession(jsonStr), which, for an expired session, calls_callRefreshToken(refreshToken)using the token serialized in the JSON string (RT1).resumedevent →startAutoRefresh(), which ticks immediately and refreshes_currentSession.refreshToken(RT1)._callRefreshToken's_refreshTokenCompleterguard only deduplicates concurrent calls. If path 2 starts and finishes first (obtainingRT2, saving the session, clearing the completer), then path 1 later calls_callRefreshToken(RT1)with the now-stale token. Since refresh tokens are single-use, the server rejects it asrefresh_token_already_used, and_callRefreshTokenreacts to that non-retryable error by removing the session and emittingsignedOut. Slower secure-storage reads on real devices widen the window, which is why it's device-specific and intermittent.Why not the previous approach (#1310)
#1310 tried to "proactively refresh on resume". As pointed out in review,
startAutoRefresh()already ticks immediately, so that change does nothing for this bug and merely adds another racing refresher.Fix
In
GoTrueClient.recoverSession, before refreshing an expired session with the token from the serialized string, check whether_currentSessionhas already advanced to a valid session via another code path. If so, return that session instead of reusing the stale token. The check runs synchronously right before_callRefreshToken(noawaitin between), so combined with the existing in-flight refresh guard it closes the race entirely.Test plan
SingleUseRefreshTokenHttpClientmirroring GoTrue's single-use refresh-token semantics, plus a regression test inclient_test.dart.refresh_token_already_usedfrom therecoverSession→_callRefreshTokenpath) and passes with it.dart analyzeclean; existing custom-client refresh tests still pass.