Skip to content

Fix concurrency crash in InternalDatabaseImpl #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import kotlinx.serialization.encodeToString

Expand Down Expand Up @@ -69,15 +71,23 @@ internal class InternalDatabaseImpl(
init {
scope.launch {
val accumulatedUpdates = mutableSetOf<String>()
val accumulatedUpdatesSetMutex = Mutex()

tableUpdates()
// Debounce will discard any events which occur inside the debounce window
// This will accumulate those table updates
.onEach { tables -> accumulatedUpdates.addAll(tables) }
.onEach { tables ->
accumulatedUpdatesSetMutex.withLock {
accumulatedUpdates.addAll(tables)
}
}
.debounce(DEFAULT_WATCH_THROTTLE_MS)
.collect {
val dataTables = accumulatedUpdates.map { toFriendlyTableName(it) }.filter { it.isNotBlank() }
driver.notifyListeners(queryKeys = dataTables.toTypedArray())
accumulatedUpdates.clear()
accumulatedUpdatesSetMutex.withLock {
val dataTables = accumulatedUpdates.map { toFriendlyTableName(it) }.filter { it.isNotBlank() }
driver.notifyListeners(queryKeys = dataTables.toTypedArray())
accumulatedUpdates.clear()
}
}
}
}
Expand Down