Skip to content

Commit 3096428

Browse files
committed
fix backup looping same keys
1 parent 448b6e1 commit 3096428

File tree

4 files changed

+11
-33
lines changed

4 files changed

+11
-33
lines changed

matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/internal/crypto/keysbackup/KeysBackupTest.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import org.junit.Assert.assertNotNull
2424
import org.junit.Assert.assertNull
2525
import org.junit.Assert.assertTrue
2626
import org.junit.FixMethodOrder
27-
import org.junit.Ignore
2827
import org.junit.Rule
2928
import org.junit.Test
3029
import org.junit.runner.RunWith
@@ -56,7 +55,6 @@ import java.util.concurrent.CountDownLatch
5655
@RunWith(AndroidJUnit4::class)
5756
@FixMethodOrder(MethodSorters.JVM)
5857
@LargeTest
59-
@Ignore
6058
class KeysBackupTest : InstrumentedTest {
6159

6260
@get:Rule val rule = RetryTestRule(3)

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/InboundGroupSessionStore.kt

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@ import kotlinx.coroutines.CoroutineScope
2121
import kotlinx.coroutines.launch
2222
import kotlinx.coroutines.sync.Mutex
2323
import org.matrix.android.sdk.api.MatrixCoroutineDispatchers
24-
import org.matrix.android.sdk.api.extensions.tryOrNull
2524
import org.matrix.android.sdk.api.logger.LoggerTag
2625
import org.matrix.android.sdk.internal.crypto.model.MXInboundMegolmSessionWrapper
2726
import org.matrix.android.sdk.internal.crypto.store.IMXCryptoStore
2827
import timber.log.Timber
29-
import java.util.Timer
30-
import java.util.TimerTask
3128
import javax.inject.Inject
3229

3330
internal data class InboundGroupSessionHolder(
@@ -57,18 +54,13 @@ internal class InboundGroupSessionStore @Inject constructor(
5754
if (oldValue != null) {
5855
cryptoCoroutineScope.launch(coroutineDispatchers.crypto) {
5956
Timber.tag(loggerTag.value).v("## Inbound: entryRemoved ${oldValue.wrapper.roomId}-${oldValue.wrapper.senderKey}")
60-
store.storeInboundGroupSessions(listOf(oldValue).map { it.wrapper })
57+
// store.storeInboundGroupSessions(listOf(oldValue).map { it.wrapper })
6158
oldValue.wrapper.session.releaseSession()
6259
}
6360
}
6461
}
6562
}
6663

67-
private val timer = Timer()
68-
private var timerTask: TimerTask? = null
69-
70-
private val dirtySession = mutableListOf<InboundGroupSessionHolder>()
71-
7264
@Synchronized
7365
fun clear() {
7466
sessionCache.evictAll()
@@ -90,7 +82,6 @@ internal class InboundGroupSessionStore @Inject constructor(
9082
@Synchronized
9183
fun replaceGroupSession(old: InboundGroupSessionHolder, new: InboundGroupSessionHolder, sessionId: String, senderKey: String) {
9284
Timber.tag(loggerTag.value).v("## Replacing outdated session ${old.wrapper.roomId}-${old.wrapper.senderKey}")
93-
dirtySession.remove(old)
9485
store.removeInboundGroupSession(sessionId, senderKey)
9586
sessionCache.remove(CacheKey(sessionId, senderKey))
9687

@@ -107,33 +98,14 @@ internal class InboundGroupSessionStore @Inject constructor(
10798

10899
private fun internalStoreGroupSession(holder: InboundGroupSessionHolder, sessionId: String, senderKey: String) {
109100
Timber.tag(loggerTag.value).v("## Inbound: getInboundGroupSession mark as dirty ${holder.wrapper.roomId}-${holder.wrapper.senderKey}")
110-
// We want to batch this a bit for performances
111-
dirtySession.add(holder)
112101

113102
if (sessionCache[CacheKey(sessionId, senderKey)] == null) {
114103
// first time seen, put it in memory cache while waiting for batch insert
115104
// If it's already known, no need to update cache it's already there
116105
sessionCache.put(CacheKey(sessionId, senderKey), holder)
117106
}
118-
119-
timerTask?.cancel()
120-
timerTask = object : TimerTask() {
121-
override fun run() {
122-
batchSave()
123-
}
124-
}
125-
timer.schedule(timerTask!!, 300)
126-
}
127-
128-
@Synchronized
129-
private fun batchSave() {
130-
val toSave = mutableListOf<InboundGroupSessionHolder>().apply { addAll(dirtySession) }
131-
dirtySession.clear()
132107
cryptoCoroutineScope.launch(coroutineDispatchers.crypto) {
133-
Timber.tag(loggerTag.value).v("## Inbound: getInboundGroupSession batching save of ${toSave.size}")
134-
tryOrNull {
135-
store.storeInboundGroupSessions(toSave.map { it.wrapper })
136-
}
108+
store.storeInboundGroupSessions(listOf(holder.wrapper))
137109
}
138110
}
139111
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/keysbackup/DefaultKeysBackupService.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,8 @@ internal class DefaultKeysBackupService @Inject constructor(
13491349

13501350
// Mark keys as backed up
13511351
cryptoStore.markBackupDoneForInboundGroupSessions(olmInboundGroupSessionWrappers)
1352+
// we can release the sessions now
1353+
olmInboundGroupSessionWrappers.onEach { it.session.releaseSession() }
13521354

13531355
if (olmInboundGroupSessionWrappers.size < KEY_BACKUP_SEND_KEYS_MAX_COUNT) {
13541356
Timber.v("backupKeys: All keys have been backed up")

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,11 +763,17 @@ internal class RealmCryptoStore @Inject constructor(
763763
// } ?: false
764764
val key = OlmInboundGroupSessionEntity.createPrimaryKey(sessionIdentifier, wrapper.sessionData.senderKey)
765765

766+
val existing = realm.where<OlmInboundGroupSessionEntity>()
767+
.equalTo(OlmInboundGroupSessionEntityFields.PRIMARY_KEY, key)
768+
.findFirst()
769+
766770
val realmOlmInboundGroupSession = OlmInboundGroupSessionEntity().apply {
767771
primaryKey = key
768772
store(wrapper)
773+
backedUp = existing?.backedUp ?: false
769774
}
770-
Timber.i("## CRYPTO | shouldShareHistory: ${wrapper.sessionData.sharedHistory} for $key")
775+
776+
Timber.v("## CRYPTO | shouldShareHistory: ${wrapper.sessionData.sharedHistory} for $key")
771777
realm.insertOrUpdate(realmOlmInboundGroupSession)
772778
}
773779
}

0 commit comments

Comments
 (0)