perf(windows): write komorebi.hwnd.json from a writer thread, not under the window manager lock

update_known_hwnds (at the end of every command) and the reaper opened,
truncated and wrote komorebi.hwnd.json with the window manager locked. They
now hand the list to reaper::save_known_hwnds, which keeps only the latest
pending list and writes it from a thread of its own; errors are logged there
as before (the reaper no longer returns the write error).

BEHAVIOUR: the file is written a moment after the lock is released, not before
the command returns, and when several updates arrive together only the last
list is written. Nothing in the daemon reads the file. Gate needed: Windows e2e
commands, lifecycle, and the lock stress. Verified: Windows clippy -D warnings
via tools/fast-check PASS; e2e not run.
This commit is contained in:
LGUG2Z committed 2026-11-05 15:03:26 -08:00
1 parent bdad4284a5
commit c89c97a429
2 files changed
+49 -28

No files matched your search

+47 -8
View File
@@ -134,14 +134,7 @@ fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> color_eyre::Result<()>
}
// Save to file
let hwnd_json = DATA_DIR.join("komorebi.hwnd.json");
let file = OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open(hwnd_json)?;
serde_json::to_writer_pretty(&file, &wm.known_hwnds.keys().collect::<Vec<_>>())?;
save_known_hwnds(wm.known_hwnds.keys().copied().collect());
}
Ok(())
@@ -213,3 +206,49 @@ fn find_orphans() -> color_eyre::Result<()> {
}
}
}
static PENDING_HWNDS: Mutex<Option<Vec<isize>>> = Mutex::new(None);
static PENDING_HWNDS_CHANGED: parking_lot::Condvar = parking_lot::Condvar::new();
static HWNDS_WRITER: std::sync::Once = std::sync::Once::new();
/// Saves the list of known handles to `komorebi.hwnd.json`, from a thread of its own: writing the
/// file with the window manager locked made every other thread wait for the disk. Only the latest
/// list is written when several arrive before the writer is done, as the file only ever says what
/// is known now.
pub fn save_known_hwnds(hwnds: Vec<isize>) {
*PENDING_HWNDS.lock() = Some(hwnds);
HWNDS_WRITER.call_once(|| {
std::thread::spawn(|| {
loop {
let hwnds = {
let mut pending = PENDING_HWNDS.lock();
while pending.is_none() {
PENDING_HWNDS_CHANGED.wait(&mut pending);
}
pending.take().unwrap_or_default()
};
let hwnd_json = DATA_DIR.join("komorebi.hwnd.json");
match OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open(hwnd_json)
{
Ok(file) => {
if let Err(error) = serde_json::to_writer_pretty(&file, &hwnds) {
tracing::error!(
"Failed to save list of known_hwnds on file: {}",
error
);
}
}
Err(error) => {
tracing::error!("Failed to save list of known_hwnds on file: {}", error);
}
}
}
});
});
PENDING_HWNDS_CHANGED.notify_one();
}
+2 -20
View File
@@ -5,7 +5,6 @@ use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
use std::env::temp_dir;
use std::fs::OpenOptions;
use std::io::ErrorKind;
use std::net::Shutdown;
use std::num::NonZeroUsize;
@@ -4008,25 +4007,8 @@ impl WindowManager {
*reaper_cache = known_hwnds.clone();
}
// Save to file
let hwnd_json = DATA_DIR.join("komorebi.hwnd.json");
match OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open(hwnd_json)
{
Ok(file) => {
if let Err(error) =
serde_json::to_writer_pretty(&file, &known_hwnds.keys().collect::<Vec<_>>())
{
tracing::error!("Failed to save list of known_hwnds on file: {}", error);
}
}
Err(error) => {
tracing::error!("Failed to save list of known_hwnds on file: {}", error);
}
}
// Save to file, from the writer thread of the reaper
crate::reaper::save_known_hwnds(known_hwnds.keys().copied().collect());
// Store new hwnds
self.known_hwnds = known_hwnds;