Skip to content

refactor(gotrue): simplify the token refresh flow - #1457

Merged
spydon merged 4 commits into
fix/recover-session-duplicate-auth-errorfrom
refactor/refresh-flow
Jun 24, 2026
Merged

spydon merged 4 commits into
fix/recover-session-duplicate-auth-errorfrom
refactor/refresh-flow

Conversation

@spydon

@spydon spydon commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Draft, stacked on top of #1450 so the diff is isolated to the refactor. Base branch is fix/recover-session-duplicate-auth-error.

Goal

Reduce the complexity of the recoverSession + _callRefreshToken + _executeRefresh + completer + error-emission combo, without any behavior change.

What changed

  • Split the old _executeRefresh(refreshToken, completer) into a clean async worker _doRefresh(refreshToken) that simply returns the AuthResponse or throws. All the scattered if (!completer.isCompleted) completer.complete/completeError(...) calls (6 of them, across every branch) are gone; the body now reads top-to-bottom.
  • _doRefresh is the single place that emits refresh outcomes: tokenRefreshed on success, signedOut for an invalid token, or a stream error for a retryable/unexpected failure.
  • _callRefreshToken just wires _doRefresh into the completer and cleans up the pending-map entry on completion.

What deliberately stayed (and why)

The Completer is kept on purpose. I initially wanted to drop it and use the worker's future directly as the de-dup token, but dispose() must be able to cancel an in-flight refresh whose network request is hung (the gate never resolves). A plain async future can't be completed from the outside, so only an external completion handle can do this. This is covered by the existing dispose completes active refresh with error test, where the network gate is never completed and the future resolves solely because dispose() errors the completer. So the completer is load-bearing, not accidental.

The other essential parts are unchanged: the _sessionVersion guard, the dispose guard, and the dual notification model (events + stream errors).

recoverSession is 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.

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.
@github-actions github-actions Bot added the auth This issue or pull request is related to authentication label Jun 22, 2026
@Vinzent03

Copy link
Copy Markdown
Collaborator

Yeah this looks much cleaner. Really like it!

spydon added 2 commits June 24, 2026 16:18
…th-error' into refactor/refresh-flow

# Conflicts:
#	packages/gotrue/lib/src/gotrue_client.dart
@spydon
spydon marked this pull request as ready for review June 24, 2026 15:47
@spydon
spydon requested a review from a team as a code owner June 24, 2026 15:47
@spydon
spydon merged commit 9d87b1c into fix/recover-session-duplicate-auth-error Jun 24, 2026
26 checks passed
@spydon
spydon deleted the refactor/refresh-flow branch June 24, 2026 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auth This issue or pull request is related to authentication

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants