From 7f3888c59d165fde691acf4e170ce6246e7db89a Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 21:07:47 -0800 Subject: [PATCH] Fonts: a rescan clears the version record after it publishes its catalog A rescan of the font folders cleared the record of families whose other versions had been looked for before it walked the folders. A version lookup made during the walk read the old catalog and recorded its family again, so a version the rescan found stayed hidden until the next scan. The record is now cleared after the new catalog is written. - The font gate used by tests: a thread's first SteadyFonts no longer waits for a font change that is itself waiting for guards other threads hold, so a guarded test does not hang when another test queues a change. It still waits for a change under way and for a waiting change that can start. - Tests that check a change waits for a guard now read the gate's own decision, recorded under its lock, instead of the request to change. --- crates/text/src/fontdb/db.rs | 5 ++- crates/text/src/test_fonts.rs | 30 +++++++++++-- crates/text/src/tests/sysfonts.rs | 72 +++++++++++++++++++++++++++---- 3 files changed, 94 insertions(+), 13 deletions(-) diff --git a/crates/text/src/fontdb/db.rs b/crates/text/src/fontdb/db.rs index 046db94dfd..1b8d9cd04d 100644 --- a/crates/text/src/fontdb/db.rs +++ b/crates/text/src/fontdb/db.rs @@ -374,8 +374,6 @@ impl FontDb { #[cfg(not(target_arch = "wasm32"))] fn scan_font_dirs(&self) -> usize { - // Files may have been installed since: other versions are looked for again. - self.version_scanned.write().unwrap_or_else(|e| e.into_inner()).clear(); let mut catalog = Catalog::new(); let mut aliases: HashMap> = HashMap::new(); let mut postscript = PostScriptNames::new(); @@ -455,6 +453,9 @@ impl FontDb { let _change = self.cataloged.get().is_some().then(crate::test_fonts::font_change); *self.stamp.lock().unwrap_or_else(|e| e.into_inner()) = Some(ScanStamp { paths, modified: modified_at }); *self.catalog.write().unwrap_or_else(|e| e.into_inner()) = catalog; + // Files may have been installed since: other versions are looked for again, in this + // catalog. A version lookup made during the scan read the previous one. + self.version_scanned.write().unwrap_or_else(|e| e.into_inner()).clear(); *self.aliases.write().unwrap_or_else(|e| e.into_inner()) = aliases; *self.postscript.write().unwrap_or_else(|e| e.into_inner()) = postscript; // Faces loaded from a file this scan did not catalog (the font was removed, or its folder diff --git a/crates/text/src/test_fonts.rs b/crates/text/src/test_fonts.rs index d8f91f8d50..6dd0ba0449 100644 --- a/crates/text/src/test_fonts.rs +++ b/crates/text/src/test_fonts.rs @@ -105,12 +105,17 @@ impl Drop for SteadyFonts { /// [`SteadyFonts`] for `db`. Catalogs `db`'s installed fonts first: a first scan does not wait for /// any guard, so this thread can wait for one running on another thread while it holds a guard. A -/// thread's first guard waits for the font changes under way or waiting. +/// thread's first guard waits for a font change under way, and for a waiting change that can start +/// now. It does not wait for a change that is waiting for guards other threads hold: the thread +/// waiting for this guard may hold one of them. pub fn steady_fonts(db: &crate::FontDb) -> SteadyFonts { db.ensure_catalog(); if STEADY.with(std::cell::Cell::get) == 0 { let mut s = steadiness(); - while !CHANGING.with(std::cell::Cell::get) && (s.changing || s.waiting > 0) { + let waits = |s: &Steadiness| !CHANGING.with(std::cell::Cell::get) && (s.changing || (s.waiting > 0 && s.holders == s.holders_waiting)); + #[cfg(test)] + gate_decisions().push((std::thread::current().id(), GateAsk::Guard, waits(&s))); + while waits(&s) { s = await_steadiness(s); } s.holders += 1; @@ -150,7 +155,10 @@ pub(crate) fn font_change() -> FontChange { font_changers().push(std::thread::current().id()); s.waiting += 1; s.holders_waiting += usize::from(holder); - while s.changing || if holder { s.holders > s.holders_waiting } else { s.holders > 0 } { + let waits = |s: &Steadiness| s.changing || if holder { s.holders > s.holders_waiting } else { s.holders > 0 }; + #[cfg(test)] + gate_decisions().push((std::thread::current().id(), GateAsk::Change, waits(&s))); + while waits(&s) { s = await_steadiness(s); } s.waiting -= 1; @@ -175,6 +183,22 @@ pub(crate) fn font_changers() -> std::sync::MutexGuard<'static, Vec std::sync::MutexGuard<'static, Vec<(std::thread::ThreadId, GateAsk, bool)>> { + static DECISIONS: std::sync::Mutex> = std::sync::Mutex::new(Vec::new()); + DECISIONS.lock().unwrap_or_else(std::sync::PoisonError::into_inner) +} + /// The family of [`variable_font`]. pub const VARIABLE_FAMILY: &str = "Varitest Sans"; diff --git a/crates/text/src/tests/sysfonts.rs b/crates/text/src/tests/sysfonts.rs index 08f6584186..b6a1e8c1aa 100644 --- a/crates/text/src/tests/sysfonts.rs +++ b/crates/text/src/tests/sysfonts.rs @@ -4,6 +4,7 @@ use std::path::{Path, PathBuf}; use std::sync::Arc; use crate::fontdb::fonts_outside; +use crate::test_fonts::GateAsk; use crate::*; const FAMILY: &str = "Sysfont Sans3"; @@ -83,10 +84,8 @@ fn steady_fonts_hold_back_other_threads_font_changes() { let _steady = guarded.then(|| crate::test_fonts::steady_fonts(&db)); db.add_font(font) }); - let id = adding.thread().id(); - while !adding.is_finished() && !crate::test_fonts::font_changers().contains(&id) { - std::thread::yield_now(); - } + let waits = first_decision(adding.thread().id(), GateAsk::Change, || adding.is_finished()); + assert_eq!(waits, Some(true), "guarded {guarded}: the other thread's change waits for the guard"); assert_eq!(db.generation(), before, "guarded {guarded}: the other thread's change waits for the guard"); let listed = db.families().iter().any(|f| f == crate::test_fonts::VARIABLE_FAMILY); assert!(!listed, "guarded {guarded}: the other thread publishes the font only once the guard is dropped"); @@ -99,10 +98,8 @@ fn steady_fonts_hold_back_other_threads_font_changes() { let steady = crate::test_fonts::steady_fonts(&db); std::thread::scope(|scope| { let adding = scope.spawn(|| db.add_font(crate::test_fonts::two_axis_font().unwrap())); - let id = adding.thread().id(); - while !adding.is_finished() && !crate::test_fonts::font_changers().contains(&id) { - std::thread::yield_now(); - } + let waits = first_decision(adding.thread().id(), GateAsk::Change, || adding.is_finished()); + assert_eq!(waits, Some(true), "the other thread's change waits for the guard"); let before = db.generation(); assert!(db.add_font(crate::test_fonts::variable_font().unwrap()) > 0); let after = db.generation(); @@ -114,6 +111,65 @@ fn steady_fonts_hold_back_other_threads_font_changes() { }); } +/// Whether the first `ask` that the thread `id` made of the font gate waited, once the thread has +/// made it; `None` when `finished` says the thread ended without making it. +fn first_decision(id: std::thread::ThreadId, ask: GateAsk, finished: impl Fn() -> bool) -> Option { + loop { + let done = finished(); + let waits = crate::test_fonts::gate_decisions().iter().find(|(t, a, _)| *t == id && *a == ask).map(|(_, _, waits)| *waits); + if waits.is_some() || done { + return waits; + } + std::thread::yield_now(); + } +} + +/// While a thread holds [`test_fonts::steady_fonts`] and a font change on another thread waits for +/// it, a third thread's first guard does not wait for that change, which cannot start before the +/// first guard is dropped. +#[test] +fn a_first_guard_does_not_wait_for_a_change_that_waits_for_other_guards() { + let db = FontDb::with_font_dirs(vec![]); + std::thread::scope(|scope| { + let steady = crate::test_fonts::steady_fonts(&db); + let adding = scope.spawn(|| db.add_font(crate::test_fonts::variable_font().unwrap())); + assert_eq!(first_decision(adding.thread().id(), GateAsk::Change, || adding.is_finished()), Some(true), "the change waits for the guard"); + let guarding = scope.spawn(|| drop(crate::test_fonts::steady_fonts(&db))); + let waits = first_decision(guarding.thread().id(), GateAsk::Guard, || guarding.is_finished()); + assert_eq!(waits, Some(false), "the new guard does not wait for the change that waits for this thread's guard"); + guarding.join().unwrap(); + drop(steady); + assert!(adding.join().unwrap() > 0, "the font is new to this database"); + }); +} + +/// A version of a family looked up while a rescan of the font folders is under way, before the +/// rescan publishes what it found, does not hide a version of that family the rescan found. +#[test] +fn a_version_looked_up_during_a_rescan_is_found_after_it() { + use crate::test_fonts::{TWIN_FAMILY, TWIN_VERSIONS, twin_font}; + let dir = std::env::temp_dir().join(format!("vc-sysfonts-{}-rescan-versions", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + std::fs::write(dir.join("Older.ttf"), twin_font(false).unwrap()).unwrap(); + let db = FontDb::with_font_dirs(vec![dir.clone()]); + assert_eq!(db.load_system_fonts(), 1); + let face = db.face(TWIN_FAMILY, "Regular").expect("the installed face"); + assert_eq!(face.version, TWIN_VERSIONS[0]); + std::fs::write(dir.join("Newer.ttf"), twin_font(true).unwrap()).unwrap(); + let versions = |db: &FontDb| db.versions(&face).iter().map(|f| f.version.clone()).collect::>(); + std::thread::scope(|scope| { + let steady = crate::test_fonts::steady_fonts(&db); + let rescanning = scope.spawn(|| db.load_system_fonts()); + let waits = first_decision(rescanning.thread().id(), GateAsk::Change, || rescanning.is_finished()); + assert_eq!(waits, Some(true), "the rescan waits for the guard before it publishes"); + assert_eq!(versions(&db), [TWIN_VERSIONS[0]], "before the rescan publishes, only the older file is cataloged"); + drop(steady); + assert_eq!(rescanning.join().unwrap(), 2); + }); + assert_eq!(versions(&db), TWIN_VERSIONS, "the version the rescan found is loaded"); + std::fs::remove_dir_all(dir).unwrap(); +} + /// The first scan of a database's installed fonts does not wait for [`test_fonts::steady_fonts`] /// held for another database, so a thread holding one can catalog a database that another thread /// is scanning.