From 78dfd6b3e622a4e155bb83b607716fdac6070b88 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 22 Oct 2019 14:20:43 +0200 Subject: [PATCH] Fix potential lock due to nested `synchronized(unknownSessionsFailure)` --- .../room/timeline/TimelineEventDecryptor.kt | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TimelineEventDecryptor.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TimelineEventDecryptor.kt index b8be58779c..62ae5f621b 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TimelineEventDecryptor.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TimelineEventDecryptor.kt @@ -38,15 +38,14 @@ internal class TimelineEventDecryptor( private val newSessionListener = object : NewSessionListener { override fun onNewSession(roomId: String?, senderKey: String, sessionId: String) { synchronized(unknownSessionsFailure) { - val toDecryptAgain = ArrayList() - val eventIds = unknownSessionsFailure[sessionId] - if (eventIds != null) toDecryptAgain.addAll(eventIds) - if (toDecryptAgain.isNotEmpty()) { - eventIds?.clear() - toDecryptAgain.forEach { - requestDecryption(it) - } - } + unknownSessionsFailure[sessionId] + .orEmpty() + .toList() + .also { + unknownSessionsFailure[sessionId]?.clear() + } + }.forEach { + requestDecryption(it) } } } @@ -67,8 +66,12 @@ internal class TimelineEventDecryptor( cryptoService.removeSessionListener(newSessionListener) executor?.shutdownNow() executor = null - unknownSessionsFailure.clear() - existingRequests.clear() + synchronized(unknownSessionsFailure) { + unknownSessionsFailure.clear() + } + synchronized(existingRequests) { + existingRequests.clear() + } } fun requestDecryption(eventId: String) { @@ -108,7 +111,7 @@ internal class TimelineEventDecryptor( eventEntity.setDecryptionResult(result) } } catch (e: MXCryptoError) { - Timber.v("Failed to decrypt event $eventId $e") + Timber.v(e, "Failed to decrypt event $eventId") if (e is MXCryptoError.Base && e.errorType == MXCryptoError.ErrorType.UNKNOWN_INBOUND_SESSION_ID) { // Keep track of unknown sessions to automatically try to decrypt on new session realm.executeTransaction {