From b7b3c1c8f45eda283100c9f4821f0a27ede22aaf Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Sat, 31 Oct 2026 18:51:14 -0700 Subject: [PATCH] feat(windows): heartbeat thread and an accept log line, evidence for a daemon-wide stall The stall analysis could not tell a frozen daemon (or guest) from a lock hold: in both, the threads are silent. A heartbeat thread (komorebi/src/heartbeat.rs) sleeps 100 ms in a loop and logs a warning, "heartbeat was N ms late: the daemon was not scheduled for that long", when a beat comes 500 ms or more after it should; started next to the command listener. If it is late exactly when the other threads are silent, the process was not scheduled; if it keeps beating while they are silent, something holds a lock (see the lock holder warnings). The Windows connection policy's on_accept also logs, at debug, "accepted a command connection (N ms since the previous one)", so a request that waited in the backlog during a stall shows up at its late accept. Both cost next to nothing (a 100 ms timer and one debug line per connection). komorebi-transport is unchanged: KDE and macOS do not see either. Diagnostics only, no behaviour change. Verified: tools/fast-check Windows clippy -D warnings PASS; e2e: none needed. --- komorebi/src/daemon.rs | 1 + komorebi/src/heartbeat.rs | 39 +++++++++++++++++++++++++++++++++ komorebi/src/lib.rs | 1 + komorebi/src/process_command.rs | 16 ++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 komorebi/src/heartbeat.rs diff --git a/komorebi/src/daemon.rs b/komorebi/src/daemon.rs index 3492bcd0..5b723e88 100644 --- a/komorebi/src/daemon.rs +++ b/komorebi/src/daemon.rs @@ -333,6 +333,7 @@ pub fn run() -> eyre::Result<()> { focus_manager::listen_for_notifications(wm.clone()); theme_manager::listen_for_notifications(); + crate::heartbeat::start(); listen_for_commands(wm.clone()); if let Some(port) = opts.tcp_port { diff --git a/komorebi/src/heartbeat.rs b/komorebi/src/heartbeat.rs new file mode 100644 index 00000000..d8039354 --- /dev/null +++ b/komorebi/src/heartbeat.rs @@ -0,0 +1,39 @@ +//! Evidence of a stall of the whole daemon. +//! +//! A thread that sleeps 100 ms in a loop and says so when it wakes up much later than that. If +//! it is late in the same moments in which every other thread of the daemon is silent, the +//! process (or the guest it runs in) was not scheduled; if only some threads are silent, one of +//! them holds something (see [`crate::lock_holder`]). + +use std::time::Duration; +use std::time::Instant; + +/// How long the heartbeat thread sleeps between two beats +const BEAT: Duration = Duration::from_millis(100); + +/// A beat later than this after the one before is logged +const LATE: Duration = Duration::from_millis(500); + +/// Starts the heartbeat thread +pub fn start() { + let _ = std::thread::Builder::new() + .name("heartbeat".to_string()) + .spawn(|| { + let mut last = Instant::now(); + + loop { + std::thread::sleep(BEAT); + + let now = Instant::now(); + let late = now.duration_since(last).saturating_sub(BEAT); + last = now; + + if late >= LATE { + tracing::warn!( + "heartbeat was {} ms late: the daemon was not scheduled for that long", + late.as_millis() + ); + } + } + }); +} diff --git a/komorebi/src/lib.rs b/komorebi/src/lib.rs index a93adca1..ef98074a 100644 --- a/komorebi/src/lib.rs +++ b/komorebi/src/lib.rs @@ -15,6 +15,7 @@ pub mod focus_manager; pub mod live_resize; pub use komorebi_collections::Lockable; pub use komorebi_collections::lockable_sequence; +pub mod heartbeat; pub mod lock_holder; pub mod monitor; pub mod monitor_reconciliator; diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index 2ee253f3..1ab0a946 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -114,6 +114,22 @@ struct WindowsConnectionPolicy; impl ConnectionPolicy for WindowsConnectionPolicy { fn on_accept(&self, stream: &UnixStream) { + // when connections come in: a request that waits in the backlog while the process is + // stalled is seen here late, next to the heartbeat + { + static LAST_ACCEPT: parking_lot::Mutex> = + parking_lot::Mutex::new(None); + let now = std::time::Instant::now(); + let since = LAST_ACCEPT + .lock() + .replace(now) + .map(|last| now.duration_since(last)); + tracing::debug!( + "accepted a command connection ({} ms since the previous one)", + since.map_or(-1, |d| d.as_millis() as i64) + ); + } + match stream.set_read_timeout(Some(Duration::from_secs(1))) { Ok(()) => {} Err(error) => tracing::error!("{}", error),