diff --git a/komorebi/src/reaper.rs b/komorebi/src/reaper.rs index 932d4f0f..02904181 100644 --- a/komorebi/src/reaper.rs +++ b/komorebi/src/reaper.rs @@ -80,6 +80,9 @@ fn handle_notifications(wm: Arc>) -> color_eyre::Result<()> let mut wm = crate::lock_holder::lock(&wm, "reaper"); let mut update_borders = false; + // what the subscribers are told, and in what state, is decided here with the lock held and + // sent after it is released (a subscriber, or the disk, must not hold the lock) + let mut notifications = vec![]; for (hwnd, (m_idx, w_idx)) in orphan_hwnds.iter() { if let Some(monitor) = wm.monitors_mut().get_mut(*m_idx) { @@ -116,16 +119,23 @@ fn handle_notifications(wm: Arc>) -> color_eyre::Result<()> wm.known_hwnds.remove(hwnd); let window = Window::from(*hwnd); - notify_subscribers( - crate::Notification { - event: NotificationEvent::WindowManager(WindowManagerEvent::Destroy( - WinEvent::ObjectDestroy, - window, - )), - state: wm.as_ref().into(), - }, - true, - )?; + notifications.push(crate::Notification { + event: NotificationEvent::WindowManager(WindowManagerEvent::Destroy( + WinEvent::ObjectDestroy, + window, + )), + state: wm.as_ref().into(), + }); + } + + let seq = HWND_LIST_SEQ.fetch_add(1, std::sync::atomic::Ordering::SeqCst) + 1; + let known_hwnds = wm.known_hwnds.keys().copied().collect::>(); + + // the lock is released here, before the subscribers are pushed to and the file is written + drop(wm); + + for notification in notifications { + notify_subscribers(notification, true)?; } if update_borders { @@ -133,14 +143,7 @@ fn handle_notifications(wm: Arc>) -> 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::>())?; + write_known_hwnds(seq, &known_hwnds)?; } Ok(()) @@ -212,3 +215,31 @@ fn find_orphans() -> color_eyre::Result<()> { } } } + +/// Numbers the lists of known handles, in the order the window manager decided them (taken with +/// the window manager locked), so that a list written late is not written over a newer one +pub static HWND_LIST_SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); + +/// The number of the list that is in `komorebi.hwnd.json`, and the lock that serialises the writes +static HWND_FILE: Mutex = Mutex::new(0); + +/// Writes `komorebi.hwnd.json`, synchronously, unless a newer list (`seq` from +/// [`HWND_LIST_SEQ`]) has been written already. The reaper calls it after it has released the +/// window manager lock, the commands with it held: the writes do not interleave and the file is +/// never overwritten by an older list. +pub fn write_known_hwnds(seq: u64, hwnds: &[isize]) -> std::io::Result<()> { + let mut written = HWND_FILE.lock(); + if seq < *written { + return Ok(()); + } + + let file = OpenOptions::new() + .write(true) + .truncate(true) + .create(true) + .open(DATA_DIR.join("komorebi.hwnd.json"))?; + serde_json::to_writer_pretty(&file, hwnds)?; + *written = seq; + + Ok(()) +} diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index 572d06ee..2abcb4f4 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -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; @@ -4007,23 +4006,12 @@ impl WindowManager { } // 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::>()) - { - 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); - } + let seq = crate::reaper::HWND_LIST_SEQ.fetch_add(1, Ordering::SeqCst) + 1; + if let Err(error) = crate::reaper::write_known_hwnds( + seq, + &known_hwnds.keys().copied().collect::>(), + ) { + tracing::error!("Failed to save list of known_hwnds on file: {}", error); } // Store new hwnds