Fix potential lock due to nested `synchronized(unknownSessionsFailure)`

This commit is contained in:
Benoit Marty 2019-10-22 14:20:43 +02:00
parent 3abce34484
commit 78dfd6b3e6
1 changed files with 15 additions and 12 deletions

View File

@ -38,18 +38,17 @@ internal class TimelineEventDecryptor(
private val newSessionListener = object : NewSessionListener {
override fun onNewSession(roomId: String?, senderKey: String, sessionId: String) {
synchronized(unknownSessionsFailure) {
val toDecryptAgain = ArrayList<String>()
val eventIds = unknownSessionsFailure[sessionId]
if (eventIds != null) toDecryptAgain.addAll(eventIds)
if (toDecryptAgain.isNotEmpty()) {
eventIds?.clear()
toDecryptAgain.forEach {
unknownSessionsFailure[sessionId]
.orEmpty()
.toList()
.also {
unknownSessionsFailure[sessionId]?.clear()
}
}.forEach {
requestDecryption(it)
}
}
}
}
}
private var executor: ExecutorService? = null
@ -67,9 +66,13 @@ internal class TimelineEventDecryptor(
cryptoService.removeSessionListener(newSessionListener)
executor?.shutdownNow()
executor = null
synchronized(unknownSessionsFailure) {
unknownSessionsFailure.clear()
}
synchronized(existingRequests) {
existingRequests.clear()
}
}
fun requestDecryption(eventId: String) {
synchronized(unknownSessionsFailure) {
@ -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 {