Conversation
_recover_and_refresh did not return after a successful refresh, so it fell through to _remove_session(). An expired-but-refreshable persisted session was refreshed, TOKEN_REFRESHED was emitted, and then the new session was deleted from storage and its auto-refresh timer cancelled, signing the user out on every restart after the access token expired. Return once the refresh succeeds. The session is still removed when the refresh fails with a non-retryable error or retries are exhausted. Applies to the async client and its generated sync twin. Fixes supabase#1646
This branch has not been deployed
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.
What kind of change does this PR introduce?
Bug fix.
What is the current behavior?
Fixes #1646
_recover_and_refresh(called byinitialize_from_storage()) doesn't return after a successful refresh:When the persisted session's access token has expired, the client refreshes it.
_save_sessionstores the new session and starts the auto-refresh timer, andTOKEN_REFRESHEDis emitted. Control then falls through to_remove_session(), which deletes the new session and cancels the timer. Any app restarted after the access token expired (1 hour by default) signs the user out, even though the refresh succeeded. Listeners also gotTOKEN_REFRESHEDfor a session that no longer exists.What is the new behavior?
_recover_and_refreshreturns as soon as the refresh succeeds. Failures behave as before:This matches auth-js's
_recoverAndRefresh, which only drops the session when the refresh returns an error.SyncGoTrueClienthas the same code (it's generated bybuild-sync), so it gets the same one-line change.Tests
test_initialize_from_storage_keeps_refreshed_sessionintests/_async/test_gotrue.py, plus its unasync-generated twin intests/_sync/test_gotrue.py. Each stores an expired session, serves/tokenfrom anhttpx.MockTransport, callsinitialize_from_storage(), and asserts:TOKEN_REFRESHEDwas emitted;Both fail on
main(storedisNone) and pass here. They don't need the GoTrue container.Validation
uv run --package supabase_auth pytest src/auth/tests/test_helpers.py: 22 passed.uv run --package supabase_auth mypy src/auth/src/supabase_auth src/auth/tests: no issues in 36 source files.uv run ruff check --fix/uv run ruff format: clean.git diff --check: clean.The rest of
src/auth/testsneeds the GoTrue container frommake auth.start-infra(Docker), which wasn't running. The full suite gives 44 passed and 122 failed with this change, versus 42 passed and 124 failed onmain. All 122 remaining failures arehttpx.ConnectError.