Skip to content

Commit 0aae9f1

Browse files
authored
Fix cold starts incorrectly using the previous firstSessionId (#7047)
Fix cold starts incorrectly using the previous firstSessionId and sessionIndex This came up during the dogfooding session When a cold start happens, we must always create a new session. Before this fix, it would preserve the current session's firstSesisonId and sessionIndex when that session hadn't expired. But a cold start is regardless of the current session expiring
1 parent 21215eb commit 0aae9f1

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

firebase-sessions/src/main/kotlin/com/google/firebase/sessions/SharedSessionRepository.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ constructor(
9898
CoroutineScope(backgroundDispatcher).launch {
9999
try {
100100
sessionDataStore.updateData {
101-
// TODO(mrober): Double check time makes sense?
102101
sessionData.copy(backgroundTime = timeProvider.currentTime())
103102
}
104103
} catch (ex: Exception) {
@@ -138,10 +137,17 @@ constructor(
138137
currentSessionData.processDataMap
139138
}
140139

140+
val currentSession =
141+
if (isColdStart) {
142+
// For a cold start, do not keep the current session
143+
null
144+
} else {
145+
currentSessionData.sessionDetails
146+
}
147+
141148
// This is an expression, and returns the updated session data
142149
if (isSessionExpired || isColdStart) {
143-
val newSessionDetails =
144-
sessionGenerator.generateNewSession(currentSessionData.sessionDetails)
150+
val newSessionDetails = sessionGenerator.generateNewSession(currentSession)
145151
sessionFirelogPublisher.mayLogSession(sessionDetails = newSessionDetails)
146152
processDataManager.onSessionGenerated()
147153
currentSessionData.copy(

firebase-sessions/src/test/kotlin/com/google/firebase/sessions/SharedSessionRepositoryTest.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,7 @@ class SharedSessionRepositoryTest {
127127
fakeDataStore.close()
128128

129129
assertThat(sharedSessionRepository.localSessionData.sessionDetails)
130-
.isEqualTo(
131-
SessionDetails(SESSION_ID_1, SESSION_ID_INIT, 1, fakeTimeProvider.currentTime().us)
132-
)
130+
.isEqualTo(SessionDetails(SESSION_ID_1, SESSION_ID_1, 0, fakeTimeProvider.currentTime().us))
133131
}
134132

135133
@Test

0 commit comments

Comments
 (0)