Repository navigation
feat: [SDK-5813] split or move the session on a user switch (3/3) - #2856
Conversation
d123284 to
2d992a5
Compare
📊 Diff Coverage ReportDiff Coverage Report (Changed Lines Only)Gate: aggregate coverage on changed executable lines must be ≥ 80% (JaCoCo line data for lines touched in the diff). Changed Files Coverage
Overall (aggregate gate)38/38 touched executable lines covered (100.0% — requires ≥ 80%) |
2d992a5 to
b10a1fc
Compare
b10a1fc to
f64d04c
Compare
f64d04c to
0b3ada9
Compare
0b3ada9 to
58cc951
Compare
58cc951 to
c8c7985
Compare
6cfe7b3 to
6700cf6
Compare
35807cc to
884c5d1
Compare
abdulraqeeb33
left a comment
There was a problem hiding this comment.
Back-to-back switches still split twice on 884c5d1.
login() and logout() replace the identity on the caller thread, UserSwitchHandler only queues (SessionService.kt:156), and startNextSession reads the live identity store (:409-416). So logout(); login("b") while focused can land both replaces before the first block runs, and you get end S1 | create S2 (b) | end S2 (b, 0 ms) | create S3 (b). login("b"); login("c") does the same, and so does a login that lands after the first focus is queued but before it runs. The hasFocused check doesn't catch that order.
Skip when the session already follows the captured user, right after the takeIf at :159:
if (onesignalId != null && session.onesignalId == onesignalId) return@runOnSerialIO
Every switch mints a new local ID, so a match means an earlier block already started that user's session. Please add a test that queues both runOnSerialIO blocks before running them.
Also on this head:
- Background switch, then a focus without consent, then consent. The pending branch (:383) sets startedWithoutConsent (:418), so the consent focus takes the new-session branch (:375), resets startTime, and fires onSessionStarted again. Gate the pending branch on config?.isAwaitingConsent != true.
- The background split enqueues S1's end without flush (:446), and backgroundRun returns before its own flush because the split is pending (:276, :314). flush = !counting.
- Nothing defends the move's writes (:164-165), and the IV move test switches to non-local IDs, so waitsForLogin is false either way.
Nits:
- SessionModel.kt:122 still says updates keep the onesignalId after a login. A held move rewrites it.
- The !releaseHeldCreate(session) -> null branch at :438 is dead.
- The PR body doesn't mention that switches before the first focus are ignored.
On login or logout, a held create moves to the new user and goes out right away if that user is identified. A logged-in user's switch or logout splits the session. An anonymous user's login after the create was sent keeps the session. Removes the push subscription workaround this replaces. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Before the first focus the session is the last run's, which the cold start ends, so a login then must not move or split it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… is unknown Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
884c5d1 to
86055bc
Compare
…ent after a background split - Skip a switch whose user the session already follows. login() and logout() queue their handling, and a focus or split reads the live user, so an earlier block can already have started the later user's session. - A focus without consent leaves a background split pending, so consent starts the next session instead of a new app session. - Flush a background split's end, since backgroundRun won't send it. - Drop the split's held-create check, which can't be reached. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks! Addressed in 26a9bc6.
Nits: the onesignalId KDoc now mentions the held move, the dead !releaseHeldCreate branch is gone, and the body notes that switches before the first focus are ignored. Also in f494428 (1/3), matching iOS: an unknown JWT requirement now holds a local anonymous create mid-session, but not at the session end. |
Description
One Line Summary
On login or logout, move a held session to the new user, or split a logged-in user's session.
Details
Motivation
Part of SDK-5813. Wires #2853 (hold) and #2854 (split) to user switches, so time after a switch goes to the new user and requests never mix one user's onesignal_id with another user's push sub.
Scope
A
UserSwitchHandlerinSessionServicelistens for identity model replaces (login, logout), ignoringHYDRATE(a user refresh). For a valid sessions API session:login()andlogout()queue their handling, so back-to-back switches (logout then login, or login B then login C) split once.backgroundRunwon't send it. A focus without consent leaves the split pending until consent is given.SessionModel.externalId: the external ID of the user the session follows.appliesToworkaround on the pinned subscription ID translator, which moving and splitting replace.splitSession(); the split is only reachable from a user switch.Notes:
externalId, so its first switch doesn't split. Fine sincenew-session-eventshasn't shipped.Stack
Testing
Unit testing
SessionServiceTests: the #2854 split tests now go through a real identity replace. New: login while held moves and sends the create (JWT off and on); anonymous login after the create was sent keeps the session, then login B splits; JWT logout splits and a later login B moves S2; login after the first unfocus moves a local anonymous session while the JWT requirement is unknown; user refresh doesn't split. Back-to-back switches queued before either runs split once; consent after a background split starts S2 without a new app session; the background split's end is flushed. Removed the workaround test.Manual testing
Ran the full stack (#2856 head) in the demo app on an Android 16 emulator, with the sessions API flag on. All session requests got 202.
Not covered on the emulator: logout in the background and login before the first focus (both need app code to trigger), and a kill mid-write. Unit tests cover these.
Affected code checklist
Checklist
Overview
Testing
Final pass
🤖 Generated with Claude Code