Skip to content

fix(gotrue,supabase): session persistence end to end - #394

Merged
Tr00d merged 19 commits into
supabase:masterfrom
fresh55:fix/session-persistence
Aug 31, 2026
Merged

Tr00d merged 19 commits into
supabase:masterfrom
fresh55:fix/session-persistence

Conversation

@fresh55

@fresh55 fresh55 commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #390

gotrue: a failed refresh destroyed the session even on a plain network error, so an offline start signed the user out for good. Now only a server-rejected refresh token signs out, same as supabase-js. While in there: an empty session store no longer fires a spurious SignedOut, a failed refresh waits one 30s tick instead of hot-looping (the supabase-js cadence), concurrent refreshes share one attempt and a refresh that finishes after the session was replaced is discarded instead of clobbering it, and a store that throws on load doesn't crash startup.

supabase: InitializeAsync now restores the persisted session, like the readme always said.

…rk reasons

RetrieveSessionAsync destroyed the session on any refresh exception, so an
offline app start wiped the persisted session for good. Only a refresh token
the server rejects signs the user out now, same as RefreshToken already does,
and being marked offline returns the session instead of throwing.

Part of supabase#390
LoadSession passed a null load straight to UpdateSession, which fires
SignedOut and in turn destroys the persistence - so a cold start with
nothing saved emitted a spurious sign-out to every listener.

Part of supabase#390
…immediately

An expired session schedules the refresh timer at zero and a failed attempt
rescheduled at zero again, so an offline start with an expired persisted
session hammered the server in a tight loop. The retry now waits 30 seconds
after a skipped or failed refresh; the defect predates this change but
restoring sessions by default makes it reachable for everyone.

Part of supabase#390
…n store

Review follow-ups: the startup refresh and the auto-refresh timer could race
the same single-use refresh token, with the loser destroying the session, and
a session store that throws on load crashed startup. Concurrent refreshes now
share the running attempt and a failing load is logged and skipped.

Part of supabase#390
The interface and readme both promise it, but the client never called
LoadSession, so after a restart every request went out with the anon key.

Fixes supabase#390
Same 30 second wait, now traceable to AUTO_REFRESH_TICK_DURATION_MS instead
of reading as an arbitrary number. Also drops two redundant identity asserts
from the restore tests.

Part of supabase#390
@fresh55 fresh55 changed the title fix: session persistence end to end fix(gotrue,supabase): session persistence end to end Aug 28, 2026
The shared refresh committed its result unconditionally, so a sign-out or a
sign-in as somebody else while the call was in flight got overwritten by the
old user's session, or signed the new user out on an invalid token. The
originating refresh token is captured and the result dropped if it no longer
matches, same as the auth-js commit guards.

Also from review: RefreshToken is async again so guard failures surface as
faulted tasks (the coalescing test now counts requests through a gated handler
instead of comparing task identity), LoadSession clears the session when the
store was emptied behind the client's back, and the never-produced
ExpiredRefreshToken guard is gone.

Part of supabase#390
An emptied store clears the session now, so the interface doc no longer
describes it as a no-op.

Part of supabase#390
The in-flight refresh was shared with any caller, so after a sign-out plus
sign-in the new session could join the old session's stale attempt and see
its outcome instead of refreshing. Attempts are now keyed to the refresh
token they started for, and the whole session is captured once so the call
can't mix the old refresh token with the new access token. Also restores
the no-persistence LoadSession no-op that the emptied-store fix regressed.

Part of supabase#390
http://localhost:1 opened a real socket; SupabaseOptions.HttpClient reaches
the auth client, so the test now throws from a handler instead.

Part of supabase#390
Adversarial review probes found three edges of the replaced-session guard:
RetrieveSessionAsync returned null while a valid replacement session was
live, RefreshSession threw NullReferenceException when a sign-out landed
mid-refresh, and a destroyed session left its refresh token in the
in-flight key.

Part of supabase#390
Clearing lived in DestroySession, but SignOut and an emptied store reach
UpdateSession(null) directly, so the token could outlive those. RefreshSession
also re-read CurrentSession across its awaits and could throw
NullReferenceException on a concurrent sign-out; it works from one snapshot
now. Adds the missing regression test for the survivor return.

Part of supabase#390
the gate only covered the dedup slot, so a sign-out could interleave with a
refresh and get its session resurrected and re-persisted. session reads,
commits and clears now happen under it, notifications stay outside.
gotrue checks nbf with no leeway and the docker clock drifts sub-second
against the host, so the admin tests failed at random with bad_jwt.
CA2016 - the handler already has the token from SendAsync.
@Tr00d Tr00d self-assigned this Aug 31, 2026
a refresh outside the timer left the old deadline armed, and a sign-in
finishing during a slow store read got overwritten by the stale load.

@Tr00d Tr00d left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, well done.
One thing I would like to change: can you remove ## 9 from the migration guide? This is not a breaking change per se, and the goal of this guide is to help customers solve breaking changes that they will encounter while upgrading.
Wdyt?

@fresh55

fresh55 commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor Author

fair point - a migration step whose only instruction is "do nothing" is not much of a step. removed 😄

@Tr00d
Tr00d merged commit ad25352 into supabase:master Aug 31, 2026
1 check passed
@Tr00d

Tr00d commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

@fresh55 I had to rename the title to make it a bit more explicit and avoid the double-context as it wouldn't render well with release-please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

session persistence doesn't work end to end

2 participants