Ensure no async transaction will occurs if the store is closed

This commit is contained in:
Benoit Marty 2021-10-12 12:08:14 +02:00
parent 4a7e0a5d95
commit 2d97640372
1 changed files with 8 additions and 0 deletions

View File

@ -100,6 +100,7 @@ import org.matrix.olm.OlmAccount
import org.matrix.olm.OlmException import org.matrix.olm.OlmException
import org.matrix.olm.OlmOutboundGroupSession import org.matrix.olm.OlmOutboundGroupSession
import timber.log.Timber import timber.log.Timber
import java.util.concurrent.Executors
import javax.inject.Inject import javax.inject.Inject
import kotlin.collections.set import kotlin.collections.set
@ -137,8 +138,11 @@ internal class RealmCryptoStore @Inject constructor(
newSessionListeners.remove(listener) newSessionListeners.remove(listener)
} }
private val monarchyWriteAsyncExecutor = Executors.newSingleThreadExecutor()
private val monarchy = Monarchy.Builder() private val monarchy = Monarchy.Builder()
.setRealmConfiguration(realmConfiguration) .setRealmConfiguration(realmConfiguration)
.setWriteAsyncExecutor(monarchyWriteAsyncExecutor)
.build() .build()
init { init {
@ -199,6 +203,10 @@ internal class RealmCryptoStore @Inject constructor(
} }
override fun close() { override fun close() {
// Ensure no async request will be run later
val tasks = monarchyWriteAsyncExecutor.shutdownNow()
Timber.w("Closing RealmCryptoStore, ${tasks.size} async task(s) cancelled")
olmSessionsToRelease.forEach { olmSessionsToRelease.forEach {
it.value.olmSession.releaseSession() it.value.olmSession.releaseSession()
} }