From 3457a764466e96554a4cea9c3040399b54477458 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 24 Jul 2023 15:01:48 +0200 Subject: [PATCH] getOrPut is not thread safe, so ensure that no multiple instance will be created per data store (#950) --- .../pushstore/impl/DefaultUserPushStoreFactory.kt | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/libraries/pushstore/impl/src/main/kotlin/io/element/android/libraries/pushstore/impl/DefaultUserPushStoreFactory.kt b/libraries/pushstore/impl/src/main/kotlin/io/element/android/libraries/pushstore/impl/DefaultUserPushStoreFactory.kt index a84fe2ea69..16100fd858 100644 --- a/libraries/pushstore/impl/src/main/kotlin/io/element/android/libraries/pushstore/impl/DefaultUserPushStoreFactory.kt +++ b/libraries/pushstore/impl/src/main/kotlin/io/element/android/libraries/pushstore/impl/DefaultUserPushStoreFactory.kt @@ -41,11 +41,13 @@ class DefaultUserPushStoreFactory @Inject constructor( // We can have only one class accessing a single data store, so keep a cache of them. private val cache = mutableMapOf() override fun create(userId: SessionId): UserPushStore { - return cache.getOrPut(userId) { - UserPushStoreDataStore( - context = context, - userId = userId - ) + return synchronized(cache) { + cache.getOrPut(userId) { + UserPushStoreDataStore( + context = context, + userId = userId + ) + } } }