From 084497c881864d87d3b0f083d518c1e1b628d39b Mon Sep 17 00:00:00 2001 From: Morgan Pretty Date: Thu, 12 Nov 2026 14:35:23 +1100 Subject: [PATCH 1/4] Let a message send job report progress as syncing A send job always marked its message as sending, so a job that only copies an already-delivered message to our own swarm would show it as unsent again. The job now carries whether it is such a sync, persisted with it so a resumed job keeps it; jobs persisted without it are ordinary sends. --- .../messaging/jobs/MessageSendJob.kt | 18 +++++++++++++++--- .../libsession/messaging/jobs/JobKryoTest.kt | 3 ++- .../messaging/jobs/MessageSendJobTest.kt | 1 + 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/session/libsession/messaging/jobs/MessageSendJob.kt b/app/src/main/java/org/session/libsession/messaging/jobs/MessageSendJob.kt index 65e5bf927b..bcfd0f0f3d 100644 --- a/app/src/main/java/org/session/libsession/messaging/jobs/MessageSendJob.kt +++ b/app/src/main/java/org/session/libsession/messaging/jobs/MessageSendJob.kt @@ -33,6 +33,12 @@ class MessageSendJob @AssistedInject constructor( @Assisted val message: Message, @Assisted val destination: Destination, @Assisted val statusCallback: SendChannel>?, + /** + * This job copies a message that has already reached its recipient to our own swarm, so its + * progress is reported as syncing rather than sending. Note to Self also sends to our own swarm + * but is a first send, which is why this can't be inferred from [destination]. + */ + @Assisted val isSyncOfSentMessage: Boolean, private val attachmentUploadJobFactory: AttachmentUploadJob.Factory, private val messageDataProvider: MessageDataProvider, private val storage: StorageProtocol, @@ -56,6 +62,7 @@ class MessageSendJob @AssistedInject constructor( // Keys used for database storage private const val MESSAGE_KEY = "message" private const val DESTINATION_KEY = "destination" + private const val SYNC_OF_SENT_MESSAGE_KEY = "sync_of_sent_message" } override suspend fun execute(dispatcherName: String) { @@ -67,7 +74,9 @@ class MessageSendJob @AssistedInject constructor( return } - messageId?.let(storage::markAsSending) + if (messageId != null) { + if (isSyncOfSentMessage) storage.markAsSyncing(messageId) else storage.markAsSending(messageId) + } if (message != null) { val isOutgoing = messageId != null && messageDataProvider.isOutgoingMessage(messageId) @@ -177,6 +186,7 @@ class MessageSendJob @AssistedInject constructor( return Data.Builder() .putByteArray(MESSAGE_KEY, serializedMessage) .putByteArray(DESTINATION_KEY, serializedDestination) + .putBoolean(SYNC_OF_SENT_MESSAGE_KEY, isSyncOfSentMessage) .build() } @@ -190,7 +200,8 @@ class MessageSendJob @AssistedInject constructor( abstract fun create( message: Message, destination: Destination, - statusCallback: SendChannel>? = null + statusCallback: SendChannel>? = null, + isSyncOfSentMessage: Boolean = false, ): MessageSendJob override fun create(data: Data): MessageSendJob? { @@ -221,7 +232,8 @@ class MessageSendJob @AssistedInject constructor( return create( message = message, destination = destination, - statusCallback = null + statusCallback = null, + isSyncOfSentMessage = data.getBooleanOrDefault(SYNC_OF_SENT_MESSAGE_KEY, false), ) } } diff --git a/app/src/test/java/org/session/libsession/messaging/jobs/JobKryoTest.kt b/app/src/test/java/org/session/libsession/messaging/jobs/JobKryoTest.kt index 3ce8e83877..360ceffc7d 100644 --- a/app/src/test/java/org/session/libsession/messaging/jobs/JobKryoTest.kt +++ b/app/src/test/java/org/session/libsession/messaging/jobs/JobKryoTest.kt @@ -48,7 +48,7 @@ class JobKryoTest { val restored = slot() val factory = mockk { every { create(any()) } answers { callOriginal() } - every { create(capture(restored), any(), any()) } returns mockk(relaxed = true) + every { create(capture(restored), any(), any(), any()) } returns mockk(relaxed = true) } assertNotNull(factory.create(sendJob(message).serialize())) @@ -59,6 +59,7 @@ class JobKryoTest { message = message, destination = Destination.ClosedGroup("03${"11".repeat(32)}"), statusCallback = null, + isSyncOfSentMessage = false, attachmentUploadJobFactory = mockk(relaxed = true), messageDataProvider = mockk(relaxed = true), storage = mockk(relaxed = true), diff --git a/app/src/test/java/org/session/libsession/messaging/jobs/MessageSendJobTest.kt b/app/src/test/java/org/session/libsession/messaging/jobs/MessageSendJobTest.kt index 3c60c86097..8bc189a342 100644 --- a/app/src/test/java/org/session/libsession/messaging/jobs/MessageSendJobTest.kt +++ b/app/src/test/java/org/session/libsession/messaging/jobs/MessageSendJobTest.kt @@ -103,6 +103,7 @@ class MessageSendJobTest { ), destination = Destination.ClosedGroup(groupId), statusCallback = statusChannel, + isSyncOfSentMessage = false, attachmentUploadJobFactory = mockk(relaxed = true), messageDataProvider = mockk(relaxed = true), storage = mockk(relaxed = true), From a58e2c120d6c5ec7de9b9b7d648d6d49c81b2a6e Mon Sep 17 00:00:00 2001 From: Morgan Pretty Date: Thu, 12 Nov 2026 14:35:23 +1100 Subject: [PATCH 2/4] Retry a 1:1 message's sync to our own swarm through the job queue After a 1:1 message reached its recipient, the copy to our own swarm was a one-shot coroutine. Any transient failure left the delivered message showing "Failed to sync" permanently: nothing retried it, and a restart couldn't either because there was no job to resume. A stored message's sync is now a send job with the same retry and backoff as any send, and is resumed after a restart. Messages with no stored record, such as call signalling, keep the best-effort sync. --- .../sending_receiving/MessageSender.kt | 25 +- .../sending_receiving/SyncToSelfTest.kt | 240 ++++++++++++++++++ 2 files changed, 264 insertions(+), 1 deletion(-) create mode 100644 app/src/test/java/org/session/libsession/messaging/sending_receiving/SyncToSelfTest.kt diff --git a/app/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt b/app/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt index dfcd0f81fc..14725184c8 100644 --- a/app/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt +++ b/app/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt @@ -1,5 +1,6 @@ package org.session.libsession.messaging.sending_receiving +import androidx.annotation.VisibleForTesting import com.google.protobuf.ByteString import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.channels.Channel @@ -512,7 +513,29 @@ class MessageSender @Inject constructor( if (message is VisibleMessage) message.syncTarget = destination.publicKey if (message is ExpirationTimerUpdate) message.syncTarget = destination.publicKey - message.id?.let(storage::markAsSyncing) + scheduleSyncToSelf(message, userPublicKey) + } + } + + /** + * A stored message's sync must be a persisted job rather than a coroutine. The message has + * already reached its recipient, so a failed sync leaves it showing "Failed to sync", and only a + * job is retried and resumed after a restart. A message with no stored record has no status to get + * stuck, and includes call signalling that must not be replayed late, so its sync stays best-effort. + */ + @VisibleForTesting + internal fun scheduleSyncToSelf(message: Message, userPublicKey: String) { + val messageId = message.id + if (messageId != null) { + storage.markAsSyncing(messageId) + jobQueue.get().add( + messageSendJobFactory.create( + message = message, + destination = Destination.Contact(userPublicKey), + isSyncOfSentMessage = true, + ) + ) + } else { scope.launch { try { sendToSnodeDestination(Destination.Contact(userPublicKey), message, true) diff --git a/app/src/test/java/org/session/libsession/messaging/sending_receiving/SyncToSelfTest.kt b/app/src/test/java/org/session/libsession/messaging/sending_receiving/SyncToSelfTest.kt new file mode 100644 index 0000000000..bc34df6af4 --- /dev/null +++ b/app/src/test/java/org/session/libsession/messaging/sending_receiving/SyncToSelfTest.kt @@ -0,0 +1,240 @@ +package org.session.libsession.messaging.sending_receiving + +import io.mockk.coEvery +import io.mockk.every +import io.mockk.mockk +import io.mockk.slot +import io.mockk.verify +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.cancel +import kotlinx.coroutines.channels.SendChannel +import kotlinx.coroutines.test.StandardTestDispatcher +import kotlinx.coroutines.test.TestScope +import kotlinx.coroutines.test.advanceUntilIdle +import kotlinx.coroutines.test.runTest +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Before +import org.junit.Test +import org.session.libsession.database.MessageDataProvider +import org.session.libsession.messaging.jobs.Job +import org.session.libsession.messaging.jobs.JobQueue +import org.session.libsession.messaging.jobs.MessageSendJob +import org.session.libsession.messaging.messages.Destination +import org.session.libsession.messaging.messages.Message +import org.session.libsession.messaging.messages.visible.VisibleMessage +import org.session.libsignal.utilities.Log +import org.thoughtcrime.securesms.NoOpLogger +import org.thoughtcrime.securesms.database.Storage +import org.thoughtcrime.securesms.database.model.MessageId +import java.io.IOException +import javax.inject.Provider + +/** + * The sync leg of a 1:1 send: after a message reaches its recipient, a copy goes to our own swarm. + * + * Encoding a message for the network needs libsession's native library, which JVM tests can't load, + * so the network fake sits at [MessageSender.sendNonDurably]. It writes the statuses + * [MessageSender] itself writes for a sync send: SYNC_FAILED when the send throws, SENT when it lands. + * Everything between that and [MessageSender.scheduleSyncToSelf] is the real job queue. + */ +class SyncToSelfTest { + private val self = "05" + "aa".repeat(32) + private val contact = "05" + "bb".repeat(32) + private val messageId = MessageId(42, false) + + /** Every status written to the message, in order. */ + private val statuses = mutableListOf() + + private val storage = mockk(relaxed = true) { + every { getUserPublicKey() } returns self + every { isJobCanceled(any()) } returns false + every { getAllPendingJobs(*anyVararg()) } returns emptyMap() + every { markAsSyncing(any()) } answers { statuses += SYNCING } + every { markAsSending(any()) } answers { statuses += SENDING } + every { markAsSyncFailed(any(), any()) } answers { statuses += SYNC_FAILED } + every { markAsSent(any()) } answers { statuses += SENT } + } + + private val messageDataProvider = mockk(relaxed = true) { + every { isDeletedMessage(any()) } returns false + every { isOutgoingMessage(any()) } returns true + } + + /** Each send that reached the fake network: its destination and whether it was a sync. */ + private val sends = mutableListOf>() + + @Before + fun setUp() { + Log.initialize(NoOpLogger) + } + + /** The fake network every sync job sends through. */ + private var network: MessageSender = mockk(relaxed = true) + + @Test + fun `a sync that fails transiently is retried until the message is sent`() = runTest { + network = network(failuresBeforeSuccess = 3) + val queue = JobQueue(queueScope(), storage) + + messageSender(queue).scheduleSyncToSelf(storedMessage(), self) + advanceUntilIdle() + + assertEquals(List(4) { Destination.Contact(self) to true }, sends) + assertEquals( + listOf( + SYNCING, + SYNCING, SYNC_FAILED, + SYNCING, SYNC_FAILED, + SYNCING, SYNC_FAILED, + SYNCING, SENT, + ), + statuses + ) + verify(exactly = 1) { storage.markJobAsSucceeded(any()) } + } + + @Test + fun `a sync that never succeeds gives up after the normal send retry limit`() = runTest { + network = network(failuresBeforeSuccess = Int.MAX_VALUE) + val queue = JobQueue(queueScope(), storage) + + messageSender(queue).scheduleSyncToSelf(storedMessage(), self) + advanceUntilIdle() + + assertEquals(10, sends.size) + assertEquals(SYNC_FAILED, statuses.last()) + verify(exactly = 1) { storage.markJobAsFailedPermanently(any()) } + } + + @Test + fun `a sync left pending across a restart is resumed and sent`() = runTest { + network = network(failuresBeforeSuccess = 0) + + // Before the restart: the sync is scheduled and persisted, and the process dies before it runs + val scheduled = slot() + val deadQueue = mockk(relaxed = true) { + every { add(capture(scheduled)) } returns Unit + } + messageSender(deadQueue).scheduleSyncToSelf(storedMessage(), self) + val persisted = scheduled.captured.serialize() + statuses.clear() + + // After the restart: a fresh queue reads the job back from storage + val restored = jobFactory.create(persisted)!!.apply { id = "1" } + every { storage.getAllPendingJobs(MessageSendJob.KEY) } returns mapOf("1" to restored) + + JobQueue(queueScope(), storage).resumePendingJobs() + advanceUntilIdle() + + assertEquals(listOf(Destination.Contact(self) to true), sends) + assertEquals(listOf(SYNCING, SENT), statuses) + } + + @Test + fun `only a message with a stored record gets a durable sync`() { + val queue = mockk(relaxed = true) + val sender = messageSender(queue) + + sender.scheduleSyncToSelf(VisibleMessage(syncTarget = contact, text = "hi"), self) + verify(exactly = 0) { queue.add(any()) } + + sender.scheduleSyncToSelf(storedMessage(), self) + verify(exactly = 1) { queue.add(any()) } + } + + /** Note to Self is also a send to our own swarm, but a first send, so it must still read as sending. */ + @Test + fun `a Note to Self send is reported as sending, a sync as syncing`() = runTest { + network = network(failuresBeforeSuccess = 0) + + jobFactory.create(storedMessage(), Destination.Contact(self), isSyncOfSentMessage = false).execute("test") + jobFactory.create(storedMessage(), Destination.Contact(self), isSyncOfSentMessage = true).execute("test") + + assertEquals(listOf(SENDING, SENT, SYNCING, SENT), statuses) + } + + private fun storedMessage() = VisibleMessage(syncTarget = contact, text = "hi").apply { + id = messageId + sentTimestamp = 1_700_000_000_000L + threadID = 7L + } + + // advanceUntilIdle() leaves backgroundScope work unrun, and the queue's dispatch loop never ends, + // so it gets a foreground scope on the test's scheduler that is cancelled when the test finishes + private fun TestScope.queueScope(): CoroutineScope { + val scope = CoroutineScope(StandardTestDispatcher(testScheduler) + SupervisorJob()) + queueScopes += scope + return scope + } + + private val queueScopes = mutableListOf() + + @After + fun tearDown() { + queueScopes.forEach { it.cancel() } + } + + /** Fails the first [failuresBeforeSuccess] sends, then succeeds. */ + private fun network(failuresBeforeSuccess: Int): MessageSender = mockk { + coEvery { sendNonDurably(any(), any(), any()) } coAnswers { + val message = firstArg() + sends += secondArg() to thirdArg() + if (sends.size <= failuresBeforeSuccess) { + val error = IOException("snode unreachable") + storage.markAsSyncFailed(message.id!!, error) + throw error + } + storage.markAsSent(message.id!!) + } + } + + private val jobFactory = object : MessageSendJob.Factory() { + override fun create( + message: Message, + destination: Destination, + statusCallback: SendChannel>?, + isSyncOfSentMessage: Boolean, + ) = MessageSendJob( + message = message, + destination = destination, + statusCallback = statusCallback, + isSyncOfSentMessage = isSyncOfSentMessage, + attachmentUploadJobFactory = mockk(relaxed = true), + messageDataProvider = messageDataProvider, + storage = storage, + configFactory = mockk(relaxed = true), + messageSender = network, + jobQueue = mockk(relaxed = true), + ) + } + + // The scope only runs the best-effort sync for a message with no stored record. It is never + // advanced, so that sync never reaches the (native) encoder. + private fun messageSender(queue: JobQueue) = MessageSender( + storage = storage, + configFactory = mockk(relaxed = true), + recipientRepository = mockk(relaxed = true), + messageDataProvider = messageDataProvider, + messageSendJobFactory = jobFactory, + messageExpirationManager = mockk(relaxed = true), + snodeClock = mockk(relaxed = true), + communityApiExecutor = mockk(relaxed = true), + sendCommunityMessageApiFactory = mockk(relaxed = true), + sendCommunityDirectMessageApiFactory = mockk(relaxed = true), + swarmApiExecutor = mockk(relaxed = true), + storeSnodeMessageApiFactory = mockk(relaxed = true), + scope = CoroutineScope(StandardTestDispatcher()), + loginStateRepository = mockk(relaxed = true), + jobQueue = Provider { queue }, + proStatusManager = Provider { mockk(relaxed = true) }, + ) + + private companion object { + const val SENDING = "SENDING" + const val SYNCING = "SYNCING" + const val SYNC_FAILED = "SYNC_FAILED" + const val SENT = "SENT" + } +} From e3e9fa8ce640cc2eb327a83617f9ac7b901d6076 Mon Sep 17 00:00:00 2001 From: Morgan Pretty Date: Wed, 18 Nov 2026 09:17:19 +1100 Subject: [PATCH 3/4] Keep a late sync from restarting the disappearing-message timer A landed sync went through the same success handling as a first send, which started the message's timer again from the moment it landed. Now that a sync can land minutes later, on a retry or after a restart, a disappearing message stayed on the sending device for that much longer than on any other. A sync of an already-sent message now leaves the timer alone and shortens its own-swarm copy to expire at the sent time plus the timer, which is when linked devices expire it. --- .../messaging/jobs/MessageSendJob.kt | 2 +- .../sending_receiving/MessageSender.kt | 45 ++++++-- .../service/ExpiringMessageManager.kt | 14 +++ .../sending_receiving/SyncToSelfTest.kt | 49 +++++++-- .../groups/GroupManagerV2ImplTest.kt | 4 +- .../service/ExpiringMessageManagerSyncTest.kt | 101 ++++++++++++++++++ 6 files changed, 197 insertions(+), 18 deletions(-) create mode 100644 app/src/test/java/org/thoughtcrime/securesms/service/ExpiringMessageManagerSyncTest.kt diff --git a/app/src/main/java/org/session/libsession/messaging/jobs/MessageSendJob.kt b/app/src/main/java/org/session/libsession/messaging/jobs/MessageSendJob.kt index bcfd0f0f3d..8985c4923c 100644 --- a/app/src/main/java/org/session/libsession/messaging/jobs/MessageSendJob.kt +++ b/app/src/main/java/org/session/libsession/messaging/jobs/MessageSendJob.kt @@ -118,7 +118,7 @@ class MessageSendJob @AssistedInject constructor( } } - messageSender.sendNonDurably(this@MessageSendJob.message, destination, isSync) + messageSender.sendNonDurably(this@MessageSendJob.message, destination, isSync, isSyncOfSentMessage) this.handleSuccess(dispatcherName) statusCallback?.trySend(Result.success(Unit)) diff --git a/app/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt b/app/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt index 14725184c8..94d37642f0 100644 --- a/app/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt +++ b/app/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt @@ -125,12 +125,20 @@ class MessageSender @Inject constructor( } } - // Convenience - suspend fun sendNonDurably(message: Message, destination: Destination, isSyncMessage: Boolean) { + /** + * @param isSyncOfSentMessage [message] has already reached its recipient and this sends a copy to + * our own swarm, as opposed to a first send there such as Note to Self. + */ + suspend fun sendNonDurably( + message: Message, + destination: Destination, + isSyncMessage: Boolean, + isSyncOfSentMessage: Boolean = false, + ) { return if (destination is Destination.OpenGroup || destination is Destination.OpenGroupInbox) { sendToOpenGroupDestination(destination, message) } else { - sendToSnodeDestination(destination, message, isSyncMessage) + sendToSnodeDestination(destination, message, isSyncMessage, isSyncOfSentMessage) } } @@ -260,7 +268,12 @@ class MessageSender @Inject constructor( } // One-on-One Chats & Closed Groups - private suspend fun sendToSnodeDestination(destination: Destination, message: Message, isSyncMessage: Boolean = false) { + private suspend fun sendToSnodeDestination( + destination: Destination, + message: Message, + isSyncMessage: Boolean = false, + isSyncOfSentMessage: Boolean = false, + ) { // Set the failure handler (need it here already for precondition failure handling) fun handleFailure(error: Exception) { handleFailedMessageSend(message, error, isSyncMessage) @@ -306,7 +319,7 @@ class MessageSender @Inject constructor( if (sendResult.isSuccess) { message.serverHash = sendResult.getOrThrow().hash - handleSuccessfulMessageSend(message, destination, isSyncMessage) + handleSuccessfulMessageSend(message, destination, isSyncMessage, isSyncOfSentMessage = isSyncOfSentMessage) } else { throw sendResult.exceptionOrNull()!! } @@ -460,7 +473,14 @@ class MessageSender @Inject constructor( } // Result Handling - private fun handleSuccessfulMessageSend(message: Message, destination: Destination, isSyncMessage: Boolean = false, openGroupSentTimestamp: Long = -1) { + @VisibleForTesting + internal fun handleSuccessfulMessageSend( + message: Message, + destination: Destination, + isSyncMessage: Boolean = false, + openGroupSentTimestamp: Long = -1, + isSyncOfSentMessage: Boolean = false, + ) { val userPublicKey = storage.getUserPublicKey()!! // Ignore future self-sends storage.addReceivedMessageTimestamp(message.sentTimestamp!!) @@ -501,7 +521,11 @@ class MessageSender @Inject constructor( storage.updateSentTimestamp(messageId, message.sentTimestamp!!) // Start the disappearing messages timer if needed - messageExpirationManager.onMessageSent(message) + if (isSyncOfSentMessage) { + messageExpirationManager.onMessageSynced(message) + } else { + messageExpirationManager.onMessageSent(message) + } } ?: run { storage.updateReactionIfNeeded(message, message.sender?:userPublicKey, openGroupSentTimestamp) } @@ -538,7 +562,12 @@ class MessageSender @Inject constructor( } else { scope.launch { try { - sendToSnodeDestination(Destination.Contact(userPublicKey), message, true) + sendToSnodeDestination( + Destination.Contact(userPublicKey), + message, + isSyncMessage = true, + isSyncOfSentMessage = true, + ) } catch (ec: Exception) { Log.e("MessageSender", "Unable to send sync message", ec) } diff --git a/app/src/main/java/org/thoughtcrime/securesms/service/ExpiringMessageManager.kt b/app/src/main/java/org/thoughtcrime/securesms/service/ExpiringMessageManager.kt index 82d1069eef..9a483b90b4 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/service/ExpiringMessageManager.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/service/ExpiringMessageManager.kt @@ -210,6 +210,20 @@ class ExpiringMessageManager @Inject constructor( } } + /** + * A copy of a message we already sent has reached our own swarm. Its timer here started when it + * was first sent, and must not restart: the copy can land long afterwards, after retries or a + * restart. Linked devices count a message from ourselves from its sent timestamp, so the copy is + * shortened to expire with theirs. + */ + fun onMessageSynced(message: Message) { + val hash = message.serverHash ?: return + val sentTimestamp = message.sentTimestamp ?: return + if (message.expiryMode == ExpiryMode.NONE) return + + scope.launch { shortenTtl(hash, sentTimestamp, message.expiryMode.expiryMillis) } + } + override fun onMessageReceived(message: Message) { val messageId = message.id ?: return diff --git a/app/src/test/java/org/session/libsession/messaging/sending_receiving/SyncToSelfTest.kt b/app/src/test/java/org/session/libsession/messaging/sending_receiving/SyncToSelfTest.kt index bc34df6af4..2194eb6696 100644 --- a/app/src/test/java/org/session/libsession/messaging/sending_receiving/SyncToSelfTest.kt +++ b/app/src/test/java/org/session/libsession/messaging/sending_receiving/SyncToSelfTest.kt @@ -28,6 +28,7 @@ import org.session.libsignal.utilities.Log import org.thoughtcrime.securesms.NoOpLogger import org.thoughtcrime.securesms.database.Storage import org.thoughtcrime.securesms.database.model.MessageId +import org.thoughtcrime.securesms.service.ExpiringMessageManager import java.io.IOException import javax.inject.Provider @@ -62,8 +63,14 @@ class SyncToSelfTest { every { isOutgoingMessage(any()) } returns true } - /** Each send that reached the fake network: its destination and whether it was a sync. */ - private val sends = mutableListOf>() + private data class Send(val destination: Destination, val isSyncMessage: Boolean, val isSyncOfSentMessage: Boolean) + + /** Each send that reached the fake network. */ + private val sends = mutableListOf() + + private val syncOfSentMessage get() = Send(Destination.Contact(self), isSyncMessage = true, isSyncOfSentMessage = true) + + private val expirationManager = mockk(relaxed = true) @Before fun setUp() { @@ -81,7 +88,7 @@ class SyncToSelfTest { messageSender(queue).scheduleSyncToSelf(storedMessage(), self) advanceUntilIdle() - assertEquals(List(4) { Destination.Contact(self) to true }, sends) + assertEquals(List(4) { syncOfSentMessage }, sends) assertEquals( listOf( SYNCING, @@ -128,7 +135,7 @@ class SyncToSelfTest { JobQueue(queueScope(), storage).resumePendingJobs() advanceUntilIdle() - assertEquals(listOf(Destination.Contact(self) to true), sends) + assertEquals(listOf(syncOfSentMessage), sends) assertEquals(listOf(SYNCING, SENT), statuses) } @@ -153,6 +160,34 @@ class SyncToSelfTest { jobFactory.create(storedMessage(), Destination.Contact(self), isSyncOfSentMessage = true).execute("test") assertEquals(listOf(SENDING, SENT, SYNCING, SENT), statuses) + assertEquals( + listOf(Send(Destination.Contact(self), isSyncMessage = true, isSyncOfSentMessage = false), syncOfSentMessage), + sends + ) + } + + // A sync can land minutes after the send, on a retry or after a restart: restarting the timer then + // would keep the message on this device that much longer than on every other + @Test + fun `a landed sync of a sent message does not restart its disappearing timer`() { + val message = storedMessage() + + messageSender(mockk(relaxed = true)) + .handleSuccessfulMessageSend(message, Destination.Contact(self), isSyncMessage = true, isSyncOfSentMessage = true) + + verify(exactly = 1) { expirationManager.onMessageSynced(message) } + verify(exactly = 0) { expirationManager.onMessageSent(any()) } + } + + @Test + fun `a landed Note to Self send starts its disappearing timer`() { + val message = storedMessage() + + messageSender(mockk(relaxed = true)) + .handleSuccessfulMessageSend(message, Destination.Contact(self), isSyncMessage = true) + + verify(exactly = 1) { expirationManager.onMessageSent(message) } + verify(exactly = 0) { expirationManager.onMessageSynced(any()) } } private fun storedMessage() = VisibleMessage(syncTarget = contact, text = "hi").apply { @@ -178,9 +213,9 @@ class SyncToSelfTest { /** Fails the first [failuresBeforeSuccess] sends, then succeeds. */ private fun network(failuresBeforeSuccess: Int): MessageSender = mockk { - coEvery { sendNonDurably(any(), any(), any()) } coAnswers { + coEvery { sendNonDurably(any(), any(), any(), any()) } coAnswers { val message = firstArg() - sends += secondArg() to thirdArg() + sends += Send(secondArg(), thirdArg(), arg(3)) if (sends.size <= failuresBeforeSuccess) { val error = IOException("snode unreachable") storage.markAsSyncFailed(message.id!!, error) @@ -218,7 +253,7 @@ class SyncToSelfTest { recipientRepository = mockk(relaxed = true), messageDataProvider = messageDataProvider, messageSendJobFactory = jobFactory, - messageExpirationManager = mockk(relaxed = true), + messageExpirationManager = expirationManager, snodeClock = mockk(relaxed = true), communityApiExecutor = mockk(relaxed = true), sendCommunityMessageApiFactory = mockk(relaxed = true), diff --git a/app/src/test/java/org/thoughtcrime/securesms/groups/GroupManagerV2ImplTest.kt b/app/src/test/java/org/thoughtcrime/securesms/groups/GroupManagerV2ImplTest.kt index 2cfca2fe18..fb6be821f2 100644 --- a/app/src/test/java/org/thoughtcrime/securesms/groups/GroupManagerV2ImplTest.kt +++ b/app/src/test/java/org/thoughtcrime/securesms/groups/GroupManagerV2ImplTest.kt @@ -177,7 +177,7 @@ class GroupManagerV2ImplTest { verify(configFactory, never()).dangerouslyAccessMutableUserConfigs() // ...and the invite response goes out anyway: it is sent once on approval with its failure // swallowed, so a re-invite is the admin's only way to recover a lost one. - verify(messageSender).sendNonDurably(any(), any(), eq(false)) + verify(messageSender).sendNonDurably(any(), any(), eq(false), eq(false)) // The invitation is dropped from our swarm, as approving one does. verify(swarmApiExecutor).send(any(), any()) } @@ -192,7 +192,7 @@ class GroupManagerV2ImplTest { manager.deliverInvitation() // An admin's membership is not established by an invite response - they write it themselves. - verify(messageSender, never()).sendNonDurably(any(), any(), any()) + verify(messageSender, never()).sendNonDurably(any(), any(), any(), any()) verify(swarmApiExecutor).send(any(), any()) } } diff --git a/app/src/test/java/org/thoughtcrime/securesms/service/ExpiringMessageManagerSyncTest.kt b/app/src/test/java/org/thoughtcrime/securesms/service/ExpiringMessageManagerSyncTest.kt new file mode 100644 index 0000000000..dd190626bc --- /dev/null +++ b/app/src/test/java/org/thoughtcrime/securesms/service/ExpiringMessageManagerSyncTest.kt @@ -0,0 +1,101 @@ +package org.thoughtcrime.securesms.service + +import io.mockk.every +import io.mockk.mockk +import io.mockk.verify +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.test.advanceUntilIdle +import kotlinx.coroutines.test.runTest +import network.loki.messenger.libsession_util.util.ExpiryMode +import network.loki.messenger.libsession_util.util.KeyPair +import org.junit.Assert.assertEquals +import org.junit.Before +import org.junit.Test +import org.session.libsession.messaging.messages.visible.VisibleMessage +import org.session.libsignal.utilities.Log +import org.thoughtcrime.securesms.NoOpLogger +import org.thoughtcrime.securesms.api.snode.AlterTtlApi +import org.thoughtcrime.securesms.database.SmsDatabase +import org.thoughtcrime.securesms.database.Storage +import org.thoughtcrime.securesms.database.model.MessageId +import kotlin.time.Duration.Companion.minutes + +/** + * A message's sync can land minutes after it was sent, on a retry or after a restart. Here: a + * five-minute disappear-after-send message whose sync lands four minutes after it was sent. + */ +class ExpiringMessageManagerSyncTest { + private val sentTimestamp = 1_700_000_000_000L + private val timer = 5.minutes.inWholeMilliseconds + private val now = sentTimestamp + 4.minutes.inWholeMilliseconds + + private val smsDatabase = mockk(relaxed = true) + + /** The expiry each shortening request asked our own swarm for. */ + private val requestedExpiries = mutableListOf() + + private val alterTtlApiFactory = mockk { + every { create(any(), any(), any(), any()) } answers { + requestedExpiries += arg(3) + mockk(relaxed = true) + } + } + + @Before + fun setUp() { + Log.initialize(NoOpLogger) + } + + @Test + fun `a late sync leaves the timer alone and expires the copy with the linked devices`() = runTest { + manager(this).onMessageSynced(message()) + advanceUntilIdle() + + verify(exactly = 0) { smsDatabase.markExpireStarted(any(), any()) } + assertEquals(listOf(sentTimestamp + timer), requestedExpiries) + } + + /** The same message landing as a first send starts its timer at that moment. */ + @Test + fun `a first send starts the timer when it lands`() = runTest { + manager(this).onMessageSent(message()) + advanceUntilIdle() + + verify(exactly = 1) { smsDatabase.markExpireStarted(42L, now) } + assertEquals(listOf(now + timer), requestedExpiries) + } + + @Test + fun `a sync of a message with no timer shortens nothing`() = runTest { + manager(this).onMessageSynced(message().apply { expiryMode = ExpiryMode.NONE }) + advanceUntilIdle() + + assertEquals(emptyList(), requestedExpiries) + } + + private fun message() = VisibleMessage(syncTarget = "05" + "bb".repeat(32), text = "hi").apply { + id = MessageId(42L, false) + sentTimestamp = this@ExpiringMessageManagerSyncTest.sentTimestamp + serverHash = "own-swarm-hash" + expiryMode = ExpiryMode.AfterSend(timer / 1000) + } + + private fun manager(scope: CoroutineScope): ExpiringMessageManager { + val storage = mockk { + every { getUserPublicKey() } returns "05" + "aa".repeat(32) + every { getUserED25519KeyPair() } returns KeyPair(ByteArray(32), ByteArray(64)) + } + + return ExpiringMessageManager( + smsDatabase = smsDatabase, + mmsDatabase = mockk(relaxed = true), + clock = mockk { every { currentTimeMillis() } returns now }, + storage = { storage }, + loginStateRepository = mockk(relaxed = true), + recipientRepository = mockk(relaxed = true), + alterTtlApiFactory = alterTtlApiFactory, + swarmApiExecutor = mockk(relaxed = true), + scope = scope, + ) + } +} From 480ebcb0af83dabf0aec7aa1eacc5f9a825259c5 Mon Sep 17 00:00:00 2001 From: Morgan Pretty Date: Wed, 18 Nov 2026 09:37:12 +1100 Subject: [PATCH 4/4] Remove a message send job once its message has been deleted A send job whose message was deleted, or had expired, before it ran stopped without marking itself done, so its row stayed and was resumed at every launch only to stop again. The same happened when a send failed for a message deleted in the meantime. Each of these now finishes the job. Sync jobs made this far more common, since one is queued for every stored 1:1 message. --- .../messaging/jobs/MessageSendJob.kt | 18 ++++++-- .../sending_receiving/SyncToSelfTest.kt | 43 ++++++++++++++++++- 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/session/libsession/messaging/jobs/MessageSendJob.kt b/app/src/main/java/org/session/libsession/messaging/jobs/MessageSendJob.kt index 8985c4923c..291c1a728f 100644 --- a/app/src/main/java/org/session/libsession/messaging/jobs/MessageSendJob.kt +++ b/app/src/main/java/org/session/libsession/messaging/jobs/MessageSendJob.kt @@ -69,8 +69,8 @@ class MessageSendJob @AssistedInject constructor( val messageId = message.id val message = message as? VisibleMessage - // do not attempt to send if the message is marked as deleted if (messageId != null && messageDataProvider.isDeletedMessage(messageId)) { + finishForDeletedMessage(dispatcherName) return } @@ -80,7 +80,10 @@ class MessageSendJob @AssistedInject constructor( if (message != null) { val isOutgoing = messageId != null && messageDataProvider.isOutgoingMessage(messageId) - if (!isOutgoing && message.reaction == null) return // The message has been deleted + if (!isOutgoing && message.reaction == null) { + finishForDeletedMessage(dispatcherName) + return + } val attachmentIDs = mutableListOf() attachmentIDs.addAll(message.attachmentIDs) message.quote?.let { it.attachmentID?.let { attachmentID -> attachmentIDs.add(attachmentID) } } @@ -158,13 +161,22 @@ class MessageSendJob @AssistedInject constructor( delegate?.handleJobFailedPermanently(this, dispatcherName, error) } + /** + * A job whose message has been deleted has nothing left to send, but it must still be removed: + * left in storage, it is resumed at every launch only to stop here again. + */ + private fun finishForDeletedMessage(dispatcherName: String) { + delegate?.handleJobSucceeded(this, dispatcherName) + } + private fun handleFailure(dispatcherName: String, error: Exception) { Log.w(TAG, "Failed to send ${message::class.simpleName}.", error) val messageId = message.id if (message is VisibleMessage && messageId != null) { if (messageDataProvider.isDeletedMessage(messageId) || !messageDataProvider.isOutgoingMessage(messageId)) { - return // The message has been deleted + finishForDeletedMessage(dispatcherName) + return } } delegate?.handleJobFailed(this, dispatcherName, error) diff --git a/app/src/test/java/org/session/libsession/messaging/sending_receiving/SyncToSelfTest.kt b/app/src/test/java/org/session/libsession/messaging/sending_receiving/SyncToSelfTest.kt index 2194eb6696..995a18a806 100644 --- a/app/src/test/java/org/session/libsession/messaging/sending_receiving/SyncToSelfTest.kt +++ b/app/src/test/java/org/session/libsession/messaging/sending_receiving/SyncToSelfTest.kt @@ -190,6 +190,44 @@ class SyncToSelfTest { verify(exactly = 0) { expirationManager.onMessageSynced(any()) } } + @Test + fun `a sync whose message was deleted before it ran sends nothing and is removed`() = runTest { + network = network(failuresBeforeSuccess = 0) + every { messageDataProvider.isDeletedMessage(any()) } returns true + + messageSender(JobQueue(queueScope(), storage)).scheduleSyncToSelf(storedMessage(), self) + advanceUntilIdle() + + assertEquals(emptyList(), sends) + verify(exactly = 1) { storage.markJobAsSucceeded(any()) } + } + + @Test + fun `a sync whose message no longer exists sends nothing and is removed`() = runTest { + network = network(failuresBeforeSuccess = 0) + every { messageDataProvider.isOutgoingMessage(any()) } returns false + + messageSender(JobQueue(queueScope(), storage)).scheduleSyncToSelf(storedMessage(), self) + advanceUntilIdle() + + assertEquals(emptyList(), sends) + verify(exactly = 1) { storage.markJobAsSucceeded(any()) } + } + + @Test + fun `a sync whose message is deleted during a failed attempt is removed, not retried`() = runTest { + network = network(failuresBeforeSuccess = 1) { + every { messageDataProvider.isDeletedMessage(any()) } returns true + } + + messageSender(JobQueue(queueScope(), storage)).scheduleSyncToSelf(storedMessage(), self) + advanceUntilIdle() + + assertEquals(listOf(syncOfSentMessage), sends) + verify(exactly = 1) { storage.markJobAsSucceeded(any()) } + verify(exactly = 0) { storage.persistJob(match { it.failureCount > 0 }) } + } + private fun storedMessage() = VisibleMessage(syncTarget = contact, text = "hi").apply { id = messageId sentTimestamp = 1_700_000_000_000L @@ -211,12 +249,13 @@ class SyncToSelfTest { queueScopes.forEach { it.cancel() } } - /** Fails the first [failuresBeforeSuccess] sends, then succeeds. */ - private fun network(failuresBeforeSuccess: Int): MessageSender = mockk { + /** Fails the first [failuresBeforeSuccess] sends, running [duringFailure] in each, then succeeds. */ + private fun network(failuresBeforeSuccess: Int, duringFailure: () -> Unit = {}): MessageSender = mockk { coEvery { sendNonDurably(any(), any(), any(), any()) } coAnswers { val message = firstArg() sends += Send(secondArg(), thirdArg(), arg(3)) if (sends.size <= failuresBeforeSuccess) { + duringFailure() val error = IOException("snode unreachable") storage.markAsSyncFailed(message.id!!, error) throw error