forked from upstream-mirrors/quik
Fix Matrix login crash by closing clients through supported API
Build and Release APK / Build APK (push) Successful in 4m17s
Build and Release APK / Build APK (push) Successful in 4m17s
This commit is contained in:
@@ -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<TrixnityRoomDatabase>().close()
|
||||
closeMatrixClient(client) { client.di.get<TrixnityRoomDatabase>().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<TrixnityRoomDatabase>().close()
|
||||
closeMatrixClient(client) { 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.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<TrixnityRoomDatabase>().close()
|
||||
closeMatrixClient(client) { client.di.get<TrixnityRoomDatabase>().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<TrixnityRoomDatabase>().close()
|
||||
closeMatrixClient(entry.value.first) {
|
||||
entry.value.first.di.get<TrixnityRoomDatabase>().close()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user