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
This commit is contained in:
Adam Brown 2021-11-08 14:50:18 +00:00
parent 52731cf59d
commit 2d1aed1839
4 changed files with 21 additions and 7 deletions

View file

@ -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

View file

@ -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()
}
}
}

View file

@ -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)

View file

@ -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()
}
}