Desktop: no disk in the frame for the shelf's files (a lost network drive froze the window)
Some checks are pending
CI / macos-build (push) Waiting to run
CI / check (push) Waiting to run
CI / windows-build (push) Waiting to run

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 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-10-26 22:51:48 +03:00
parent a2e6215cef
commit 50888b12cd
6 changed files with 144 additions and 17 deletions

View file

@ -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…)

View file

@ -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) {

View file

@ -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<PathBuf, State>,
asked: HashSet<PathBuf>,
}
pub struct FileStates {
shared: Arc<Mutex<Shared>>,
}
/// 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<PathBuf> = 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<State> {
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);
}
}

View file

@ -175,6 +175,9 @@ const SEARCH_BOARD_MIN: f32 = 340.0;
pub struct Library {
slots: HashMap<PathBuf, Slot>,
/// 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<usize>,
/// A newer version to offer (its number), next to Settings.
pub update: Option<String>,
/// File sizes of databases (`file_size`).
sizes: HashMap<PathBuf, u64>,
}
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());

View file

@ -11,6 +11,7 @@ mod engine_download;
mod engine_offer;
mod engines;
mod examples;
mod file_state;
mod game_view;
mod graphics;
mod i18n;

View file

@ -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<std::path::PathBuf, usize>,
/// 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)],