revert: write komorebi.hwnd.json from a writer thread

Reverts 3d2386e3 (review finding 8). The detached latest-wins writer thread
left the file lagging behind the daemon (a command that returned, or stop, could
precede the write, and std::process::exit cut a write in progress), and it
opened and truncated the file in place, so a reader could see it half written.
The file is small and the synchronous write was the original behaviour; the
stall analysis did not show it to be a cause of any hold (the per-step and
lock-holder instrumentation of the same stack will show whether it ever is).
Restoring the synchronous write in update_known_hwnds and the reaper is the
simplest form that is correct: the file is complete when the command returns
and when stop exits.

Behaviour is the original again for the file. Verified: Windows clippy
-D warnings via tools/fast-check PASS; e2e: lifecycle and commands (the file is
written again before the reply).
This commit is contained in:
LGUG2Z committed 2026-11-05 15:03:26 -08:00
1 parent 6d1262b4a3
commit b5a634f59c
2 files changed
+28 -49

No files matched your search

+8 -47
View File
@@ -134,7 +134,14 @@ fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> color_eyre::Result<()>
}
// Save to file
save_known_hwnds(wm.known_hwnds.keys().copied().collect());
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<_>>())?;
}
Ok(())
@@ -206,49 +213,3 @@ 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();
}
+20 -2
View File
@@ -5,6 +5,7 @@ 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,8 +4009,25 @@ impl WindowManager {
*reaper_cache = known_hwnds.clone();
}
// Save to file, from the writer thread of the reaper
crate::reaper::save_known_hwnds(known_hwnds.keys().copied().collect());
// 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);
}
}
// Store new hwnds
self.known_hwnds = known_hwnds;