Skip to content

A tiny friend in your Mac's notch and on your iPhone that keeps an eye on your AI coding agents: Claude Code, Codex, Cursor, Gemini CLI, Antigravity and more. Approve from the notch or your Lock Screen.

fix(settings): serialize saves to prevent temp file collisions (#858)

unknown2c9485224bb0
diff --git a/windows/src-tauri/src/lib.rs b/windows/src-tauri/src/lib.rs
index c8f57a90..823c024e 100644
--- a/windows/src-tauri/src/lib.rs
+++ b/windows/src-tauri/src/lib.rs
@@ -90,7 +90,7 @@ fn boot(app: AppHandle, shared: State<Shared>) -> BootInfo {
#[tauri::command]
fn save_settings(app: AppHandle, shared: State<Shared>, settings: Settings) {
- let (screen_changed, autostart_changed, shortcuts_changed) = {
+ let (settings, screen_changed, autostart_changed, shortcuts_changed) = {
let mut current = shared.settings.lock().unwrap();
let screen_changed = current.screen != settings.screen;
let autostart_changed = current.autostart != settings.autostart;
@@ -111,9 +111,8 @@ fn save_settings(app: AppHandle, shared: State<Shared>, settings: Settings) {
settings.phone_relay_url = current.phone_relay_url.clone();
settings.phone_chat_models = current.phone_chat_models.clone();
*current = settings;
- (screen_changed, autostart_changed, shortcuts_changed)
+ (current.clone(), screen_changed, autostart_changed, shortcuts_changed)
};
- let settings = shared.settings.lock().unwrap().clone();
if let Err(err) = settings::save(&settings) {
log::line(format!("could not save settings: {err}"));
}
diff --git a/windows/src-tauri/src/settings.rs b/windows/src-tauri/src/settings.rs
index 5fdbdc5f..b9656236 100644
--- a/windows/src-tauri/src/settings.rs
+++ b/windows/src-tauri/src/settings.rs
@@ -205,6 +205,14 @@ fn not_loaded() -> MutexGuard<'static, Vec<PathBuf>> {
NOT_LOADED.lock().unwrap_or_else(PoisonError::into_inner)
}
+/// Serializes saves across all threads so concurrent writes do not collide on
+/// the temporary file or interleave file renames.
+static SAVE_LOCK: Mutex<()> = Mutex::new(());
+
+fn save_lock() -> MutexGuard<'static, ()> {
+ SAVE_LOCK.lock().unwrap_or_else(PoisonError::into_inner)
+}
+
/// One line in coucou.log. Tests must never write to the real one.
fn note(message: String) {
#[cfg(not(test))]
@@ -390,6 +398,7 @@ fn write_whole(file: &mut std::fs::File, bytes: &[u8]) -> std::io::Result<()> {
/// `save`, for the file at `path`, whose directory already exists.
fn save_to(path: &Path, settings: &Settings) -> std::io::Result<()> {
+ let _lock = save_lock();
keep_what_was_not_loaded(path)?;
let json = serde_json::to_vec_pretty(settings)
.map_err(|e| std::io::Error::new(ErrorKind::InvalidData, e))?;
@@ -1011,4 +1020,28 @@ mod tests {
assert_eq!(std::fs::read(&file).unwrap(), CUSTOM.as_bytes());
let _ = std::fs::remove_dir_all(&dir);
}
+
+ #[test]
+ fn concurrent_saves_never_collide() {
+ let (dir, file) = scratch("concurrent");
+ let handles: Vec<_> = (0..20)
+ .map(|i| {
+ let path = file.clone();
+ std::thread::spawn(move || {
+ let mut s = Settings::default();
+ s.sound_volume = (i as f64) * 0.05;
+ save_to(&path, &s)
+ })
+ })
+ .collect();
+
+ for h in handles {
+ assert!(h.join().unwrap().is_ok());
+ }
+
+ // Must still be valid settings that load cleanly without corruption
+ let loaded = load_from(&file);
+ assert!(loaded.sound_volume >= 0.0);
+ let _ = std::fs::remove_dir_all(&dir);
+ }
}