Fix Matrix login crash by closing clients through supported API
Build and Release APK / Build APK (push) Successful in 4m17s

This commit is contained in:
2026-10-18 00:02:31 +02:00
parent 0e8391b8f2
commit 792d548715
4 changed files with 54 additions and 15 deletions
@@ -1,7 +1,6 @@
package ch.skew.remotrix package ch.skew.remotrix
import android.content.Context import android.content.Context
import kotlinx.coroutines.cancel
import android.content.Intent import android.content.Intent
import android.widget.Toast import android.widget.Toast
import androidx.compose.foundation.layout.Arrangement 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.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.room.Room.databaseBuilder import androidx.room.Room.databaseBuilder
import ch.skew.remotrix.bridge.closeMatrixClient
import ch.skew.remotrix.background.CommandService import ch.skew.remotrix.background.CommandService
import ch.skew.remotrix.components.LabelledRadioButton import ch.skew.remotrix.components.LabelledRadioButton
import ch.skew.remotrix.components.PasswordField import ch.skew.remotrix.components.PasswordField
@@ -38,7 +38,6 @@ import ch.skew.remotrix.data.RemotrixDB
import ch.skew.remotrix.data.RemotrixSettings import ch.skew.remotrix.data.RemotrixSettings
import io.ktor.http.Url import io.ktor.http.Url
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
import net.folivo.trixnity.clientserverapi.client.SyncState import net.folivo.trixnity.clientserverapi.client.SyncState
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@@ -423,9 +422,7 @@ fun onLoginClick(
} }
// The service opens its own client. Release this setup client first, // The service opens its own client. Release this setup client first,
// so reset/reload can close and delete each encryption store safely. // so reset/reload can close and delete each encryption store safely.
(client as CoroutineScope).cancel() closeMatrixClient(client) { client.di.get<TrixnityRoomDatabase>().close() }
client.coroutineContext[Job]?.join()
client.di.get<TrixnityRoomDatabase>().close()
clientToClose = null clientToClose = null
Intent(context, CommandService::class.java) Intent(context, CommandService::class.java)
.apply { .apply {
@@ -439,9 +436,7 @@ fun onLoginClick(
} finally { } finally {
clientToClose?.let { client -> clientToClose?.let { client ->
kotlinx.coroutines.withContext(kotlinx.coroutines.NonCancellable) { kotlinx.coroutines.withContext(kotlinx.coroutines.NonCancellable) {
(client as CoroutineScope).cancel() closeMatrixClient(client) { client.di.get<TrixnityRoomDatabase>().close() }
client.coroutineContext[Job]?.join()
client.di.get<TrixnityRoomDatabase>().close()
} }
} }
} }
@@ -20,6 +20,7 @@ import ch.skew.remotrix.data.RemotrixDB
import ch.skew.remotrix.data.RemotrixSettings import ch.skew.remotrix.data.RemotrixSettings
import ch.skew.remotrix.data.forwardRuleDB.ForwardRule import ch.skew.remotrix.data.forwardRuleDB.ForwardRule
import ch.skew.remotrix.data.roomIdDB.RoomIdData import ch.skew.remotrix.data.roomIdDB.RoomIdData
import ch.skew.remotrix.bridge.closeMatrixClient
import ch.skew.remotrix.bridge.resetBridge import ch.skew.remotrix.bridge.resetBridge
import ch.skew.remotrix.bridge.RoomResolver import ch.skew.remotrix.bridge.RoomResolver
import ch.skew.remotrix.contacts.ContactLookup import ch.skew.remotrix.contacts.ContactLookup
@@ -29,7 +30,6 @@ import io.ktor.utils.io.ByteReadChannel
import kotlinx.coroutines.CoroutineExceptionHandler import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel import kotlinx.coroutines.cancel
import kotlinx.coroutines.withTimeoutOrNull import kotlinx.coroutines.withTimeoutOrNull
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
@@ -202,9 +202,7 @@ class CommandService: Service() {
stopClients = { stopClients = {
for ((client, _) in clients.values) { for ((client, _) in clients.values) {
withTimeoutOrNull(10_000) { client.stopSync() } withTimeoutOrNull(10_000) { client.stopSync() }
(client as CoroutineScope).cancel() closeMatrixClient(client) { client.di.get<TrixnityRoomDatabase>().close() }
client.coroutineContext[Job]?.join()
client.di.get<TrixnityRoomDatabase>().close()
} }
clients.clear() clients.clear()
}, },
@@ -245,9 +243,9 @@ class CommandService: Service() {
entry.value.first.stopSync() entry.value.first.stopSync()
while (entry.value.first.syncState.first() != SyncState.STOPPED) kotlinx.coroutines.delay(500) while (entry.value.first.syncState.first() != SyncState.STOPPED) kotlinx.coroutines.delay(500)
} }
(entry.value.first as kotlinx.coroutines.CoroutineScope).cancel() closeMatrixClient(entry.value.first) {
kotlinx.coroutines.delay(200) entry.value.first.di.get<TrixnityRoomDatabase>().close()
entry.value.first.di.get<TrixnityRoomDatabase>().close() }
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
} }
@@ -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()
}
@@ -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<String>()
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)
}
}