From 0437d37f232e210b572b51c076ac2b75cf7ff574 Mon Sep 17 00:00:00 2001 From: Clark Date: Thu, 15 Jun 2023 09:50:22 -0400 Subject: [PATCH] Fix pending retry receipt cache deadlock. --- .../securesms/database/PendingRetryReceiptCache.kt | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/PendingRetryReceiptCache.kt b/app/src/main/java/org/thoughtcrime/securesms/database/PendingRetryReceiptCache.kt index 7ac9a56d40..65b6f7dc57 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/PendingRetryReceiptCache.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/PendingRetryReceiptCache.kt @@ -23,10 +23,15 @@ class PendingRetryReceiptCache @VisibleForTesting constructor( fun insert(author: RecipientId, authorDevice: Int, sentTimestamp: Long, receivedTimestamp: Long, threadId: Long) { if (!FeatureFlags.retryReceipts()) return ensurePopulated() - + val model: PendingRetryReceiptModel = database.insert(author, authorDevice, sentTimestamp, receivedTimestamp, threadId) synchronized(pendingRetries) { - val model: PendingRetryReceiptModel = database.insert(author, authorDevice, sentTimestamp, receivedTimestamp, threadId) - pendingRetries[RemoteMessageId(author, sentTimestamp)] = model + val key = RemoteMessageId(author, sentTimestamp) + val existing: PendingRetryReceiptModel? = pendingRetries[key] + + // We rely on db unique constraint and auto-incrementing ids for conflict resolution here. + if (existing == null || existing.id < model.id) { + pendingRetries[key] = model + } } } @@ -54,8 +59,8 @@ class PendingRetryReceiptCache @VisibleForTesting constructor( synchronized(pendingRetries) { pendingRetries.remove(RemoteMessageId(model.author, model.sentTimestamp)) - database.delete(model) } + database.delete(model) } fun clear() {