From 2d1aed18398b8ea5f573935fd62f5b29703ee405 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Mon, 8 Nov 2021 14:50:18 +0000 Subject: [PATCH] separating the stopping of in-flight tasks form the cleaning up of the session state - by stopping the session tasks before invalidating the current session we're able to avoid any extra token failures which retrigger the signout flow --- .../matrix/android/sdk/internal/SessionManager.kt | 7 +++++++ .../session/account/DeactivateAccountTask.kt | 4 ++-- .../sdk/internal/session/cleanup/CleanupSession.kt | 14 ++++++++++---- .../sdk/internal/session/signout/SignOutTask.kt | 3 ++- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/SessionManager.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/SessionManager.kt index c746ad863a..38c80e0690 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/SessionManager.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/SessionManager.kt @@ -51,6 +51,13 @@ internal class SessionManager @Inject constructor(private val matrixComponent: M } } + fun stopSession(sessionId: String) { + if (sessionComponents.containsKey(sessionId).not()) { + throw RuntimeException("You don't have a session for id $sessionId") + } + sessionComponents[sessionId]!!.session().stopSync() + } + fun getOrCreateSessionComponent(sessionParams: SessionParams): SessionComponent { return sessionComponents.getOrPut(sessionParams.credentials.sessionId()) { DaggerSessionComponent diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/account/DeactivateAccountTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/account/DeactivateAccountTask.kt index 1a8e80ab68..752856b931 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/account/DeactivateAccountTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/account/DeactivateAccountTask.kt @@ -44,7 +44,7 @@ internal class DefaultDeactivateAccountTask @Inject constructor( override suspend fun execute(params: DeactivateAccountTask.Params) { val deactivateAccountParams = DeactivateAccountParams.create(params.userAuthParam, params.eraseAllData) - + cleanupSession.stopActiveTasks() val canCleanup = try { executeRequest(globalErrorReceiver) { accountAPI.deactivate(deactivateAccountParams) @@ -71,7 +71,7 @@ internal class DefaultDeactivateAccountTask @Inject constructor( runCatching { identityDisconnectTask.execute(Unit) } .onFailure { Timber.w(it, "Unable to disconnect identity server") } - cleanupSession.handle() + cleanupSession.cleanup() } } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/cleanup/CleanupSession.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/cleanup/CleanupSession.kt index aad8e10cc0..c42141a0aa 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/cleanup/CleanupSession.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/cleanup/CleanupSession.kt @@ -50,14 +50,20 @@ internal class CleanupSession @Inject constructor( @CryptoDatabase private val realmCryptoConfiguration: RealmConfiguration, @UserMd5 private val userMd5: String ) { - suspend fun handle() { + + fun stopActiveTasks() { + Timber.d("Cleanup: cancel pending works...") + workManagerProvider.cancelAllWorks() + + Timber.d("Cleanup: stop session...") + sessionManager.stopSession(sessionId) + } + + suspend fun cleanup() { val sessionRealmCount = Realm.getGlobalInstanceCount(realmSessionConfiguration) val cryptoRealmCount = Realm.getGlobalInstanceCount(realmCryptoConfiguration) Timber.d("Realm instance ($sessionRealmCount - $cryptoRealmCount)") - Timber.d("Cleanup: cancel pending works...") - workManagerProvider.cancelAllWorks() - Timber.d("Cleanup: release session...") sessionManager.releaseSession(sessionId) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/signout/SignOutTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/signout/SignOutTask.kt index 19f34746ab..7ac34e80e9 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/signout/SignOutTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/signout/SignOutTask.kt @@ -43,6 +43,7 @@ internal class DefaultSignOutTask @Inject constructor( override suspend fun execute(params: SignOutTask.Params) { // It should be done even after a soft logout, to be sure the deviceId is deleted on the if (params.signOutFromHomeserver) { + cleanupSession.stopActiveTasks() Timber.d("SignOut: send request...") try { executeRequest(globalErrorReceiver) { @@ -67,6 +68,6 @@ internal class DefaultSignOutTask @Inject constructor( .onFailure { Timber.w(it, "Unable to disconnect identity server") } Timber.d("SignOut: cleanup session...") - cleanupSession.handle() + cleanupSession.cleanup() } }