Skip to content

Commit

Permalink
fix: deadlock while cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
yamilmedina committed Nov 26, 2024
1 parent 20b3892 commit dca57d4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit dca57d4

Please sign in to comment.