Skip to content

Commit bd0de3a

Browse files
fix(audience-session): reset starts a new session
Prior Reset cleared userId and purged the queue but kept the old Session running. Subsequent Track events carried the old sessionId with a new anonymousId — a confusing "session straddles two users" wire state. Match Web SDK reset() behaviour: - Dispose old session (session_end is enqueued then wiped by PurgeAll, matching Web SDK's silent teardown). - Reset identity on disk (next GetOrCreate mints a fresh anonymousId). - Mint a new Session when consent allows tracking; fire session_start via Start() outside _initLock. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 736b453 commit bd0de3a

1 file changed

Lines changed: 29 additions & 8 deletions

File tree

src/Packages/Audience/Runtime/ImmutableAudience.cs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,18 +250,39 @@ public static void Alias(string fromId, string fromType, string toId, string toT
250250
Enqueue(msg);
251251
}
252252

253-
// Logs out the current player. Clears userId, mints a fresh anonymousId,
254-
// discards queued events. Call FlushAsync() first to preserve queued events.
253+
// Logs out the current player. Clears userId, discards queued events,
254+
// mints a fresh anonymousId, and starts a new session. Matches Web SDK
255+
// reset(): no session_end is emitted for the old session (it is enqueued
256+
// and then purged). Call FlushAsync() first to preserve queued events.
255257
public static void Reset()
256258
{
257-
if (!_initialized) return;
259+
Session? sessionToStart = null;
260+
AudienceConfig? config;
261+
lock (_initLock)
262+
{
263+
if (!_initialized) return;
264+
config = _config;
265+
if (config == null) return;
258266

259-
var config = _config;
260-
if (config == null) return;
267+
// Dispose old session. session_end lands in the queue and is
268+
// wiped by PurgeAll below — matches Web SDK's silent-teardown.
269+
_session?.Dispose();
270+
_session = null;
261271

262-
_userId = null;
263-
_queue?.PurgeAll();
264-
Identity.Reset(config.PersistentDataPath!);
272+
_queue?.PurgeAll();
273+
Identity.Reset(config.PersistentDataPath!);
274+
_userId = null;
275+
276+
// Mint a new session if consent allows tracking.
277+
if (_consent.CanTrack())
278+
{
279+
_session = new Session(Track);
280+
sessionToStart = _session;
281+
}
282+
}
283+
284+
// Start outside _initLock — session_start → Track takes its own locks.
285+
sessionToStart?.Start();
265286
}
266287

267288
// Asks the backend to erase this player's data. Await for ack, or discard for fire-and-forget.

0 commit comments

Comments
 (0)