Skip to content

Commit

Permalink
Try catch for datastore write (#5833)
Browse files Browse the repository at this point in the history
Proposed fix for issue #5802. Datastore crashes when writing from a
device that has full internal memory.

I did a manual test by creating a clean project and importing the part
of the code with the condition checkers to simulate the issue. I was
able to receive the issue stacktrace the developer was encountering and
was able to verify that the try catch condition mitigates the issue.
  • Loading branch information
argzdev committed Apr 17, 2024
1 parent 1d717ea commit ec9b699
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
2 changes: 2 additions & 0 deletions firebase-sessions/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

* [fixed] Handled datastore writes when device has full internal memory more gracefully.

# 1.2.3

* [fixed] Force validation or rotation of FIDs.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import androidx.datastore.preferences.preferencesDataStore
import com.google.firebase.Firebase
import com.google.firebase.app
import com.google.firebase.sessions.ProcessDetailsProvider.getProcessName
import java.io.IOException
import java.util.concurrent.atomic.AtomicReference
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -84,8 +85,15 @@ internal class SessionDatastoreImpl(

override fun updateSessionId(sessionId: String) {
CoroutineScope(backgroundDispatcher).launch {
context.dataStore.edit { preferences ->
preferences[FirebaseSessionDataKeys.SESSION_ID] = sessionId
try {
context.dataStore.edit { preferences ->
preferences[FirebaseSessionDataKeys.SESSION_ID] = sessionId
}
} catch (e: IOException) {
Log.w(
TAG,
"Failed to update session Id: $e",
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,16 @@ internal class SettingsCache(private val dataStore: DataStore<Preferences>) {

@VisibleForTesting
internal suspend fun removeConfigs() {
dataStore.edit { preferences ->
preferences.clear()
updateSessionConfigs(preferences)
try {
dataStore.edit { preferences ->
preferences.clear()
updateSessionConfigs(preferences)
}
} catch (e: IOException) {
Log.w(
TAG,
"Failed to remove config values: $e",
)
}
}

Expand Down

0 comments on commit ec9b699

Please sign in to comment.