Skip to content

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

fix: rollback uncommitted ImmediateTx via spawned task instead of leaking connections

Louis Beaumonte0fb66f4ba74
diff --git a/crates/screenpipe-db/src/db.rs b/crates/screenpipe-db/src/db.rs
index 99a2c48c2..b8dec7ed8 100644
--- a/crates/screenpipe-db/src/db.rs
+++ b/crates/screenpipe-db/src/db.rs
@@ -95,23 +95,15 @@ impl Drop for ImmediateTx {
// into the task, so if rollback succeeds, the clean connection is
// returned to the pool when dropped. If rollback fails, we detach.
warn!("ImmediateTx dropped without commit — scheduling rollback");
- // Use tokio::task::block_in_place if inside a tokio worker thread,
- // otherwise detach as fallback. block_in_place moves the current
- // task off the worker thread so blocking is safe.
- if tokio::runtime::Handle::try_current().is_ok() {
- tokio::task::block_in_place(|| {
- tokio::runtime::Handle::current().block_on(async {
- if let Err(e) = sqlx::query("ROLLBACK").execute(&mut *conn).await {
- error!("failed to rollback on drop: {}", e);
- let _raw = conn.detach();
- }
- // Connection is clean — returned to pool when `conn` drops
- });
- });
- } else {
- // No tokio runtime available — detach as fallback
- let _raw = conn.detach();
- }
+ // Rollback and return the connection to the pool. We spawn an async
+ // task because Drop is synchronous. The connection is moved into the
+ // task; if rollback succeeds, it returns to the pool when dropped.
+ tokio::spawn(async move {
+ if let Err(e) = sqlx::query("ROLLBACK").execute(&mut *conn).await {
+ error!("failed to rollback on drop: {}", e);
+ let _raw = conn.detach();
+ }
+ });
}
}
// _write_permit is dropped here, releasing the semaphore for the next writer
diff --git a/crates/screenpipe-db/tests/db.rs b/crates/screenpipe-db/tests/db.rs
index 99d099858..678caf929 100644
--- a/crates/screenpipe-db/tests/db.rs
+++ b/crates/screenpipe-db/tests/db.rs
@@ -1569,4 +1569,38 @@ mod tests {
.unwrap();
assert_eq!(count, 0, "Should count zero results for non-matching query");
}
+
+ #[tokio::test]
+ async fn test_dropped_tx_rolls_back_and_returns_connection() {
+ let db = setup_test_db().await;
+
+ // Insert an audio chunk via a committed transaction (baseline)
+ let chunk_id = db.insert_audio_chunk("rollback_test.mp4").await.unwrap();
+ assert!(chunk_id > 0);
+
+ // Start a transaction, insert a row, then DROP without committing.
+ // The Drop impl should rollback and return the connection to the pool.
+ {
+ let mut tx = db.begin_immediate_with_retry().await.unwrap();
+ sqlx::query("INSERT INTO audio_chunks (file_path, timestamp) VALUES ('should_not_exist.mp4', datetime('now'))")
+ .execute(&mut **tx.conn())
+ .await
+ .unwrap();
+ // tx is dropped here without commit — should rollback
+ }
+
+ // Verify the uncommitted row was rolled back
+ let row: (i64,) = sqlx::query_as("SELECT COUNT(*) FROM audio_chunks WHERE file_path = 'should_not_exist.mp4'")
+ .fetch_one(&db.pool)
+ .await
+ .unwrap();
+ assert_eq!(row.0, 0, "Uncommitted row should have been rolled back");
+
+ // Verify the pool is still healthy — we can acquire connections and do work.
+ // If the connection was leaked (detached), the pool would eventually exhaust.
+ for i in 0..5 {
+ let id = db.insert_audio_chunk(&format!("pool_health_{}.mp4", i)).await.unwrap();
+ assert!(id > 0, "Pool should still be healthy after rollback");
+ }
+ }
}