use mutex on suspend and not synchronized

This commit is contained in:
Valere 2022-03-01 19:16:25 +01:00
parent 49d33f3a4b
commit 6546f98858
1 changed files with 69 additions and 65 deletions

View File

@ -16,6 +16,8 @@
package org.matrix.android.sdk.internal.crypto.actions package org.matrix.android.sdk.internal.crypto.actions
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.matrix.android.sdk.api.MatrixCoroutineDispatchers import org.matrix.android.sdk.api.MatrixCoroutineDispatchers
import org.matrix.android.sdk.api.logger.LoggerTag import org.matrix.android.sdk.api.logger.LoggerTag
@ -39,16 +41,18 @@ internal class EnsureOlmSessionsForDevicesAction @Inject constructor(
private val coroutineDispatchers: MatrixCoroutineDispatchers, private val coroutineDispatchers: MatrixCoroutineDispatchers,
private val oneTimeKeysForUsersDeviceTask: ClaimOneTimeKeysForUsersDeviceTask) { private val oneTimeKeysForUsersDeviceTask: ClaimOneTimeKeysForUsersDeviceTask) {
private val ensureMutex = Mutex()
/** /**
* We want to synchronize a bit here, because we are iterating to check existing olm session and * We want to synchronize a bit here, because we are iterating to check existing olm session and
* also adding some * also adding some
*/ */
@Synchronized
suspend fun handle(devicesByUser: Map<String, List<CryptoDeviceInfo>>, force: Boolean = false): MXUsersDevicesMap<MXOlmSessionResult> { suspend fun handle(devicesByUser: Map<String, List<CryptoDeviceInfo>>, force: Boolean = false): MXUsersDevicesMap<MXOlmSessionResult> {
ensureMutex.withLock {
val results = MXUsersDevicesMap<MXOlmSessionResult>()
val deviceList = devicesByUser.flatMap { it.value } val deviceList = devicesByUser.flatMap { it.value }
Timber.tag(loggerTag.value) Timber.tag(loggerTag.value)
.d("ensure olm forced:$force for ${deviceList.joinToString { it.shortDebugString() }}") .d("ensure olm forced:$force for ${deviceList.joinToString { it.shortDebugString() }}")
val results = MXUsersDevicesMap<MXOlmSessionResult>()
val devicesToCreateSessionWith = mutableListOf<CryptoDeviceInfo>() val devicesToCreateSessionWith = mutableListOf<CryptoDeviceInfo>()
if (force) { if (force) {
// we take all devices and will query otk for them // we take all devices and will query otk for them
@ -113,9 +117,9 @@ internal class EnsureOlmSessionsForDevicesAction @Inject constructor(
} }
} }
} }
return results return results
} }
}
private fun verifyKeyAndStartSession(oneTimeKey: MXKey, userId: String, deviceInfo: CryptoDeviceInfo): String? { private fun verifyKeyAndStartSession(oneTimeKey: MXKey, userId: String, deviceInfo: CryptoDeviceInfo): String? {
var sessionId: String? = null var sessionId: String? = null