Skip to content

Commit 94eeda7

Browse files
branchseerclaude
andcommitted
fix(cache): use file lock instead of IF NOT EXISTS for db init
Use an exclusive file lock (db_open.lock) to prevent race conditions when multiple processes initialize the database simultaneously, instead of relying on CREATE TABLE IF NOT EXISTS. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent e4e2643 commit 94eeda7

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

  • crates/vite_task/src/session/cache

crates/vite_task/src/session/cache/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
pub mod display;
44

5-
use std::{fmt::Display, io::Write, sync::Arc, time::Duration};
5+
use std::{fmt::Display, fs::File, io::Write, sync::Arc, time::Duration};
66

77
use bincode::{Decode, Encode, decode_from_slice, encode_to_vec};
88
// Re-export display functions for convenience
@@ -71,6 +71,11 @@ impl ExecutionCache {
7171
tracing::info!("Creating task cache directory at {:?}", path);
7272
std::fs::create_dir_all(path)?;
7373

74+
// Use file lock to prevent race conditions when multiple processes initialize the database
75+
let lock_path = path.join("db_open.lock");
76+
let lock_file = File::create(lock_path.as_path())?;
77+
lock_file.lock()?;
78+
7479
let db_path = path.join("cache.db");
7580
let conn = Connection::open(db_path.as_path())?;
7681
conn.execute_batch("PRAGMA journal_mode=WAL;")?;
@@ -101,6 +106,7 @@ impl ExecutionCache {
101106
}
102107
}
103108
}
109+
// Lock is released when lock_file is dropped
104110
Ok(Self { conn: Mutex::new(conn) })
105111
}
106112

0 commit comments

Comments
 (0)