Stop the settings write loop between windows

Two or more windows saved the appearance section to each other without end. The host stores object keys in alphabetical order. ThemeSync compared the stored value and the normalized value as JSON text, so a different key order started a new save in each window. Each save replaced settings.json, about 150 times each second. On a SATA SSD without a DRAM cache, these writes stalled all disk access for 6 to 8 seconds, about every 40 seconds.

- ThemeSync compares the two values with sameSettingsValue, which ignores the key order.
- The host skips a write when the section already holds the value, and sends no settings-changed event. This also stops a similar loop in another section.
- New tests: key order in sameSettingsValue, an appearance from the host that needs no new save, and an unchanged host write.
This commit is contained in:
Knotty Wyvern
2026-11-25 07:32:37 -07:00
parent b52eeb14a1
commit 1f3179facc
5 changed files with 97 additions and 5 deletions
+46 -3
View File
@@ -2,7 +2,8 @@
//!
//! The file holds a `schema` number, a `revision` number, and one value for each section.
//! The host does not interpret the section values. The frontend normalizes them.
//! Every write replaces the complete file atomically and sends `settings-changed` to all windows.
//! A write that changes a section replaces the complete file atomically and sends `settings-changed` to all windows.
//! A write of the value that a section already holds changes nothing and sends no event.
//! Plugin settings stay in the app data folder (see `plugins.rs`).
use std::io;
@@ -178,6 +179,19 @@ impl Store {
Ok(Change { section: section.into(), revision: next, value })
}
/// Like `write`, but when the file already holds `value` in this section, the file stays as it is.
/// The change then has the current revision, and `false` tells the caller to send no event.
/// The compare ignores the key order of objects. Without this check, windows that save the same value
/// can start each other again without end, and each write replaces the file.
pub fn write_if_changed(&mut self, section: &str, value: Value) -> Result<(Change, bool)> {
check_section(section)?;
let loaded = self.load()?;
if loaded.exists && *loaded.document.get(section).unwrap_or(&Value::Null) == value {
return Ok((Change { section: section.into(), revision: revision(&loaded.document), value }, false));
}
Ok((self.write(section, value)?, true))
}
/// Write the first file from the migrated values. When a file exists, keep it and return it unchanged.
pub fn migrate(&mut self, sections: Map<String, Value>) -> Result<Snapshot> {
for name in sections.keys() { check_section(name)?; }
@@ -229,9 +243,9 @@ pub async fn settings_read(app: AppHandle) -> Result<Snapshot> {
#[tauri::command]
pub async fn settings_write(app: AppHandle, section: String, value: Value) -> Result<Change> {
let change = with_store(&app, move |store| store.write(&section, value)).await?;
let (change, saved) = with_store(&app, move |store| store.write_if_changed(&section, value)).await?;
// Every window shows the same preferences, so the change goes to all of them.
let _ = app.emit(CHANGED_EVENT, &change);
if saved { let _ = app.emit(CHANGED_EVENT, &change); }
Ok(change)
}
@@ -292,6 +306,35 @@ mod tests {
fs::remove_dir_all(dir).unwrap();
}
#[test]
fn a_write_of_the_stored_value_keeps_the_file_and_the_revision() {
let (dir, path) = fixture("unchanged");
let mut store = Store::new(path.clone());
// A missing file gets created, also for a value that matches the empty default.
let (created, saved) = store.write_if_changed("language", Value::Null).unwrap();
assert!(saved);
assert_eq!(created.revision, 1);
let (first, saved) = store.write_if_changed("appearance", json!({ "theme": "harvest-moon", "mode": "dark", "transparency": { "enabled": true, "amount": 18 } })).unwrap();
assert!(saved);
assert_eq!(first.revision, 2);
let before = fs::read(&path).unwrap();
// The same value in a different key order changes nothing.
let (again, saved) = store.write_if_changed("appearance", json!({ "transparency": { "amount": 18, "enabled": true }, "mode": "dark", "theme": "harvest-moon" })).unwrap();
assert!(!saved, "No save and no event");
assert_eq!(again.revision, 2);
assert_eq!(fs::read(&path).unwrap(), before, "The file stays as it is");
// A missing section and `null` are the same.
assert!(!store.write_if_changed("terminal", Value::Null).unwrap().1);
// A different value saves as usual.
let (changed, saved) = store.write_if_changed("appearance", json!({ "theme": "harvest-moon", "mode": "light" })).unwrap();
assert!(saved);
assert_eq!(changed.revision, 3);
assert_eq!(on_disk(&path)["appearance"], json!({ "mode": "light", "theme": "harvest-moon" }));
assert!(store.write_if_changed("plugins", json!({})).is_err(), "Unknown sections stay refused");
assert_eq!(files(&dir), vec![FILE_NAME.to_owned()], "No temporary files remain");
fs::remove_dir_all(dir).unwrap();
}
#[test]
fn writes_reject_unknown_sections_and_values_above_the_limit() {
let (dir, path) = fixture("limits");