From 792d548715c185a34d519017cbcc71289cd3c973 Mon Sep 17 00:00:00 2001 From: karol Date: Sun, 18 Oct 2026 00:02:31 +0200 Subject: [PATCH] Fix Matrix login crash by closing clients through supported API --- .../main/java/ch/skew/remotrix/NewAccount.kt | 11 ++---- .../remotrix/background/CommandService.kt | 12 +++--- .../skew/remotrix/bridge/CloseMatrixClient.kt | 9 +++++ .../remotrix/bridge/CloseMatrixClientTest.kt | 37 +++++++++++++++++++ 4 files changed, 54 insertions(+), 15 deletions(-) create mode 100644 remotrix-sync/src/main/java/ch/skew/remotrix/bridge/CloseMatrixClient.kt create mode 100644 remotrix-sync/src/test/java/ch/skew/remotrix/bridge/CloseMatrixClientTest.kt diff --git a/remotrix-sync/src/main/java/ch/skew/remotrix/NewAccount.kt b/remotrix-sync/src/main/java/ch/skew/remotrix/NewAccount.kt index cca86e6a..797fcaa4 100644 --- a/remotrix-sync/src/main/java/ch/skew/remotrix/NewAccount.kt +++ b/remotrix-sync/src/main/java/ch/skew/remotrix/NewAccount.kt @@ -1,7 +1,6 @@ package ch.skew.remotrix import android.content.Context -import kotlinx.coroutines.cancel import android.content.Intent import android.widget.Toast import androidx.compose.foundation.layout.Arrangement @@ -31,6 +30,7 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.room.Room.databaseBuilder +import ch.skew.remotrix.bridge.closeMatrixClient import ch.skew.remotrix.background.CommandService import ch.skew.remotrix.components.LabelledRadioButton import ch.skew.remotrix.components.PasswordField @@ -38,7 +38,6 @@ import ch.skew.remotrix.data.RemotrixDB import ch.skew.remotrix.data.RemotrixSettings import io.ktor.http.Url import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Job import kotlinx.coroutines.flow.first import net.folivo.trixnity.clientserverapi.client.SyncState import kotlinx.coroutines.launch @@ -423,9 +422,7 @@ fun onLoginClick( } // The service opens its own client. Release this setup client first, // so reset/reload can close and delete each encryption store safely. - (client as CoroutineScope).cancel() - client.coroutineContext[Job]?.join() - client.di.get().close() + closeMatrixClient(client) { client.di.get().close() } clientToClose = null Intent(context, CommandService::class.java) .apply { @@ -439,9 +436,7 @@ fun onLoginClick( } finally { clientToClose?.let { client -> kotlinx.coroutines.withContext(kotlinx.coroutines.NonCancellable) { - (client as CoroutineScope).cancel() - client.coroutineContext[Job]?.join() - client.di.get().close() + closeMatrixClient(client) { client.di.get().close() } } } } diff --git a/remotrix-sync/src/main/java/ch/skew/remotrix/background/CommandService.kt b/remotrix-sync/src/main/java/ch/skew/remotrix/background/CommandService.kt index a06c4f27..f2edf6e0 100644 --- a/remotrix-sync/src/main/java/ch/skew/remotrix/background/CommandService.kt +++ b/remotrix-sync/src/main/java/ch/skew/remotrix/background/CommandService.kt @@ -20,6 +20,7 @@ import ch.skew.remotrix.data.RemotrixDB import ch.skew.remotrix.data.RemotrixSettings import ch.skew.remotrix.data.forwardRuleDB.ForwardRule import ch.skew.remotrix.data.roomIdDB.RoomIdData +import ch.skew.remotrix.bridge.closeMatrixClient import ch.skew.remotrix.bridge.resetBridge import ch.skew.remotrix.bridge.RoomResolver import ch.skew.remotrix.contacts.ContactLookup @@ -29,7 +30,6 @@ import io.ktor.utils.io.ByteReadChannel import kotlinx.coroutines.CoroutineExceptionHandler import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.Job import kotlinx.coroutines.cancel import kotlinx.coroutines.withTimeoutOrNull import kotlinx.coroutines.delay @@ -202,9 +202,7 @@ class CommandService: Service() { stopClients = { for ((client, _) in clients.values) { withTimeoutOrNull(10_000) { client.stopSync() } - (client as CoroutineScope).cancel() - client.coroutineContext[Job]?.join() - client.di.get().close() + closeMatrixClient(client) { client.di.get().close() } } clients.clear() }, @@ -245,9 +243,9 @@ class CommandService: Service() { entry.value.first.stopSync() while (entry.value.first.syncState.first() != SyncState.STOPPED) kotlinx.coroutines.delay(500) } - (entry.value.first as kotlinx.coroutines.CoroutineScope).cancel() - kotlinx.coroutines.delay(200) - entry.value.first.di.get().close() + closeMatrixClient(entry.value.first) { + entry.value.first.di.get().close() + } } catch (e: Exception) { e.printStackTrace() } diff --git a/remotrix-sync/src/main/java/ch/skew/remotrix/bridge/CloseMatrixClient.kt b/remotrix-sync/src/main/java/ch/skew/remotrix/bridge/CloseMatrixClient.kt new file mode 100644 index 00000000..ced30297 --- /dev/null +++ b/remotrix-sync/src/main/java/ch/skew/remotrix/bridge/CloseMatrixClient.kt @@ -0,0 +1,9 @@ +package ch.skew.remotrix.bridge + +import net.folivo.trixnity.client.MatrixClient + +/** Closing locally keeps the saved login; logout would invalidate the bot session. */ +suspend fun closeMatrixClient(client: MatrixClient, closeStore: () -> Unit) { + client.closeSuspending() + closeStore() +} diff --git a/remotrix-sync/src/test/java/ch/skew/remotrix/bridge/CloseMatrixClientTest.kt b/remotrix-sync/src/test/java/ch/skew/remotrix/bridge/CloseMatrixClientTest.kt new file mode 100644 index 00000000..bba51aad --- /dev/null +++ b/remotrix-sync/src/test/java/ch/skew/remotrix/bridge/CloseMatrixClientTest.kt @@ -0,0 +1,37 @@ +package ch.skew.remotrix.bridge + +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.runBlocking +import net.folivo.trixnity.client.MatrixClient +import org.junit.Assert.* +import org.junit.Test +import java.lang.reflect.Proxy + +class CloseMatrixClientTest { + private fun client(onClose: () -> Unit): MatrixClient = Proxy.newProxyInstance( + MatrixClient::class.java.classLoader, arrayOf(MatrixClient::class.java) + ) { _, method, _ -> + when (method.name) { + "closeSuspending" -> { onClose(); Unit } + else -> error("Unexpected client call: ${method.name}") + } + } as MatrixClient + + @Test + fun closesClientWithoutCoroutineScopeCastOrLogoutBeforeClosingStore() = runBlocking { + val calls = mutableListOf() + val client = client { calls.add("client") } + assertFalse(client is CoroutineScope) + closeMatrixClient(client) { calls.add("store") } + assertEquals(listOf("client", "store"), calls) + } + + @Test + fun leavesStoreOpenWhenClientCannotBeClosed() = runBlocking { + val client = client { error("close failed") } + var storeClosed = false + val result = runCatching { closeMatrixClient(client) { storeClosed = true } } + assertTrue(result.isFailure) + assertFalse(storeClosed) + } +}