perf(windows): the reaper releases the window manager lock before it notifies and writes the hwnd file

The reaper held the lock across workspace.update(), notify_subscribers for each
reaped window and the komorebi.hwnd.json write (the stall analysis: real holds
of 1.0 to 1.3 s in the state_fidelity and placement scenarios, with a command
dropped meanwhile). Now it reaps and updates under the lock, builds the
Destroy notifications there (each carries the state as it was after that
window was reaped), takes a sequence number for the list of known handles
(HWND_LIST_SEQ), and then drops the lock; the subscribers are pushed to, the
borders notified and the file written afterwards. The write stays synchronous
and on the same thread (no writer thread).

Ordering. The notifications still go out in the order they were made and each
carries its own snapshot, but a command may now run between the reap and the
push, so its notification can reach subscribers before the Destroy of the
reaper; subscribers get full states, so the later one is still correct. The
hwnd file: update_known_hwnds (commands) and the reaper both write it through
reaper::write_known_hwnds(seq, list), which holds a small mutex around the write
and skips a list older than the one in the file, so the two writers do not
interleave and an older list never overwrites a newer one. If workspace.update()
fails for a window (the existing ? ), the notifications made before it are
dropped with the lock released; before they had been sent.

BEHAVIOUR: notification order relative to commands, and the file written
after the unlock. Gate needed: Windows e2e lifecycle, lifecycle_hidden,
state_fidelity, placement, commands, and the lock stress; the next hold ranking
should lose the reaper holds. Verified: tools/fast-check Windows clippy
-D warnings PASS; e2e not run.
This commit is contained in:
LGUG2Z committed 2026-11-05 15:03:26 -08:00
1 parent e24a446fe3
commit d0ee249ba2
2 files changed
+55 -36

No files matched your search

+49 -18
View File
@@ -80,6 +80,9 @@ fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> 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<Mutex<WindowManager>>) -> 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::<Vec<_>>();
// 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<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<_>>())?;
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<u64> = 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(())
}
+6 -18
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;
@@ -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::<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);
}
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::<Vec<_>>(),
) {
tracing::error!("Failed to save list of known_hwnds on file: {}", error);
}
// Store new hwnds