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(db): pre-convert journal_mode=WAL before pool creation

rhodium-inc6fe1e8aa437f
diff --git a/crates/screenpipe-db/src/db.rs b/crates/screenpipe-db/src/db.rs
index 81c30a1eb..c1353766c 100644
--- a/crates/screenpipe-db/src/db.rs
+++ b/crates/screenpipe-db/src/db.rs
@@ -11,6 +11,8 @@ use sqlx::migrate::MigrateDatabase;
use sqlx::pool::PoolConnection;
use sqlx::sqlite::{SqliteConnectOptions, SqlitePool, SqlitePoolOptions};
use sqlx::Column;
+use sqlx::ConnectOptions;
+use sqlx::Connection;
use sqlx::Error as SqlxError;
use sqlx::Row;
use sqlx::Sqlite;
@@ -300,6 +302,21 @@ impl DatabaseManager {
// Crash recovery: ~200ms replay at most.
.pragma("wal_autocheckpoint", "4000");
+ // Fresh DB conversion to journal_mode=WAL requires an exclusive lock.
+ // When the pool opens read_pool + write_pool connections concurrently,
+ // each connection tries the WAL conversion and they race, with losers
+ // failing initialization with SQLITE_BUSY ("database is locked")
+ // (~50% reproduction with fresh data-dir). Pre-converting via a single
+ // connection before pool creation makes pool connections see a WAL'd
+ // DB and skip conversion entirely — no race.
+ {
+ let mut conn = connect_options.connect().await?;
+ sqlx::query("PRAGMA journal_mode=WAL")
+ .execute(&mut conn)
+ .await?;
+ conn.close().await?;
+ }
+
// Read pool: handles all SELECT queries (search, timeline, API, pipes).
let read_pool = SqlitePoolOptions::new()
.max_connections(config.read_pool_max)