fix olm session proliferation
This commit is contained in:
parent
c7b54b8d3d
commit
e8bd7ea967
|
@ -0,0 +1 @@
|
||||||
|
Unwedging could cause the SDK to force creating a new olm session every hour
|
|
@ -60,7 +60,6 @@ import kotlin.coroutines.resume
|
||||||
*/
|
*/
|
||||||
@RunWith(AndroidJUnit4::class)
|
@RunWith(AndroidJUnit4::class)
|
||||||
@FixMethodOrder(MethodSorters.JVM)
|
@FixMethodOrder(MethodSorters.JVM)
|
||||||
@Ignore
|
|
||||||
class UnwedgingTest : InstrumentedTest {
|
class UnwedgingTest : InstrumentedTest {
|
||||||
|
|
||||||
private lateinit var messagesReceivedByBob: List<TimelineEvent>
|
private lateinit var messagesReceivedByBob: List<TimelineEvent>
|
||||||
|
|
|
@ -18,6 +18,8 @@ package org.matrix.android.sdk.internal.crypto
|
||||||
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import org.matrix.android.sdk.api.MatrixCallback
|
import org.matrix.android.sdk.api.MatrixCallback
|
||||||
import org.matrix.android.sdk.api.MatrixCoroutineDispatchers
|
import org.matrix.android.sdk.api.MatrixCoroutineDispatchers
|
||||||
|
@ -68,6 +70,7 @@ internal class EventDecryptor @Inject constructor(
|
||||||
val senderKey: String?
|
val senderKey: String?
|
||||||
)
|
)
|
||||||
|
|
||||||
|
private val wedgedMutex = Mutex()
|
||||||
private val wedgedDevices = mutableListOf<WedgedDeviceInfo>()
|
private val wedgedDevices = mutableListOf<WedgedDeviceInfo>()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -151,11 +154,13 @@ internal class EventDecryptor @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun markOlmSessionForUnwedging(senderId: String, senderKey: String) {
|
private suspend fun markOlmSessionForUnwedging(senderId: String, senderKey: String) {
|
||||||
val info = WedgedDeviceInfo(senderId, senderKey)
|
wedgedMutex.withLock {
|
||||||
if (!wedgedDevices.contains(info)) {
|
val info = WedgedDeviceInfo(senderId, senderKey)
|
||||||
Timber.tag(loggerTag.value).d("Marking device from $senderId key:$senderKey as wedged")
|
if (!wedgedDevices.contains(info)) {
|
||||||
wedgedDevices.add(info)
|
Timber.tag(loggerTag.value).d("Marking device from $senderId key:$senderKey as wedged")
|
||||||
|
wedgedDevices.add(info)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -167,15 +172,17 @@ internal class EventDecryptor @Inject constructor(
|
||||||
Timber.tag(loggerTag.value).v("Unwedging: ${wedgedDevices.size} are wedged")
|
Timber.tag(loggerTag.value).v("Unwedging: ${wedgedDevices.size} are wedged")
|
||||||
// get the one that should be retried according to rate limit
|
// get the one that should be retried according to rate limit
|
||||||
val now = clock.epochMillis()
|
val now = clock.epochMillis()
|
||||||
val toUnwedge = wedgedDevices.filter {
|
val toUnwedge = wedgedMutex.withLock {
|
||||||
val lastForcedDate = lastNewSessionForcedDates[it] ?: 0
|
wedgedDevices.filter {
|
||||||
if (now - lastForcedDate < DefaultCryptoService.CRYPTO_MIN_FORCE_SESSION_PERIOD_MILLIS) {
|
val lastForcedDate = lastNewSessionForcedDates[it] ?: 0
|
||||||
Timber.tag(loggerTag.value).d("Unwedging, New session for $it already forced with device at $lastForcedDate")
|
if (now - lastForcedDate < DefaultCryptoService.CRYPTO_MIN_FORCE_SESSION_PERIOD_MILLIS) {
|
||||||
return@filter false
|
Timber.tag(loggerTag.value).d("Unwedging, New session for $it already forced with device at $lastForcedDate")
|
||||||
|
return@filter false
|
||||||
|
}
|
||||||
|
// let's already mark that we tried now
|
||||||
|
lastNewSessionForcedDates[it] = now
|
||||||
|
true
|
||||||
}
|
}
|
||||||
// let's already mark that we tried now
|
|
||||||
lastNewSessionForcedDates[it] = now
|
|
||||||
true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (toUnwedge.isEmpty()) {
|
if (toUnwedge.isEmpty()) {
|
||||||
|
@ -230,6 +237,15 @@ internal class EventDecryptor @Inject constructor(
|
||||||
withContext(coroutineDispatchers.io) {
|
withContext(coroutineDispatchers.io) {
|
||||||
sendToDeviceTask.executeRetry(sendToDeviceParams, remainingRetry = SEND_TO_DEVICE_RETRY_COUNT)
|
sendToDeviceTask.executeRetry(sendToDeviceParams, remainingRetry = SEND_TO_DEVICE_RETRY_COUNT)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
deviceList.values.flatten().forEach { deviceInfo ->
|
||||||
|
wedgedMutex.withLock {
|
||||||
|
wedgedDevices.removeAll {
|
||||||
|
it.senderKey == deviceInfo.identityKey() &&
|
||||||
|
it.userId == deviceInfo.userId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (failure: Throwable) {
|
} catch (failure: Throwable) {
|
||||||
deviceList.flatMap { it.value }.joinToString { it.shortDebugString() }.let {
|
deviceList.flatMap { it.value }.joinToString { it.shortDebugString() }.let {
|
||||||
Timber.tag(loggerTag.value).e(failure, "## Failed to unwedge devices: $it}")
|
Timber.tag(loggerTag.value).e(failure, "## Failed to unwedge devices: $it}")
|
||||||
|
|
Loading…
Reference in New Issue