Repository navigation
refactor(gotrue): simplify the token refresh flow - #1457
Merged
spydon merged 4 commits intoJun 24, 2026
Merged
Conversation
Split the refresh logic into a clean async _doRefresh that returns the AuthResponse or throws, and is the single place that emits refresh outcomes. _callRefreshToken now just wires it into a completer. The completer is kept on purpose: dispose() must be able to cancel an in-flight refresh whose network request is hung, which only an external completion handle can do (covered by 'dispose completes active refresh with error'). Behavior is unchanged; all refresh/race/dispose tests pass.
Collaborator
|
Yeah this looks much cleaner. Really like it! |
Vinzent03
approved these changes
Jun 22, 2026
…th-error' into refactor/refresh-flow # Conflicts: # packages/gotrue/lib/src/gotrue_client.dart
spydon
marked this pull request as ready for review
June 24, 2026 15:47
spydon
merged commit Jun 24, 2026
9d87b1c
into
fix/recover-session-duplicate-auth-error
26 checks passed
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.
Goal
Reduce the complexity of the
recoverSession+_callRefreshToken+_executeRefresh+ completer + error-emission combo, without any behavior change.What changed
_executeRefresh(refreshToken, completer)into a cleanasyncworker_doRefresh(refreshToken)that simply returns theAuthResponseor throws. All the scatteredif (!completer.isCompleted) completer.complete/completeError(...)calls (6 of them, across every branch) are gone; the body now reads top-to-bottom._doRefreshis the single place that emits refresh outcomes:tokenRefreshedon success,signedOutfor an invalid token, or a stream error for a retryable/unexpected failure._callRefreshTokenjust wires_doRefreshinto the completer and cleans up the pending-map entry on completion.What deliberately stayed (and why)
The
Completeris kept on purpose. I initially wanted to drop it and use the worker's future directly as the de-dup token, butdispose()must be able to cancel an in-flight refresh whose network request is hung (the gate never resolves). A plainasyncfuture can't be completed from the outside, so only an external completion handle can do this. This is covered by the existingdispose completes active refresh with errortest, where the network gate is never completed and the future resolves solely becausedispose()errors the completer. So the completer is load-bearing, not accidental.The other essential parts are unchanged: the
_sessionVersionguard, the dispose guard, and the dual notification model (events + stream errors).recoverSessionis untouched here, it still rethrows with the current stack to stay in the async stack trace (that mechanism is independent of how the refresh resolves).Behavior / non-breaking
No public API change. All refresh, race, dedup, dispose, stack-trace, and single-error tests pass (
token_refresh_race_test.dart+refresh_token_race_test.dart), analyze + DCM clean.