Skip to content

feat: [SDK-5814] split a session at a user switch (2/3) - #1819

Merged
fadi-george merged 2 commits into
fadi-SDK-5814from
fadi-SDK-5814-split
Nov 23, 2026
Merged

fadi-george merged 2 commits into
fadi-SDK-5814from
fadi-SDK-5814-split

Conversation

@fadi-george

@fadi-george fadi-george commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Description

One Line Summary

Add OSSessionService.splitSession(): end the current sessions API session at a user switch and start the next one for the new user.

Details

Motivation

Part of SDK-5814, the iOS port of OneSignal/OneSignal-Android-SDK#2854. When a logged-in user logs in as someone else or logs out, the session should split so time after the switch goes to the new user. This PR adds the split; 3/3 wires it to user switches.

Scope

  • Focused: sends S1's end with the duration so far and the switch time as its end, then starts S2 now: new session ID, IDs of the new user, start time = switch time. S2's create goes out now, or is held if the new user is anonymous (1/3). The heartbeat restarts from S2's start.
  • Backgrounded: sends S1's end now with its last unfocus time and duration, and saves splitPending. The next focus within the threshold starts S2, attributed to a notification open if that focus came from one. A timeout or cold start in between sends no second end.
  • No legacy session start or end (session_count, IAM, outcomes), since the app session goes on.
  • S1 with a held create sends it before its end.
  • Provider gets sessionEnd(_:). The record build and hold rule are shared with startNewSession.
  • Legacy sessions: no-op. Not wired to anything yet.

Stack

  1. feat: [SDK-5814] hold an anonymous session's create (1/3) #1818: hold the anonymous create
  2. This PR: split
  3. feat: [SDK-5814] split or move the session on a user switch (3/3) #1820: wiring

Testing

Unit testing

New OSSessionSplitTests: focused split (end then create, IDs, start time); split to an anonymous user holds S2's create; heartbeat restarts from S2; anonymous S1 sends its held create before its end; background split ends now and starts S2 on the next focus with attribution; timeout and cold start after a background split send no second end; a second split while pending sends nothing; legacy no-op.

Manual testing

Not wired yet; wired in 3/3.

Affected code checklist

  • Notifications
    • Display
    • Open
    • Push Processing
    • Confirm Deliveries
  • Outcomes
  • Sessions
  • In-App Messaging
  • REST API requests
  • Public API changes

Checklist

Overview

  • I have filled out all REQUIRED sections above
  • PR does one thing
  • Any Public API changes are explained in the PR details and conform to existing APIs

Testing

  • I have included test coverage for these changes, or explained why they are not needed
  • All automated tests pass, or I explained why that is not possible
  • I have personally tested this on my device, or explained why that is not possible

Final pass

  • Code is as readable as possible.
  • I have reviewed this PR myself, ensuring it meets each checklist item

🤖 Generated with Claude Code

@abdulraqeeb33 abdulraqeeb33 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.

Request changes. This is the same split as ebd479a, and #1820 does not change onFocus.

onFocus reads the pending user before it takes the lock (OSSessionService.swift:335). A split on another thread can land in between. If nothing was pending, the focus continues the session that was just ended and arms its heartbeat (:344, :363). Those updates are dropped, and the visit counts for nobody. If a split was already pending, a second switch in that gap starts the next session for the user from before the switch. #1820 calls this from login on the caller thread and from _logout on the executor queues. Bump a counter under the lock at the top of splitSession, before the splitPending guard, and retry the read in onFocus when it moved. Bumping only after a committed split leaves the second case open.

splitPending and splitSession say "in the background". This runs on any unfocus. Say "while unfocused".

Removing focusedAt = now (:614) or openFocusTime = wallNow (:615) leaves the tests green. Without :615, the next session's end after its first heartbeat is 20 minutes early.

The only CI failure is OSRemoteLoggingControllerTests.testConstructsRemoteLoggerOnMainWhenConfiguredInBackground, a 2 second timeout this diff does not touch.

Add OSSessionService.splitSession(): end the current sessions API session at a
user switch and start the next one for the new user. In the background, the
end is sent now and the next session starts on the next focus. No legacy
session start or end is sent, since the app session goes on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- A switch bumps a counter under the lock before it reads the new user. A focus that read the pending split's user outside the lock retries when the counter moved.
- Say "while unfocused" for the split, since it runs on any unfocus
- Cover the next session's focus and heartbeat times after a foreground split

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fadi-george

Copy link
Copy Markdown
Contributor Author

Thanks, all three addressed in a4ef80d:

  • Race: added userSwitchGeneration, bumped under the lock at the top of splitSession, before the splitPending guard and before it reads the new user. onFocus reads it along with the pending split's user and retries when it moved by the time it takes the lock. feat: [SDK-5814] split or move the session on a user switch (3/3) #1820 keeps the bump at the top of onUserSwitched. Test: testSwitchDuringTheFocusAfterABackgroundSplitStartsTheNextSessionForTheLatestUser, which lands a second switch inside the focus's user read. The nothing-pending case goes through the same check, but I couldn't hit it from a test without adding a test-only hook, since that read makes no call outside the service.
  • Wording: splitPending, splitSession and takeSplit now say "while unfocused".
  • Coverage: testNextSessionCountsOnlyTheTimeSinceTheSplit covers focusedAt = now, and testNextSessionKilledAfterItsFirstHeartbeatEndsAtThatHeartbeat covers openFocusTime = wallNow. Removing the generation check or the openFocusTime line fails exactly those tests.

#1820 is rebased on top.

@fadi-george
fadi-george merged commit c91bdd1 into new-session-events Nov 23, 2026
6 checks passed
@fadi-george
fadi-george deleted the fadi-SDK-5814-split branch November 23, 2026 23:54
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.

3 participants