Skip to content

Commit

Permalink
fix: do not create a deadlock
Browse files Browse the repository at this point in the history
As `getStoredQueue` is now using `mutex`, we cannot run it in the scope of the same `Mutex#withLock` as it'll conflict and lock both operations.
  • Loading branch information
wzieba committed Nov 11, 2023
1 parent 8d4edd4 commit 643defe
Showing 1 changed file with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ internal open class LocalStorageRepository(private val context: Context) {
}
}

open suspend fun remove(toRemove: List<Map<String, Any?>?>) = mutex.withLock {
persistObject(getStoredQueue() - toRemove.toSet())
open suspend fun remove(toRemove: List<Map<String, Any?>?>) {
val storedEvents = getStoredQueue()

mutex.withLock {
persistObject(storedEvents - toRemove.toSet())
}
}

/**
Expand Down Expand Up @@ -67,8 +71,12 @@ internal open class LocalStorageRepository(private val context: Context) {
/**
* Save the event queue to persistent storage.
*/
open suspend fun insertEvents(toInsert: List<Map<String, Any?>?>) = mutex.withLock {
persistObject(ArrayList((toInsert + getStoredQueue()).distinct()))
open suspend fun insertEvents(toInsert: List<Map<String, Any?>?>){
val storedEvents = getStoredQueue()

mutex.withLock {
persistObject(ArrayList((toInsert + storedEvents).distinct()))
}
}

companion object {
Expand Down

0 comments on commit 643defe

Please sign in to comment.