Repository navigation
fix(gotrue): prevent stale token refresh from overwriting concurrent session changes - #1351
Conversation
There was a problem hiding this comment.
Pull request overview
This PR addresses race conditions in packages/gotrue token refresh flow where an in-flight refresh could overwrite a newer session established concurrently (e.g., via signIn, signOut, or setSession), and improves refresh de-duplication behavior.
Changes:
- Add a monotonic session version counter to detect and discard stale refresh results.
- Replace the previous global refresh completer with a same-token de-dup + different-token serialization mechanism.
- Add a new test suite covering common token refresh race scenarios.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| packages/gotrue/lib/src/gotrue_client.dart | Introduces session version guarding and a serialized refresh queue with same-token de-duplication. |
| packages/gotrue/test/src/token_refresh_race_test.dart | Adds unit tests to validate refresh concurrency behavior and stale-result handling. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
I'm starting to think if this is the correct solution to fix this. Couldn't we more adopt how auth-js does this? I haven't looked too much into their solution, but they are locking their session, which could be a more generally solution. |
|
@Vinzent03 JS also have many issues related to token refresh, so JS may not be the right solution, I think both needs to rethink how token refresh is implemented, not saying this is the right approach, I didn't have enough time yet to review it. |
|
@brunovsiqueira thanks a lot for your contribution, those are a lot of complex changes, and really hard scenarios to manually test. Did you face all the issues this PR claims to fix when using the library? Also, can you fix CI by formatting the test file, that seems like the only issue. |
|
Hey @grdsdev I added unit tests to reproduce the race conditions, then did the refactor, and the race conditions tests got fixed. Thats how I "tested" it, but I didnt experience them myself. Fixed the test file formatting in ad7c342. |
|
Sorry, it was @grdsdev who suggested applying the same pattern to token refresh 😅 |
|
Interesting that the js SDK has issues with this as well. So I think we can be a bit more freely on how to fix this. |
ad7c342 to
100065a
Compare
|
@Vinzent03 Good points. The serial queue ( Regarding |
|
Hi team. Anything pending to merge this PR in? |
Hey @brunovsiqueira, sorry for the wait! |
|
@brunovsiqueira what about doing something like this instead? So that we don't have to do class _SessionState {
Session? _session;
int version = 0;
Session? get session => _session;
set session(Session? value) {
version++;
_session = value;
}
}
Session? get _currentSession => _sessionState.session;
set _currentSession(Session? value) => _sessionState.session = value;Like this we make sure that everywhere where |
100065a to
21d0415
Compare
spydon
left a comment
There was a problem hiding this comment.
Thanks for your contribution @brunovsiqueira, I'll try to get out a new version with this today :)
- Replace single _activeRefresh record with Map<String, Completer> to fix A-B-A dedup bug: refresh(A) → refresh(B) → refresh(A) now correctly deduplicates the second A call instead of enqueuing a third network request. - Bump _sessionVersion in updateUser and setInitialSession, which were writing _currentSession directly and bypassing the version guard. - Add _isDisposed flag checked in _executeRefresh to prevent mutating state or emitting events on closed stream controllers after dispose(). - Pass stack traces to completeError for better debugging. - Add A-B-A dedup test case.
…uard Remove _pendingRefreshOperation serial future chain. Different-token refreshes now run concurrently — the _sessionVersion guard already ensures only the latest result is applied, making the queue redundant for correctness. This simplifies the code at the cost of an occasional extra HTTP request in the rare different-token race scenario.
Introduce a small _SessionState helper so every write to _currentSession automatically bumps version, removing the need to manually call _sessionVersion++ before each assignment.
…attern The de2adf9 main commit added a refresh_token_already_used handler that completed the old _refreshTokenCompleter. Adapt it to use the new Completer<AuthResponse> parameter from _executeRefresh instead.
21d0415 to
d45302c
Compare
|
@spydon sorry for the late reply. |
Haha don't worry about it, your reply was like a month faster than ours 😄 |
Summary
Fixes race conditions in
GoTrueClient._callRefreshToken()where an in-flight token refresh could overwrite a newer session established by a concurrentsignIn,signOut, orsetSessioncall._SessionState: session and version are encapsulated in a_SessionStatehelper whose setter auto-increments the version on every write._executeRefreshsnapshots the version before the network round-trip and discards the result if it changed, preventing stale refreshes from overwriting a newer session.Completerpattern ignored therefreshTokenparameter, so_callRefreshToken(tokenB)would silently return tokenA's in-flight future. Now, same-token calls de-duplicate correctly while different-token calls each run their own request, tracked in aMap<String, Completer<AuthResponse>> _pendingRefreshes._autoRefreshTokenTick: reads_currentSessioninto a local variable once so thatrefreshTokenandexpiresAtcome from the same snapshot._removeSession()and emitssignedOutif the session was replaced by a concurrent operation while the refresh was in-flight. Allcompleter.complete/completeErrorcalls are guarded with!completer.isCompletedfor dispose safety. If arefresh_token_already_usederror is received but the current session is still valid, the result is resolved with the existing session instead of signing the user out._isDisposedflag prevents_executeRefreshfrom mutating state or emitting events on closed stream controllers afterdispose(). All pending completers are resolved with anAuthException('Disposed')on teardown.Test results: before vs after
All 8 race condition tests were run against
main(old code) and this branch. 7 of 8 fail onmain, all 8 pass after the fix:Related issues
Test plan
packages/gotrue/test/src/token_refresh_race_test.dartdart analyzeclean (only pre-existing deprecation infos)