From dca57d43504c48234565cdcce755f0901db98408 Mon Sep 17 00:00:00 2001 From: yamilmedina Date: Tue, 26 Nov 2024 16:49:01 +0100 Subject: [PATCH] fix: deadlock while cleanup --- .../data/client/ProteusClientProvider.kt | 23 ++++++++++++------- .../client/ProteusMigrationRecoveryHandler.kt | 2 +- .../ProteusMigrationRecoveryHandlerImpl.kt | 3 ++- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/client/ProteusClientProvider.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/client/ProteusClientProvider.kt index a3bb9b35298..3c4f9bf4f65 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/client/ProteusClientProvider.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/client/ProteusClientProvider.kt @@ -68,13 +68,7 @@ class ProteusClientProviderImpl( private val mutex = Mutex() override suspend fun clearLocalFiles() { - mutex.withLock { - withContext(dispatcher.io) { - _proteusClient?.close() - _proteusClient = null - FileUtil.deleteDirectory(rootProteusPath) - } - } + mutex.withLock { removeLocalFiles() } } override suspend fun getOrCreate(): ProteusClient { @@ -135,7 +129,7 @@ class ProteusClientProviderImpl( return try { central.proteusClient() } catch (exception: ProteusStorageMigrationException) { - proteusMigrationRecoveryHandler.clearClientData() + proteusMigrationRecoveryHandler.clearClientData { removeLocalFiles() } val logMap = mapOf( "userId" to userId.value.obfuscateId(), "exception" to exception, @@ -147,6 +141,19 @@ class ProteusClientProviderImpl( } } + /** + * Actually deletes the proteus local files. + * Important! It is the caller responsibility to use the mutex, DON'T add a mutex here or it will be dead lock it. + */ + private suspend fun removeLocalFiles() { + withContext(dispatcher.io) { + _proteusClient?.close() + _proteusClient = null + FileUtil.deleteDirectory(rootProteusPath) + } + } + + private companion object { const val TAG = "ProteusClientProvider" } diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/client/ProteusMigrationRecoveryHandler.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/client/ProteusMigrationRecoveryHandler.kt index 7bf7443ecb1..9fe6880ecc4 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/client/ProteusMigrationRecoveryHandler.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/client/ProteusMigrationRecoveryHandler.kt @@ -26,5 +26,5 @@ import com.wire.kalium.logic.data.logout.LogoutReason * This achieves that the client data is cleared and the user is logged out without losing content. */ interface ProteusMigrationRecoveryHandler { - suspend fun clearClientData() + suspend fun clearClientData(clearLocalFiles: suspend () -> Unit) } diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/client/ProteusMigrationRecoveryHandlerImpl.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/client/ProteusMigrationRecoveryHandlerImpl.kt index 294da1712f7..c2b7e42bfbb 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/client/ProteusMigrationRecoveryHandlerImpl.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/client/ProteusMigrationRecoveryHandlerImpl.kt @@ -33,9 +33,10 @@ internal class ProteusMigrationRecoveryHandlerImpl( * This achieves that the client data is cleared and the user is logged out without losing content. */ @Suppress("TooGenericExceptionCaught") - override suspend fun clearClientData() { + override suspend fun clearClientData(clearLocalFiles: suspend () -> Unit) { try { kaliumLogger.withTextTag(TAG).i("Starting the recovery from failed Proteus storage migration") + clearLocalFiles() logoutUseCase.value(LogoutReason.MIGRATION_TO_CC_FAILED, true) } catch (e: Exception) { kaliumLogger.withTextTag(TAG).e("Fatal, error while clearing client data: $e")