fix(android-sdk): discard in-flight token responses after sign-out#263
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a sign-out race in the Android SDK where an in-flight token refresh (or sign-in code exchange) could persist rotated tokens after signOut() / clearCredentials(), unintentionally resurrecting a signed-out session. It introduces a per-client SessionGuard generation stamp to discard stale async token responses.
Changes:
- Add
SessionGuardand use a generation stamp to prevent stale token responses from being committed after credential invalidation. - Refactor
signOut()/clearCredentials()to atomically clear credentials viadropCredentials()(which also bumps the generation). - Add deterministic unit tests covering refresh/sign-out and sign-in/sign-out races.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| android-sdk/android/src/main/kotlin/io/logto/sdk/android/LogtoClient.kt | Introduces SessionGuard, stamps token flows, and commits token writes only if the session generation is unchanged. |
| android-sdk/android/src/test/kotlin/io/logto/sdk/android/LogtoClientTest.kt | Adds race-focused unit tests to ensure stale in-flight refresh/sign-in results are discarded after sign-out. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
charIeszhao
approved these changes
Jun 11, 2026
Closed
4 tasks
xiaoyijun
added a commit
that referenced
this pull request
Jun 12, 2026
) * fix(android-sdk): discard in-flight token responses after sign-out (#263) * fix(android-sdk): discard in-flight token responses after sign-out * fix(android-sdk): run in-flight token flows on credential snapshots * fix(android-sdk): complete stale token flows with NOT_AUTHENTICATED consistently * refactor(android-sdk): rename SessionGuard to CredentialGuard (cherry picked from commit 6a84601) * fix(android-sdk): invalidate unauthenticated sign-out flows * fix(android-sdk): keep the refresh token when a refresh response omits it
Merged
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.
Summary
Fixes #253.
signOut()/clearCredentials()cleared the local credentials synchronously, but a token refresh (or sign-in code exchange) that was already in flight would land afterwards andverifyAndSaveTokenResponsewould unconditionally write the rotated tokens back into memory and persistent storage — silently resurrecting a signed-out session (isAuthenticatedflips back totrueon the next client construction). See the issue for the deterministic repro and timeline.This introduces a
SessionGuard— an optimistic lock (the in-memory equivalent ofUPDATE ... WHERE version = ?) around the credential set:signIn, the refresh path ofgetAccessToken) take astamp()of the current credential generation when they start.signOut/clearCredentialsdrop the credentials insideinvalidate {}, which bumps the generation atomically with the clear (extracted asdropCredentials()).verifyAndSaveTokenResponsepersists the response insidecommit(stamp) {}, which applies the write only when the generation is unchanged; otherwise the response is discarded and the flow completes withNOT_AUTHENTICATED.Clear and write are mutually exclusive critical sections (the guard's monitor), so there is no check-then-write window. A monotonic generation — rather than a boolean or an
isAuthenticatedre-check — also covers the "sign out, then immediately sign in again" case: a pre-sign-out response can no longer clobber the newer session with its already-revoked token family. JWT verification and all completions stay outside the lock; the critical sections contain only field writes.No public API changes;
NOT_AUTHENTICATEDis reused instead of adding an exception type, so the fix backports tov2.xas a clean cherry-pick.The guard is per-instance, matching the SDK's intended single-client usage; cross-instance in-memory state coherence is a pre-existing, separate concern.
Testing
unit tests — three new race tests (deterministic, captured-callback timing, no sleeps):
signOutis discarded and does not resurrect the credentials;signOutis discarded.All three fail on the previous implementation and pass with the fix; the 35 existing tests are unaffected.
Checklist
.changeset(N/A — this repo uses release-please)🤖 Generated with Claude Code