From 349f7a7989880b6d61ee0a33e743a2cdf0473808 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Tue, 12 May 2026 19:21:14 -0400 Subject: [PATCH] fix(quartz): make IngestQueue writer-start KMP-safe via AtomicBoolean Replaces JVM-only @Volatile + synchronized double-checked locking with kotlin.concurrent.atomics.AtomicBoolean.compareAndSet so the file builds on all commonMain targets, mirroring the pattern in BasicRelayClient. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../quartz/nip01Core/relay/server/IngestQueue.kt | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/server/IngestQueue.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/server/IngestQueue.kt index 5b4876693..8c9cb467d 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/server/IngestQueue.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/server/IngestQueue.kt @@ -33,6 +33,8 @@ import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.ClosedReceiveChannelException import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.launch +import kotlin.concurrent.atomics.AtomicBoolean +import kotlin.concurrent.atomics.ExperimentalAtomicApi import kotlin.coroutines.CoroutineContext /** @@ -73,6 +75,7 @@ import kotlin.coroutines.CoroutineContext * propagates back through the WebSocket pump so a slow disk * eventually slows the publisher rather than ballooning JVM memory. */ +@OptIn(ExperimentalAtomicApi::class) class IngestQueue( private val store: IEventStore, parentContext: CoroutineContext, @@ -120,9 +123,7 @@ class IngestQueue( * keeps relays that never see an EVENT (read-only sessions, * negentropy-only) from paying for the writer at all. */ - @Volatile - private var writerStarted = false - private val startLock = Any() + private val writerStarted = AtomicBoolean(false) /** * Hand off [event] for insertion. [onComplete] is invoked once @@ -144,11 +145,8 @@ class IngestQueue( } private fun ensureWriterStarted() { - if (writerStarted) return - synchronized(startLock) { - if (writerStarted) return + if (writerStarted.compareAndSet(expectedValue = false, newValue = true)) { scope.launch { drainLoop() } - writerStarted = true } }