YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
fix(db): clear stale txn before BEGIN IMMEDIATE (CLI-RC / CLI-SR write wedge) (#4607)
Louis Beaumont16e78fea36ef
diff --git a/crates/screenpipe-db/src/write_queue.rs b/crates/screenpipe-db/src/write_queue.rsindex 5163c2bc3..3704e83a4 100644--- a/crates/screenpipe-db/src/write_queue.rs+++ b/crates/screenpipe-db/src/write_queue.rs@@ -795,6 +795,28 @@ async fn execute_batch(}};+ // Proactively clear any transaction a prior batch left open on this+ // pooled connection (sqlx's SQLite pool does not reset connections on+ // release). Without this, the BEGIN IMMEDIATE below fails with+ // "cannot start a transaction within a transaction" (SQLITE code 1) and+ // the batch errors out *before* the reactive rollback handler can recover+ // it — the first-failure event still reaching Sentry as+ // SCREENPIPE-CLI-RC / CLI-SR. It never discards committed data, only an+ // orphaned uncommitted transaction that was already doomed. The reactive+ // handler below stays as a belt-and-suspenders net.+ //+ // `Ok` means the ROLLBACK actually cleared an orphaned transaction — i.e.+ // a poisoned connection was detected and recovered. Surface that at warn!+ // (a breadcrumb, NOT a Sentry issue under the default tracing+ // EventFilter), so the poisoning stays observable instead of silently+ // masked — without re-flooding Sentry. The common `Err` is the harmless+ // "no transaction is active" no-op on a clean connection; ignore it.+ if sqlx::query("ROLLBACK").execute(&mut *conn).await.is_ok() {+ warn!(+ "write_queue: cleared an orphaned transaction on a pooled connection before BEGIN (recovered a poisoned connection that would have failed 'cannot start a transaction within a transaction')"+ );+ }+match sqlx::query("BEGIN IMMEDIATE").execute(&mut *conn).await {Ok(_) => {conn_opt = Some(conn);@@ -2556,6 +2578,41 @@ mod tests {);}+ /// Proves the proactive `ROLLBACK` before `BEGIN IMMEDIATE` in the drain+ /// loop: a connection left mid-transaction (the poison behind CLI-RC/CLI-SR)+ /// makes the next BEGIN fail with the nested-transaction error; a ROLLBACK+ /// clears it so BEGIN then succeeds, and a ROLLBACK on a clean connection is+ /// a harmless ignorable error (never a panic, never data loss).+ #[tokio::test]+ async fn rollback_before_begin_clears_a_stale_transaction() {+ let pool = sqlx::sqlite::SqlitePoolOptions::new()+ .max_connections(1)+ .connect("sqlite::memory:")+ .await+ .unwrap();+ let mut conn = pool.acquire().await.unwrap();++ // Poison: leave a transaction open (as a returned-mid-batch connection would).+ sqlx::query("BEGIN IMMEDIATE")+ .execute(&mut *conn)+ .await+ .unwrap();+ // Without clearing, the next BEGIN IMMEDIATE fails as a nested transaction.+ let nested = sqlx::query("BEGIN IMMEDIATE").execute(&mut *conn).await;+ assert!(is_nested_transaction_error(&nested.unwrap_err()));++ // The proactive rollback clears the orphaned transaction...+ let _ = sqlx::query("ROLLBACK").execute(&mut *conn).await;+ // ...so BEGIN IMMEDIATE now succeeds.+ sqlx::query("BEGIN IMMEDIATE")+ .execute(&mut *conn)+ .await+ .unwrap();+ // Clean-up, then confirm a ROLLBACK with no active txn is a harmless error.+ sqlx::query("ROLLBACK").execute(&mut *conn).await.unwrap();+ assert!(sqlx::query("ROLLBACK").execute(&mut *conn).await.is_err());+ }+/// `Database` errors flow through `is_fatal_sqlite_message`: a/// genuinely benign one (no such table) must NOT be classified as/// a connection error or the batch loop would discard whole