From 50888b12cd87e140c95e5a5371417dd19917bc5c Mon Sep 17 00:00:00 2001 From: vrubelroman Date: Mon, 26 Oct 2026 22:51:48 +0300 Subject: [PATCH] Desktop: no disk in the frame for the shelf's files (a lost network drive froze the window) The owner's window froze for about a minute when the network dropped during a game; its main thread waited in rpc_wait_bit_killable: an NFS mount (hard) holding a database of the shelf. While a game is shown the reference tab's list checked every shelf file's existence each frame, and the library asked sizes, permissions and existence the same way. file_state.rs: a thread of its own looks at the files the screen asks about every 3 s; the screen reads what it last found (a file not looked at yet counts as there). The first database at start is read in the background without a check. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 5 ++ apps/desktop/src/app.rs | 16 +++-- apps/desktop/src/file_state.rs | 111 +++++++++++++++++++++++++++++++++ apps/desktop/src/library.rs | 22 +++---- apps/desktop/src/main.rs | 1 + apps/desktop/src/play.rs | 6 +- 6 files changed, 144 insertions(+), 17 deletions(-) create mode 100644 apps/desktop/src/file_state.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index becf4f9..6ed25fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes are listed here. Versions follow [Semantic Versioning](https ## [Unreleased] +- The window no longer freezes when a database on a network drive (NFS) becomes + unreachable, for example when the network drops during a game: whether the shelf's + files are there, their sizes and permissions are looked at by a thread of their own, + not in the frame. + ## [1.7.0] — 2026-10-26 - Rodent's list of styles gets Rodent IV's: 13 characters (Amanda, Defender, Strangler…) diff --git a/apps/desktop/src/app.rs b/apps/desktop/src/app.rs index 3d74df8..5230858 100644 --- a/apps/desktop/src/app.rs +++ b/apps/desktop/src/app.rs @@ -348,7 +348,9 @@ impl App { } let (tx, rx) = channel(); let mut library = Library::new(); - if let Some(first) = settings.databases.first().filter(|p| p.exists()) { + // Read in the background: a missing file says so there (a lost network drive + // does not hold the start). + if let Some(first) = settings.databases.first() { library.select_db(first, ctx); } let mut app = Self { @@ -788,7 +790,7 @@ impl App { let preselect = target.or(self.settings.last_target.clone()).or(source); // Databases not read yet are read now, for their counts and encodings. for p in self.settings.databases.clone() { - if self.library.database(&p).is_none() && p.exists() { + if self.library.database(&p).is_none() && self.library.files.exists(&p) { self.library.load(&p, ctx); } } @@ -2372,7 +2374,7 @@ impl eframe::App for App { .settings .databases .iter() - .filter(|p| p.exists()) + .filter(|p| self.library.files.exists(p)) .map(|p| crate::reference::RefDb { path: p.clone(), name: p @@ -2738,10 +2740,16 @@ impl eframe::App for App { self.confirm_dialog(&ctx); self.own_dialogs(&ctx); + // Whether the database the dialog names is there, as last found. + if let Some(d) = self.play_dialog.as_mut() { + d.target_there = d + .record_target() + .is_some_and(|p| self.library.files.get(p).is_some_and(|s| s.exists)); + } // The number of games of the database the dialog names (read once, if needed). if let Some(d) = self.play_dialog.as_mut() && let Some(path) = d.record_target().map(std::path::Path::to_path_buf) - && path.exists() + && self.library.files.exists(&path) && !d.counts.contains_key(&path) { match self.library.database(&path) { diff --git a/apps/desktop/src/file_state.rs b/apps/desktop/src/file_state.rs new file mode 100644 index 0000000..38dc669 --- /dev/null +++ b/apps/desktop/src/file_state.rs @@ -0,0 +1,111 @@ +//! What the screen needs to know about the databases' files (there or not, their size, +//! read only) without touching the disk in the frame: a network drive that has lost its +//! network (an NFS mount waits for it) would hold the whole window. A thread of its own +//! looks at the files the screen asks about, again every few seconds; the screen reads +//! what it last found. A file not looked at yet counts as there. + +use std::collections::{HashMap, HashSet}; +use std::path::{Path, PathBuf}; +use std::sync::{Arc, Mutex}; +use std::time::Duration; + +/// A file as last found. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct State { + pub exists: bool, + pub size: u64, + pub readonly: bool, +} + +#[derive(Default)] +struct Shared { + found: HashMap, + asked: HashSet, +} + +pub struct FileStates { + shared: Arc>, +} + +/// How often the files are looked at again. +const EVERY: Duration = Duration::from_secs(3); + +impl FileStates { + pub fn new() -> Self { + let shared = Arc::new(Mutex::new(Shared::default())); + let looked = shared.clone(); + let _ = std::thread::Builder::new() + .name("file-states".into()) + .spawn(move || { + loop { + let asked: Vec = match looked.lock() { + Ok(s) => s.asked.iter().cloned().collect(), + Err(_) => return, + }; + for path in asked { + // May wait long (a lost network drive): only this thread waits. + let state = look(&path); + if let Ok(mut s) = looked.lock() { + s.found.insert(path, state); + } + } + std::thread::sleep(EVERY); + } + }); + Self { shared } + } + + /// The file as last found (`None`: not looked at yet); it is looked at from now on. + pub fn get(&self, path: &Path) -> Option { + let mut s = self.shared.lock().ok()?; + if !s.asked.contains(path) { + s.asked.insert(path.to_owned()); + } + s.found.get(path).copied() + } + + /// Whether the file is there (as last found; yes until looked at). + pub fn exists(&self, path: &Path) -> bool { + self.get(path).is_none_or(|s| s.exists) + } +} + +fn look(path: &Path) -> State { + match std::fs::metadata(path) { + Ok(m) => State { + exists: true, + size: m.len(), + readonly: m.permissions().readonly(), + }, + Err(_) => State { + exists: false, + size: 0, + readonly: false, + }, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn files_are_looked_at_away_from_the_caller() { + let dir = std::env::temp_dir().join(format!("cp-file-states-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let there = dir.join("a.pgn"); + std::fs::write(&there, "1. e4 *\n").unwrap(); + let gone = dir.join("b.pgn"); + let states = FileStates::new(); + // Not looked at yet: counted as there, nothing waited for. + assert!(states.get(&there).is_none()); + assert!(states.exists(&gone)); + let deadline = std::time::Instant::now() + Duration::from_secs(5); + while states.get(&gone).is_none() && std::time::Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(20)); + } + assert!(!states.exists(&gone)); + assert_eq!(states.get(&there).map(|s| s.size), Some(8)); + let _ = std::fs::remove_dir_all(&dir); + } +} diff --git a/apps/desktop/src/library.rs b/apps/desktop/src/library.rs index 7492cc9..cab6cc1 100644 --- a/apps/desktop/src/library.rs +++ b/apps/desktop/src/library.rs @@ -175,6 +175,9 @@ const SEARCH_BOARD_MIN: f32 = 340.0; pub struct Library { slots: HashMap, + /// The files as last found, away from the frame (a lost network drive must not hold + /// the window). + pub files: crate::file_state::FileStates, /// The players' databases (player_games.rs) and whose: a knight on their cards. player_dbs: Vec<(PathBuf, String)>, /// Games the user has opened (by content). @@ -195,14 +198,13 @@ pub struct Library { pub online_running: Option, /// A newer version to offer (its number), next to Settings. pub update: Option, - /// File sizes of databases (`file_size`). - sizes: HashMap, } impl Library { pub fn new() -> Self { Self { slots: HashMap::new(), + files: crate::file_state::FileStates::new(), player_dbs: Vec::new(), viewed: Viewed::load(), large: HashMap::new(), @@ -213,7 +215,6 @@ impl Library { online: false, online_busy: Vec::new(), online_running: None, - sizes: HashMap::new(), update: None, } } @@ -314,7 +315,7 @@ impl Library { if crate::examples::is_example(path) { return Some(Block::Example); } - if !path.exists() { + if !self.files.exists(path) { return Some(Block::Missing); } match self.info.get(path).map(|i| i.format) { @@ -340,7 +341,7 @@ impl Library { None => return Some(Block::ReadOnly), } } - if std::fs::metadata(path).is_ok_and(|m| m.permissions().readonly()) { + if self.files.get(path).is_some_and(|s| s.readonly) { return Some(Block::NoPermission); } None @@ -364,12 +365,9 @@ impl Library { } } - /// Size of a database's file (cached: asked every frame by the reference tab). - pub fn file_size(&mut self, path: &Path) -> u64 { - *self - .sizes - .entry(path.to_owned()) - .or_insert_with(|| std::fs::metadata(path).map_or(0, |m| m.len())) + /// Size of a database's file (as last found; asked every frame by the reference tab). + pub fn file_size(&self, path: &Path) -> u64 { + self.files.get(path).map_or(0, |s| s.size) } /// The database is being read now. @@ -992,7 +990,7 @@ impl Library { trf("loading… {n}", &[("n", &p.games)]) } Some(Slot::Failed(_)) => tr("error").into(), - None if !path.exists() => tr("file not found").into(), + None if !self.files.exists(path) => tr("file not found").into(), None => tr("not loaded").into(), }; let (rect, resp) = ui.allocate_exact_size(vec2(190.0, 64.0), Sense::click()); diff --git a/apps/desktop/src/main.rs b/apps/desktop/src/main.rs index 2dc51a9..65920d7 100644 --- a/apps/desktop/src/main.rs +++ b/apps/desktop/src/main.rs @@ -11,6 +11,7 @@ mod engine_download; mod engine_offer; mod engines; mod examples; +mod file_state; mod game_view; mod graphics; mod i18n; diff --git a/apps/desktop/src/play.rs b/apps/desktop/src/play.rs index 0f4afd7..a13f16f 100644 --- a/apps/desktop/src/play.rs +++ b/apps/desktop/src/play.rs @@ -1353,6 +1353,9 @@ pub struct Dialog { focus: bool, /// The number of games of the players' databases read so far (the app fills it). pub counts: std::collections::HashMap, + /// Whether the database the games go to is there (the app tells, from the files as + /// last found: no disk in the frame). + pub target_there: bool, /// Where the games of the name typed go: for that name (found once per name). target: Option<(String, std::path::PathBuf)>, } @@ -1393,6 +1396,7 @@ impl Dialog { from: None, keep_color: false, counts: Default::default(), + target_there: false, target: None, } } @@ -1452,7 +1456,7 @@ impl Dialog { &[("db", &db)], ); } - if !path.exists() { + if !self.target_there { return trf( "Games will be written to a new database «{db}».", &[("db", &db)],