Skip to content

Commit de93104

Browse files
authored
Make AQS resilient to background init in multi-process apps (#6699)
1 parent 263fbc0 commit de93104

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

firebase-sessions/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Unreleased
22

3+
* [fixed] Make AQS resilient to background init in multi-process apps.
34

45
# 2.0.7
56
* [fixed] Removed extraneous logs that risk leaking internal identifiers.

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,16 @@ internal class SessionLifecycleService : Service() {
127127

128128
/** Generates a new session id and sends it everywhere it's needed */
129129
private fun newSession() {
130-
SessionGenerator.instance.generateNewSession()
131-
Log.d(TAG, "Generated new session.")
132-
broadcastSession()
133-
SessionDatastore.instance.updateSessionId(SessionGenerator.instance.currentSession.sessionId)
130+
try {
131+
SessionGenerator.instance.generateNewSession()
132+
Log.d(TAG, "Generated new session.")
133+
broadcastSession()
134+
SessionDatastore.instance.updateSessionId(
135+
SessionGenerator.instance.currentSession.sessionId
136+
)
137+
} catch (ex: IllegalStateException) {
138+
Log.w(TAG, "Failed to generate new session.", ex)
139+
}
134140
}
135141

136142
/**
@@ -149,10 +155,14 @@ internal class SessionLifecycleService : Service() {
149155
if (hasForegrounded) {
150156
sendSessionToClient(client, SessionGenerator.instance.currentSession.sessionId)
151157
} else {
152-
// Send the value from the datastore before the first foregrounding it exists
153-
val storedSession = SessionDatastore.instance.getCurrentSessionId()
154-
Log.d(TAG, "App has not yet foregrounded. Using previously stored session.")
155-
storedSession?.let { sendSessionToClient(client, it) }
158+
try {
159+
// Send the value from the datastore before the first foregrounding it exists
160+
val storedSession = SessionDatastore.instance.getCurrentSessionId()
161+
Log.d(TAG, "App has not yet foregrounded. Using previously stored session.")
162+
storedSession?.let { sendSessionToClient(client, it) }
163+
} catch (ex: IllegalStateException) {
164+
Log.w(TAG, "Failed to send session to client.", ex)
165+
}
156166
}
157167
}
158168

0 commit comments

Comments
 (0)