From d4b2d3f03447b011596ec0c67bd6d99f4d9915bf Mon Sep 17 00:00:00 2001 From: Valentin Haudiquet Date: Wed, 2 Dec 2026 18:08:22 +0100 Subject: [PATCH] fix(claw): wait out the write lock when recording a turn's events MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The recorded stream is what the chat surface replays, and an append that fails is a hole in it. The append read the next sequence and then wrote inside a deferred transaction, which is the shape SQLite answers with SQLITE_BUSY_SNAPSHOT — "database is locked", returned at once with the busy timeout never asked — as soon as the dispatcher's checkpoint on the same file committed in between. One streaming turn logged thirty-six of those inside five milliseconds: a burst of deltas the replay no longer holds. The transaction is immediate now, so the write lock is taken up front and the five-second busy timeout covers the wait. The new test holds the lock from a second connection and releases it mid-append: it failed with DatabaseBusy before, it waits and lands now. --- crates/rhema-claw/src/turn_store.rs | 47 +++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/crates/rhema-claw/src/turn_store.rs b/crates/rhema-claw/src/turn_store.rs index 4ef694f..2b3ba78 100644 --- a/crates/rhema-claw/src/turn_store.rs +++ b/crates/rhema-claw/src/turn_store.rs @@ -15,7 +15,7 @@ use std::path::Path; use std::sync::{Mutex, PoisonError}; -use rusqlite::params; +use rusqlite::{TransactionBehavior, params}; use serde_json::Value; use crate::hub::TurnEvent; @@ -99,7 +99,16 @@ impl TurnStore { return Ok(()); } let mut conn = self.conn.lock().unwrap_or_else(PoisonError::into_inner); - let tx = conn.transaction().map_err(TurnStoreError::from)?; + // Immediate, not deferred. The append reads the next sequence + // and then writes, and a deferred transaction that reads first + // and upgrades to a writer later is the shape SQLite answers + // with SQLITE_BUSY_SNAPSHOT — "database is locked", straight + // past the busy timeout — as soon as the dispatcher's checkpoint + // commits in between. Taking the write lock up front is what + // makes the timeout cover the wait. + let tx = conn + .transaction_with_behavior(TransactionBehavior::Immediate) + .map_err(TurnStoreError::from)?; let next: i64 = tx .query_row( "SELECT COALESCE(MAX(seq) + 1, 0) FROM turn_events WHERE key = ?1", @@ -308,6 +317,40 @@ mod tests { } } + /// The dispatcher checkpoints on the same file while a turn streams, + /// so an append can find the write lock taken. It has to wait for it + /// — a lost append is a hole in the replay — and a deferred + /// transaction that reads the next sequence first answers + /// "database is locked" instead of waiting. + #[test] + fn an_append_waits_for_another_connections_write_lock() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("turns.db"); + let store = TurnStore::open(&path).expect("store opens"); + let holder = rusqlite::Connection::open(&path).expect("second connection opens"); + holder + .execute_batch("BEGIN IMMEDIATE") + .expect("the other connection takes the write lock"); + let release = std::thread::spawn(move || { + std::thread::sleep(std::time::Duration::from_millis(200)); + holder + .execute_batch("COMMIT") + .expect("the other connection releases it"); + }); + + store + .append("interactive:web", &[text_delta("hi")]) + .expect("the append waits out the lock"); + release.join().unwrap(); + assert_eq!( + store + .events_from("interactive:web", -1, usize::MAX) + .expect("the events read back") + .len(), + 1 + ); + } + #[test] fn an_appended_history_round_trips_through_events() { let store = TurnStore::in_memory().expect("store opens");