diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/ThreadSummaryHelper.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/ThreadSummaryHelper.kt index 8d9304f61b..bfe310cd8b 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/ThreadSummaryHelper.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/ThreadSummaryHelper.kt @@ -213,6 +213,7 @@ internal fun ThreadSummaryEntity.Companion.createOrUpdate( } } +// note: runBlocking should be used here while we are in realm single thread executor, to avoid thread switching private fun decryptIfNeeded(cryptoService: CryptoService?, eventEntity: EventEntity, roomId: String) = runBlocking { cryptoService ?: return@runBlocking val event = eventEntity.asDomain() diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TimelineEventDecryptor.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TimelineEventDecryptor.kt index 3ddd877b78..e225b31f45 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TimelineEventDecryptor.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TimelineEventDecryptor.kt @@ -101,9 +101,7 @@ internal class TimelineEventDecryptor @Inject constructor( executor?.execute { Realm.getInstance(realmConfiguration).use { realm -> try { - runBlocking { - processDecryptRequest(request, realm) - } + processDecryptRequest(request, realm) } catch (e: InterruptedException) { Timber.i("Decryption got interrupted") } @@ -113,16 +111,18 @@ internal class TimelineEventDecryptor @Inject constructor( private fun threadAwareNonEncryptedEvents(request: DecryptionRequest, realm: Realm) { val event = request.event - realm.executeTransaction { - val eventId = event.eventId ?: return@executeTransaction - val eventEntity = EventEntity - .where(it, eventId = eventId) - .findFirst() - val decryptedEvent = eventEntity?.asDomain() - threadsAwarenessHandler.makeEventThreadAware(realm, event.roomId, decryptedEvent, eventEntity) + realm.executeTransaction { + val eventId = event.eventId ?: return@executeTransaction + val eventEntity = EventEntity + .where(it, eventId = eventId) + .findFirst() + val decryptedEvent = eventEntity?.asDomain() + threadsAwarenessHandler.makeEventThreadAware(realm, event.roomId, decryptedEvent, eventEntity) } } - private suspend fun processDecryptRequest(request: DecryptionRequest, realm: Realm) { + + // note: runBlocking should be used here while we are in realm single thread executor, to avoid thread switching + private fun processDecryptRequest(request: DecryptionRequest, realm: Realm) = runBlocking { val event = request.event val timelineId = request.timelineId @@ -130,7 +130,7 @@ internal class TimelineEventDecryptor @Inject constructor( // Here we have requested a decryption to an event that is not encrypted // We will simply make this event thread aware threadAwareNonEncryptedEvents(request, realm) - return + return@runBlocking } try { val result = cryptoService.decryptEvent(request.event, timelineId) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/handler/room/RoomSyncHandler.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/handler/room/RoomSyncHandler.kt index 51e32b25cb..e488fd0671 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/handler/room/RoomSyncHandler.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/handler/room/RoomSyncHandler.kt @@ -506,6 +506,7 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle } } + // note: runBlocking should be used here while we are in realm single thread executor, to avoid thread switching private fun decryptIfNeeded(event: Event, roomId: String) = runBlocking { try { // Event from sync does not have roomId, so add it to the event first