adds a mutex to make sure memory clearing only happens once at a time.

This commit is contained in:
Vitor Pamplona
2025-03-31 11:09:23 -04:00
parent 396831456c
commit b762907d69

View File

@@ -71,6 +71,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import java.util.concurrent.atomic.AtomicBoolean
@Stable
class ServiceManager(
@@ -84,6 +85,8 @@ class ServiceManager(
private var collectorJob: Job? = null
var isTrimmingMemoryMutex = AtomicBoolean(false)
private fun start(account: Account) {
this.account = account
start()
@@ -259,6 +262,8 @@ class ServiceManager(
}
suspend fun trimMemory() {
if (isTrimmingMemoryMutex.compareAndSet(false, true)) {
try {
LocalCache.cleanObservers()
val accounts =
@@ -274,6 +279,10 @@ class ServiceManager(
LocalCache.prunePastVersionsOfReplaceables()
LocalCache.pruneExpiredEvents()
}
} finally {
isTrimmingMemoryMutex.getAndSet(false)
}
}
}
// This method keeps the pause/start in a Syncronized block to