fix(claw): wait out the write lock when recording a turn's events
android / debug (push) Successful in 2m38s
android / release (push) Successful in 2m5s
ci / checks (push) Successful in 2m48s
ci / test (push) Successful in 1m9s
ci / chart (push) Successful in 6s
ci / docker (push) Successful in 1m55s
ci / web (push) Successful in 14s
ci / publish-edge-images (deploy/docker/claw.Dockerfile, rhema-claw) (push) Successful in 2m0s
ci / publish-edge-images (deploy/docker/ui.Dockerfile, rhema-claw-ui) (push) Successful in 9s
ci / publish-edge-chart (push) Successful in 3s

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.
This commit is contained in:
vhaudiquet committed 2026-12-02 18:08:22 +01:00
1 parent ae69972e36
commit d4b2d3f034
1 file changed
+45 -2
+45 -2
View File
@@ -15,7 +15,7 @@
use std::path::Path; use std::path::Path;
use std::sync::{Mutex, PoisonError}; use std::sync::{Mutex, PoisonError};
use rusqlite::params; use rusqlite::{TransactionBehavior, params};
use serde_json::Value; use serde_json::Value;
use crate::hub::TurnEvent; use crate::hub::TurnEvent;
@@ -99,7 +99,16 @@ impl TurnStore {
return Ok(()); return Ok(());
} }
let mut conn = self.conn.lock().unwrap_or_else(PoisonError::into_inner); 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 let next: i64 = tx
.query_row( .query_row(
"SELECT COALESCE(MAX(seq) + 1, 0) FROM turn_events WHERE key = ?1", "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] #[test]
fn an_appended_history_round_trips_through_events() { fn an_appended_history_round_trips_through_events() {
let store = TurnStore::in_memory().expect("store opens"); let store = TurnStore::in_memory().expect("store opens");