fix(windows): the lock-holder record is released with the lock guard, not after it

The Held record was a separate local next to the lock guard, so it outlived
the guard where the guard was dropped early: in the border manager the record
ran on through everything the thread does after drop(state) (foreground
lookup, border windows, painting), and in the monitor reconciliator through the
3 s thread::sleep of the "potential monitor removal" verification, which runs
with the lock released. The stall analysis of the cand5 gate counted both as
holds (rank 2 and 3 of the ranking; artifacts). lock_holder::lock(&mutex,
site) now returns a HeldGuard that owns the lock guard and the record, derefs
to the window manager, and drops the record first and the guard second, so
drop(guard) ends both together. All six sites that recorded a holder use it
(process_event, reaper, border manager, monitor reconciliator including its two
re-locks, transparency and stackbar managers); the per-command record in
process_command stays (it is dropped just before the transport's guard).

Diagnostics only, no behaviour change (the locks are taken and released where
they were). Verified: tools/fast-check Windows clippy -D warnings (default
members, own target dir) PASS; e2e: none needed, the next gate's hold ranking
should lose the two artifacts.
This commit is contained in:
LGUG2Z committed 2026-11-05 15:03:26 -08:00
1 parent 2d6919438b
commit 1acdd9bb20
8 files changed
+44 -16

No files matched your search

+4 -2
View File
@@ -835,8 +835,10 @@ mod tests {
let tracked_hwnd = tracked.0 as isize;
// put back when the guard is dropped, unless something else changed it meanwhile
let handling_override = crate::settings::settings()
.scoped(|settings| &mut settings.platform.window_handling_behaviour, handling);
let handling_override = crate::settings::settings().scoped(
|settings| &mut settings.platform.window_handling_behaviour,
handling,
);
let mut border =
Border::create("komorebi-border-test", tracked_hwnd, 0).expect("a border to paint");
+1 -2
View File
@@ -421,8 +421,7 @@ pub fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> color_eyre::Result
// however this pass ends, `continue` included, the thread is waiting for the next
let _waiting = Waiting;
// Check the wm state every time we receive a notification
let state = wm.lock();
let _held = crate::lock_holder::held("border manager");
let state = crate::lock_holder::lock(&wm, "border manager");
let is_paused = state.is_paused;
let focused_monitor_idx = state.focused_monitor_idx();
let focused_workspace_idx =
+32
View File
@@ -91,6 +91,38 @@ pub fn describe() -> Option<String> {
})
}
/// A lock guard together with the record of who holds it, released together: the record first, then
/// the lock, so the diagnostics never say that a thread holds the lock after it has let go. Drop it
/// (`drop(guard)`) at the point where the lock is released.
pub struct HeldGuard<'a, T> {
// field order is drop order: the record goes before the guard
_held: Held,
guard: parking_lot::MutexGuard<'a, T>,
}
/// Locks `mutex` and records `what` as the holder until the returned guard is dropped
pub fn lock<T>(mutex: &Mutex<T>, what: impl Into<Cow<'static, str>>) -> HeldGuard<'_, T> {
let guard = mutex.lock();
HeldGuard {
_held: held(what),
guard,
}
}
impl<T> std::ops::Deref for HeldGuard<'_, T> {
type Target = T;
fn deref(&self) -> &T {
&self.guard
}
}
impl<T> std::ops::DerefMut for HeldGuard<'_, T> {
fn deref_mut(&mut self) -> &mut T {
&mut self.guard
}
}
#[cfg(test)]
mod tests {
use super::*;
+3 -4
View File
@@ -276,8 +276,7 @@ where
// Keep reference to Arc for potential re-locking
let wm_arc = Arc::clone(&wm);
let mut wm = wm.lock();
let _held = crate::lock_holder::held("monitor reconciliator");
let mut wm = crate::lock_holder::lock(&wm, "monitor reconciliator");
let initial_state = State::from(wm.as_ref());
@@ -452,7 +451,7 @@ where
);
// Re-acquire locks for state restoration
wm = wm_arc.lock();
wm = crate::lock_holder::lock(&wm_arc, "monitor reconciliator");
// Update Win32 data for all monitors
for monitor in wm.monitors_mut() {
@@ -535,7 +534,7 @@ where
);
// Re-acquire locks for removal processing
wm = wm_arc.lock();
wm = crate::lock_holder::lock(&wm_arc, "monitor reconciliator");
monitor_cache = MONITOR_CACHE
.get_or_init(|| Mutex::new(HashMap::new()))
.lock();
+1 -2
View File
@@ -122,8 +122,7 @@ pub fn listen_for_events(wm: Arc<Mutex<WindowManager>>) {
tracing::info!("listening");
loop {
if let Ok(event) = receiver.recv() {
let mut guard = wm.lock();
let _held = crate::lock_holder::held("process_event");
let mut guard = crate::lock_holder::lock(&wm, "process_event");
match guard.process_event(event) {
Ok(()) => {}
Err(error) => {
+1 -2
View File
@@ -77,8 +77,7 @@ fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> color_eyre::Result<()>
for notification in receiver {
let orphan_hwnds = notification.0;
let mut wm = wm.lock();
let _held = crate::lock_holder::held("reaper");
let mut wm = crate::lock_holder::lock(&wm, "reaper");
let mut update_borders = false;
+1 -2
View File
@@ -95,8 +95,7 @@ pub fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> color_eyre::Result
let mut stackbars_monitors = STACKBARS_MONITORS.lock();
// Check the wm state every time we receive a notification
let mut state = wm.lock();
let _held = crate::lock_holder::held("stackbar manager");
let mut state = crate::lock_holder::lock(&wm, "stackbar manager");
// If stackbars are disabled
if matches!(STACKBAR_MODE.load(), StackbarMode::Never)
+1 -2
View File
@@ -78,8 +78,7 @@ pub fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> color_eyre::Result
known_hwnds.lock().clear();
// Check the wm state every time we receive a notification
let state = wm.lock();
let _held = crate::lock_holder::held("transparency manager");
let state = crate::lock_holder::lock(&wm, "transparency manager");
let focused_monitor_idx = state.focused_monitor_idx();