Skip to content

A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.

The log notes when the window stops answering for a second or more

gelenmedya2eca7836fe74
diff --git a/crates/rf-app/src/lib.rs b/crates/rf-app/src/lib.rs
index 319d6007..f31ecb85 100644
--- a/crates/rf-app/src/lib.rs
+++ b/crates/rf-app/src/lib.rs
@@ -947,6 +947,8 @@ pub fn main() {
start_mark("app running");
gpui_kit::init(cx);
ui_stall::start(cx);
+ // (always on and cheap: a line in the log when the window stops answering for a second or more, issue 748)
+ rf_win::ui_watch::start(if dock { "dock" } else { "file manager" });
cx.set_global(settings::Settings::load());
start_mark("settings loaded");
// (an update may have written the shortcuts again: the chosen icon goes back on them)
diff --git a/crates/rf-win/src/lib.rs b/crates/rf-win/src/lib.rs
index 9ee84c5d..31f779bc 100644
--- a/crates/rf-win/src/lib.rs
+++ b/crates/rf-win/src/lib.rs
@@ -135,6 +135,7 @@ pub mod sound;
pub mod toast_post;
pub mod toasts;
pub mod tree_watch;
+pub mod ui_watch;
pub mod wide;
pub mod workfit;
diff --git a/crates/rf-win/src/ui_watch.rs b/crates/rf-win/src/ui_watch.rs
new file mode 100644
index 00000000..dd1b080a
--- /dev/null
+++ b/crates/rf-win/src/ui_watch.rs
@@ -0,0 +1,113 @@
+//! Notes in the log when the UI thread stops answering for a while, always on and cheap.
+//!
+//! WHY (issue 748: "the application freezes" on a right click, and the log had nothing to say about it). The detailed stall trace
+//! (`RUSTYFILES_TRACE_STALL`) wakes the UI thread every 10 ms and is off unless asked for, so a freeze a user meets leaves no trace.
+//!
+//! HOW. A message-only window is made on the UI thread; a thread of its own sends it `WM_NULL` every [`EVERY`] with a time limit. A
+//! UI thread that does not take it within [`HELD_FOR`] is held: the thread keeps asking until it answers, and the log says once,
+//! at the end, how long the window was held (and once when it starts, for a freeze that never ends). The UI thread is woken twice
+//! a second at most and does nothing but answer.
+
+use std::time::{Duration, Instant};
+
+use windows::Win32::Foundation::{HWND, LPARAM, WPARAM};
+use windows::Win32::UI::WindowsAndMessaging::{
+ CreateWindowExW, HWND_MESSAGE, SMTO_BLOCK, SendMessageTimeoutW, WINDOW_EX_STYLE, WINDOW_STYLE, WM_NULL,
+};
+use windows::core::w;
+
+/// How often the UI thread is asked.
+const EVERY: Duration = Duration::from_secs(2);
+/// Longer than this without an answer is a hold worth a line.
+const HELD_FOR: Duration = Duration::from_secs(1);
+
+/// What a hold of `held` deserves in the log: nothing for a short one.
+pub fn worth_a_line(held: Duration) -> bool {
+ held >= HELD_FOR
+}
+
+/// Starts the watch. Call ON THE UI THREAD, once (`what` names the process in the log: "file manager", "dock").
+pub fn start(what: &'static str) {
+ // (a STATIC window answers WM_NULL by itself; message-only, it is never seen)
+ let Ok(h) = (unsafe { CreateWindowExW(WINDOW_EX_STYLE(0), w!("STATIC"), w!("rf-ui-watch"), WINDOW_STYLE(0), 0, 0, 0, 0, Some(HWND_MESSAGE), None, None, None) }) else {
+ return;
+ };
+ let raw = h.0 as isize;
+ let _ = std::thread::Builder::new().name("ui-watch".into()).spawn(move || {
+ let h = HWND(raw as *mut _);
+ loop {
+ std::thread::sleep(EVERY);
+ let asked = Instant::now();
+ if answers_within(h, HELD_FOR) {
+ continue;
+ }
+ if !unsafe { windows::Win32::UI::WindowsAndMessaging::IsWindow(Some(h)) }.as_bool() {
+ return;
+ }
+ log::warn!("ui held: the {what} window has not answered for {} ms", asked.elapsed().as_millis());
+ // (asked again until it answers: then how long it was held in all)
+ while !answers_within(h, Duration::from_secs(5)) {
+ if !unsafe { windows::Win32::UI::WindowsAndMessaging::IsWindow(Some(h)) }.as_bool() {
+ return;
+ }
+ }
+ let held = asked.elapsed();
+ if worth_a_line(held) {
+ log::warn!("ui held: the {what} window answered again after {} ms", held.as_millis());
+ }
+ }
+ });
+}
+
+fn answers_within(h: HWND, limit: Duration) -> bool {
+ let ms = limit.as_millis().min(u32::MAX as u128) as u32;
+ // (no SMTO_ABORTIFHUNG: a thread Windows already calls hung is still waited for, to know how long it was held)
+ let r = unsafe { SendMessageTimeoutW(h, WM_NULL, WPARAM(0), LPARAM(0), SMTO_BLOCK, ms, None) };
+ r.0 != 0
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn only_a_hold_of_a_second_or_more_is_written() {
+ assert!(!worth_a_line(Duration::from_millis(200)));
+ assert!(!worth_a_line(Duration::from_millis(999)));
+ assert!(worth_a_line(Duration::from_millis(1000)));
+ assert!(worth_a_line(Duration::from_secs(30)));
+ }
+
+ /// A window of a thread that reads its messages answers at once.
+ #[test]
+ fn a_window_whose_thread_reads_its_messages_answers() {
+ let (tx, rx) = std::sync::mpsc::channel();
+ std::thread::spawn(move || unsafe {
+ use windows::Win32::UI::WindowsAndMessaging::{DispatchMessageW, GetMessageW, MSG, TranslateMessage};
+ let h = CreateWindowExW(WINDOW_EX_STYLE(0), w!("STATIC"), w!(""), WINDOW_STYLE(0), 0, 0, 0, 0, Some(HWND_MESSAGE), None, None, None).unwrap();
+ tx.send(h.0 as isize).unwrap();
+ let mut m = MSG::default();
+ while GetMessageW(&mut m, None, 0, 0).as_bool() {
+ let _ = TranslateMessage(&m);
+ DispatchMessageW(&m);
+ }
+ });
+ let h = HWND(rx.recv().unwrap() as *mut _);
+ assert!(answers_within(h, Duration::from_secs(2)));
+ }
+
+ /// A window of a thread that is busy does not answer within the limit.
+ #[test]
+ fn a_window_whose_thread_is_busy_does_not() {
+ let (tx, rx) = std::sync::mpsc::channel();
+ std::thread::spawn(move || unsafe {
+ let h = CreateWindowExW(WINDOW_EX_STYLE(0), w!("STATIC"), w!(""), WINDOW_STYLE(0), 0, 0, 0, 0, Some(HWND_MESSAGE), None, None, None).unwrap();
+ tx.send(h.0 as isize).unwrap();
+ std::thread::sleep(Duration::from_secs(3));
+ });
+ let h = HWND(rx.recv().unwrap() as *mut _);
+ let t = Instant::now();
+ assert!(!answers_within(h, Duration::from_millis(300)));
+ assert!(t.elapsed() >= Duration::from_millis(250));
+ }
+}