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): allow startup recovery after a force quit (#7043)

Ezra Ellettecff038807747
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/main.rs b/apps/screenpipe-app-tauri/src-tauri/src/main.rs
index 87944d888..2f04ffa7a 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/main.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/main.rs
@@ -478,9 +478,9 @@ async fn main() {
// Point debug builds at their own data dir and ports so `bun tauri dev`
// can't hand off to (or kill) an installed production app. Must run before
- // the DB-recovery-lock check, the /focus single-instance handoff and the
- // telemetry store read below — all of which resolve the data directory or
- // the focus port. No-op in release builds. See `dev_isolation`.
+ // the /focus single-instance handoff and telemetry store read below, which
+ // resolve the data directory or focus port. No-op in release builds.
+ // See `dev_isolation`.
dev_isolation::apply();
#[cfg(target_os = "linux")]
@@ -500,45 +500,6 @@ async fn main() {
#[cfg(target_os = "windows")]
windows_webview_env::install_user_data_dir();
- // Refuse to launch while a `screenpipe db recover|cleanup` operation is in
- // progress. The CLI writes ~/.screenpipe/.db_recovery.lock before doing
- // anything destructive; if the user double-clicks the app icon mid-recovery,
- // we'd otherwise race the swap and corrupt the DB again. The CLI heartbeats
- // the lock every 30 s, so a fresh mtime means the op is genuinely live.
- //
- // Escape hatches (in order of preference):
- // 1. `screenpipe db unlock` — friendly path
- // 2. SCREENPIPE_IGNORE_DB_LOCK=1 env var — bypass on this launch only
- // 3. `rm ~/.screenpipe/.db_recovery.lock` — manual
- //
- // See `crates/screenpipe-engine/src/cli/db.rs`.
- if std::env::var("SCREENPIPE_IGNORE_DB_LOCK").ok().as_deref() != Some("1") {
- let lock_path =
- screenpipe_core::paths::default_screenpipe_data_dir().join(".db_recovery.lock");
- if let Ok(metadata) = std::fs::metadata(&lock_path) {
- let stale = metadata
- .modified()
- .ok()
- .and_then(|m| m.elapsed().ok())
- .map(|d| d.as_secs() > 3600)
- .unwrap_or(false);
- if stale {
- let _ = std::fs::remove_file(&lock_path);
- } else {
- let body = std::fs::read_to_string(&lock_path).unwrap_or_default();
- eprintln!(
- "screenpipe: a `screenpipe db ...` operation is in progress.\n\
- lock: {}\n\
- content: {}\n\
- options:\n • wait for the op to finish, then re-open the app\n • run `screenpipe db unlock` if you're sure it's stuck\n • set SCREENPIPE_IGNORE_DB_LOCK=1 and retry to bypass this check",
- lock_path.display(),
- body.trim(),
- );
- std::process::exit(2);
- }
- }
- }
-
// Export the Windows root/CA cert stores to a PEM file and set
// NODE_EXTRA_CA_CERTS before any bun/node subprocess can spawn. Fixes
// "unable to verify the first certificate" on corporate networks where
diff --git a/crates/screenpipe-engine/src/cli/db.rs b/crates/screenpipe-engine/src/cli/db.rs
index df10f1dcf..18027a073 100644
--- a/crates/screenpipe-engine/src/cli/db.rs
+++ b/crates/screenpipe-engine/src/cli/db.rs
@@ -15,9 +15,10 @@
//! * Heartbeated every 30 s by a background thread so a long recovery
//! (multi-GB DB) doesn't look stale.
//! * Released on normal Drop, on SIGINT, and on SIGTERM.
-//! * The desktop app refuses to start while the lock is fresh
-//! (`apps/screenpipe-app-tauri/src-tauri/src/main.rs`); env var
-//! `SCREENPIPE_IGNORE_DB_LOCK=1` is the escape hatch.
+//! * Desktop and CLI recording acquire the same lock through
+//! `prepare_database_startup`, which reclaims dead owners before
+//! reconciling interrupted recovery. A live owner blocks database startup,
+//! while the desktop shell and its logs remain available.
//!
//! ## When the lock is "stale"
//!
@@ -2166,6 +2167,85 @@ mod recovery_tests {
assert!(pid_alive(std::process::id()));
}
+ #[tokio::test]
+ async fn startup_recovers_immediately_after_lock_owner_is_force_quit() {
+ const CHILD_DATA_DIR: &str = "SCREENPIPE_TEST_FORCE_QUIT_DB_LOCK_DIR";
+ if let Some(data_dir) = std::env::var_os(CHILD_DATA_DIR).map(PathBuf::from) {
+ let _guard = prepare_database_startup(&data_dir).await.unwrap();
+ let live = data_dir.join("db.sqlite");
+ let recovery = data_dir.join("db-recovery-force-quit");
+ let source = recovery.join("source-generation");
+ fs::create_dir_all(&source).unwrap();
+ atomic_write_manifest(
+ &recovery.join(RECOVERY_MANIFEST_FILE),
+ &test_manifest(&live),
+ )
+ .unwrap();
+ fs::rename(sqlite_sidecar(&live, "-wal"), source.join("db.sqlite-wal")).unwrap();
+ fs::write(data_dir.join("ready"), b"lock held; WAL move interrupted").unwrap();
+ // Bound the child lifetime even if the parent test fails.
+ tokio::time::sleep(Duration::from_secs(30)).await;
+ panic!("parent did not force quit the lock owner");
+ }
+
+ let dir = tempfile::tempdir().unwrap();
+ let live = dir.path().join("db.sqlite");
+ write_generation(&live);
+ let mut child = std::process::Command::new(std::env::current_exe().unwrap())
+ .args([
+ "--exact",
+ "cli::db::recovery_tests::startup_recovers_immediately_after_lock_owner_is_force_quit",
+ "--nocapture",
+ ])
+ .env(CHILD_DATA_DIR, dir.path())
+ .spawn()
+ .unwrap();
+ let ready = tokio::time::timeout(Duration::from_secs(10), async {
+ while !dir.path().join("ready").exists() {
+ if child.try_wait().unwrap().is_some() {
+ return false;
+ }
+ tokio::time::sleep(Duration::from_millis(20)).await;
+ }
+ true
+ })
+ .await;
+ if !matches!(ready, Ok(true)) {
+ let _ = child.kill();
+ let _ = child.wait();
+ panic!("child did not acquire the database startup lock");
+ }
+
+ let blocked = prepare_database_startup(dir.path()).await;
+ let wal_still_archived = !sqlite_sidecar(&live, "-wal").exists();
+ child.kill().unwrap();
+ child.wait().unwrap();
+ assert!(blocked.is_err(), "a live owner must keep recovery excluded");
+ assert!(
+ wal_still_archived,
+ "blocked startup must not reconcile the WAL"
+ );
+ assert!(
+ dir.path().join(LOCK_FILE).exists(),
+ "force quit leaves the lock file"
+ );
+ assert!(!pid_alive(child.id()));
+
+ let _guard = prepare_database_startup(dir.path()).await.unwrap();
+ assert_eq!(fs::read(&live).unwrap(), b"database-bytes");
+ assert_eq!(
+ fs::read(sqlite_sidecar(&live, "-wal")).unwrap(),
+ b"wal-bytes"
+ );
+ assert_eq!(
+ fs::read(sqlite_sidecar(&live, "-shm")).unwrap(),
+ b"shm-bytes"
+ );
+ let replacement: LockPayload =
+ serde_json::from_slice(&fs::read(dir.path().join(LOCK_FILE)).unwrap()).unwrap();
+ assert_eq!(replacement.pid, std::process::id());
+ }
+
#[tokio::test]
async fn end_to_end_recovery_salvages_corruption_installs_fresh_inode_and_archives_original() {
let dir = tempfile::tempdir().expect("tempdir");