From f4e32bac1a21041a8f29c628d4d35d7be0264659 Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Fri, 3 Oct 2025 16:38:25 +0200 Subject: [PATCH] Clear queued state updates on immediate emission (#1685) There was a possible race condition where immediate state updates got overwritten by previously queued ones. For example, when the download was successful but the downloaded files are deemed invalid, a previously queued download progress state update might overwrite the emitted error state. --- .../tachidesk/server/util/WebInterfaceManager.kt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/server/src/main/kotlin/suwayomi/tachidesk/server/util/WebInterfaceManager.kt b/server/src/main/kotlin/suwayomi/tachidesk/server/util/WebInterfaceManager.kt index 2ddc8ddf..11b2a745 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/server/util/WebInterfaceManager.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/server/util/WebInterfaceManager.kt @@ -22,7 +22,6 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.SupervisorJob -import kotlinx.coroutines.channels.BufferOverflow.DROP_OLDEST import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.combine @@ -87,8 +86,7 @@ object WebInterfaceManager { private val json: Json by injectLazy() private val network: NetworkHelper by injectLazy() - private val notifyFlow = - MutableSharedFlow(extraBufferCapacity = 1, onBufferOverflow = DROP_OLDEST) + private val notifyFlow = MutableSharedFlow() private val statusFlow = MutableSharedFlow() val status = @@ -102,7 +100,10 @@ object WebInterfaceManager { scope.launch { @OptIn(FlowPreview::class) notifyFlow.sample(1.seconds).collect { - statusFlow.emit(it) + if (it != null) { + logger.debug { "notifyFlow: sampling $it" } + statusFlow.emit(it) + } } } @@ -667,6 +668,7 @@ object WebInterfaceManager { val status = getStatus(version, state, progress) if (immediate) { + notifyFlow.emit(null) statusFlow.emit(status) return@launch }