fix(persistence): serialize SqliteEventStore reads with append transaction (#52)

Reads (read/readFrom/lastSequence/allEvents/allSessionIds/lastGlobalSequence)
shared the single JDBC Connection with append's transaction unsynchronized.
The coroutine appendMutex only excludes append-vs-append; a read on the caller
thread could hit the connection mid-transaction (setAutoCommit(false)..commit)
and corrupt tx state — SQLite JDBC is not thread-safe. Guard every connection
access with a shared JVM monitor (synchronized(connection)) via withConnection,
so reads and the IO-thread append transaction are mutually exclusive.
This commit is contained in:
2026-07-12 13:17:38 +04:00
parent bb73bc9371
commit 759828c0f5
@@ -72,7 +72,8 @@ class SqliteEventStore(
appendMutex.withLock {
artifactStore.flushBefore {
withContext(Dispatchers.IO) {
stored = connection.transaction {
stored = withConnection {
connection.transaction {
val existing = findByEventId(event.metadata.eventId)
check(existing == null) { "duplicate event_id: ${event.metadata.eventId}" }
val globalSeqVal = nextGlobalSequence()
@@ -89,6 +90,7 @@ class SqliteEventStore(
}
}
}
}
val result = checkNotNull(stored)
subscriptions[event.metadata.sessionId]?.tryEmit(result)
globalFlow.emit(result)
@@ -102,7 +104,8 @@ class SqliteEventStore(
appendMutex.withLock {
artifactStore.flushBefore {
withContext(Dispatchers.IO) {
stored = connection.transaction {
stored = withConnection {
connection.transaction {
events.map { event ->
val existing = findByEventId(event.metadata.eventId)
check(existing == null) { "duplicate event_id: ${event.metadata.eventId}" }
@@ -121,13 +124,14 @@ class SqliteEventStore(
}
}
}
}
val flow = subscriptions[sessionId]
if (flow != null) stored.forEach { flow.tryEmit(it) }
stored.forEach { globalFlow.emit(it) }
return stored
}
override fun read(sessionId: SessionId): List<StoredEvent> =
override fun read(sessionId: SessionId): List<StoredEvent> = withConnection {
connection.prepareStatement(
"""
SELECT * FROM events
@@ -144,8 +148,9 @@ class SqliteEventStore(
}
}
}
}
override fun readFrom(sessionId: SessionId, fromSequence: Long): List<StoredEvent> =
override fun readFrom(sessionId: SessionId, fromSequence: Long): List<StoredEvent> = withConnection {
connection.prepareStatement(
"""
SELECT * FROM events
@@ -164,8 +169,9 @@ class SqliteEventStore(
}
}
}
}
override fun lastSequence(sessionId: SessionId): Long? =
override fun lastSequence(sessionId: SessionId): Long? = withConnection {
connection.prepareStatement(
"SELECT MAX(session_sequence) FROM events WHERE session_id = ?"
).use { ps ->
@@ -175,6 +181,7 @@ class SqliteEventStore(
else null
}
}
}
override fun subscribe(sessionId: SessionId): Flow<StoredEvent> =
subscriptions.computeIfAbsent(sessionId) { MutableSharedFlow(replay = 0, extraBufferCapacity = 64) }
@@ -183,6 +190,7 @@ class SqliteEventStore(
override suspend fun lastGlobalSequence(): Long =
withContext(Dispatchers.IO) {
withConnection {
connection.prepareStatement(
"SELECT COALESCE(MAX(sequence), 0) FROM events"
).use { ps ->
@@ -192,8 +200,9 @@ class SqliteEventStore(
}
}
}
}
override fun allEvents(): Sequence<StoredEvent> =
override fun allEvents(): Sequence<StoredEvent> = withConnection {
connection.prepareStatement(
"""
SELECT * FROM events
@@ -208,8 +217,9 @@ class SqliteEventStore(
}
}
}.asSequence()
}
override fun allSessionIds(): Set<SessionId> =
override fun allSessionIds(): Set<SessionId> = withConnection {
mutableSetOf<SessionId>().apply {
connection.prepareStatement("SELECT DISTINCT session_id FROM events").use { ps ->
ps.executeQuery().use { rs ->
@@ -217,9 +227,19 @@ class SqliteEventStore(
}
}
}
}
// ---------- helpers ----------
/**
* Serializes all JDBC access to the shared [connection] on a single JVM monitor. Reads run on the
* caller thread while append's transaction runs on Dispatchers.IO; the coroutine [appendMutex] only
* excludes append-vs-append, so without this a read could hit the connection mid-transaction
* (SQLite JDBC is not thread-safe → corrupted tx state). ponytail: store-wide lock, one session at a
* time is fine here; shard per-connection only if read throughput ever matters.
*/
private inline fun <T> withConnection(block: () -> T): T = synchronized(connection) { block() }
private fun nextGlobalSequence(): Long =
connection.prepareStatement(
"SELECT COALESCE(MAX(sequence), 0) FROM events"