mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Merge pull request #11652 from wanikhawar/claude/stoic-yalow-d695ab
A scratch folder that stops answering no longer freezes the app
This commit is contained in:
5 files changed
+711
-87
No files matched your search
@@ -21,7 +21,7 @@ use std::fs::File;
|
||||
use std::path::{Path, PathBuf};
|
||||
#[cfg(windows)]
|
||||
use std::sync::OnceLock;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
|
||||
use std::sync::{Mutex, PoisonError};
|
||||
|
||||
const ALIGN: u64 = 4096;
|
||||
@@ -48,6 +48,7 @@ pub(crate) struct Allocator {
|
||||
|
||||
impl Allocator {
|
||||
/// Bytes currently allocated (end of file minus free space).
|
||||
#[cfg(test)]
|
||||
pub fn used(&self) -> u64 {
|
||||
self.end - self.by_offset.values().sum::<u64>()
|
||||
}
|
||||
@@ -108,6 +109,9 @@ pub(crate) struct ScratchFile {
|
||||
#[cfg(not(windows))]
|
||||
linked: bool,
|
||||
allocator: Mutex<Allocator>,
|
||||
/// What [`ScratchFile::used`] reads. No lock: `free` truncates the file under the allocator lock,
|
||||
/// and a dead folder would block that, so a status read must not wait for it.
|
||||
used: AtomicU64,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for ScratchFile {
|
||||
@@ -129,6 +133,7 @@ impl ScratchFile {
|
||||
#[cfg(not(windows))]
|
||||
linked,
|
||||
allocator: Mutex::new(Allocator::default()),
|
||||
used: AtomicU64::new(0),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -145,6 +150,7 @@ impl ScratchFile {
|
||||
pub fn write(&self, data: &[u8]) -> std::io::Result<Extent> {
|
||||
let len = u32::try_from(data.len()).map_err(|_| std::io::Error::other("block larger than 4 GiB"))?;
|
||||
let mut extent = self.lock().alloc(len);
|
||||
self.used.fetch_add(extent.alloc, Ordering::Relaxed);
|
||||
if let Err(e) = write_all_at(&self.main, data, extent.offset) {
|
||||
self.free(extent);
|
||||
return Err(e);
|
||||
@@ -164,6 +170,7 @@ impl ScratchFile {
|
||||
}
|
||||
|
||||
pub fn free(&self, extent: Extent) {
|
||||
self.used.fetch_sub(extent.alloc, Ordering::Relaxed);
|
||||
let mut allocator = self.lock();
|
||||
allocator.free(extent);
|
||||
if allocator.end == 0 {
|
||||
@@ -175,7 +182,7 @@ impl ScratchFile {
|
||||
}
|
||||
|
||||
pub fn used(&self) -> u64 {
|
||||
self.lock().used()
|
||||
self.used.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
fn lock(&self) -> std::sync::MutexGuard<'_, Allocator> {
|
||||
@@ -483,6 +490,23 @@ mod tests {
|
||||
assert_eq!(a.alloc(10).offset, 4096, "remainder of the split hole is used next");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn used_follows_writes_and_frees() {
|
||||
let dir = std::env::temp_dir().join(format!("pc-scratch-used-{}", std::process::id()));
|
||||
let scratch = ScratchFile::create(&dir).unwrap();
|
||||
assert_eq!(scratch.used(), 0);
|
||||
let a = scratch.write(b"one").unwrap();
|
||||
assert_eq!(scratch.used(), a.alloc);
|
||||
let b = scratch.write(&[1u8; 9000]).unwrap();
|
||||
assert_eq!(scratch.used(), a.alloc + b.alloc);
|
||||
scratch.free(a);
|
||||
assert_eq!(scratch.used(), b.alloc);
|
||||
scratch.free(b);
|
||||
assert_eq!(scratch.used(), 0);
|
||||
drop(scratch);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_roundtrip_and_crc() {
|
||||
let dir = std::env::temp_dir().join("photocraft-scratch-test");
|
||||
|
||||
+438
-80
@@ -22,10 +22,16 @@
|
||||
//! poisons the tile: edits and saves check the health of the document they publish, plus
|
||||
//! [`check_since`] for failures that came and went during the operation. The
|
||||
//! infallible display API uses a temporary placeholder, never replacement pixel storage.
|
||||
//!
|
||||
//! No lock is held across a call into the scratch folder. Opening and measuring it run on a
|
||||
//! short-lived query thread, at most one per folder and question; callers wait a bounded time
|
||||
//! for the answer, and a late answer still applies if that folder is still the configured one.
|
||||
//! Status reads the UI makes every frame stay fast whatever the folder does.
|
||||
|
||||
use std::collections::VecDeque;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering};
|
||||
use std::sync::{Arc, Mutex, PoisonError, RwLock, RwLockReadGuard, RwLockWriteGuard, Weak};
|
||||
use std::sync::{Arc, Condvar, Mutex, PoisonError, RwLock, RwLockReadGuard, RwLockWriteGuard, Weak};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use crate::scratch_file::{Extent, ScratchFile};
|
||||
@@ -261,6 +267,8 @@ struct Config {
|
||||
backoff: Duration,
|
||||
/// Explicit scratch size limit ([`set_size_cap`]); the free-space reserve always applies.
|
||||
size_cap: Option<u64>,
|
||||
/// Scratch bytes the volume allows, as of the last answer; `None` = unmeasured.
|
||||
room: Option<u64>,
|
||||
}
|
||||
|
||||
struct Pause {
|
||||
@@ -274,6 +282,51 @@ const RETRY_MAX: Duration = Duration::from_secs(60);
|
||||
const SPACE_CHECK_EVERY: u64 = 64 << 20;
|
||||
/// Leave at least max(1 GiB, 5 % of the volume) free for the system and other programs.
|
||||
const RESERVE_MIN: u64 = 1 << 30;
|
||||
/// How long a caller waits for the scratch folder to answer. A healthy disk answers in far less;
|
||||
/// the query goes on after this and its answer still counts when it comes.
|
||||
const QUERY_WAIT: Duration = Duration::from_secs(2);
|
||||
/// Most folder queries in flight at once. A system call on a dead hard mount can't be cancelled,
|
||||
/// so each stuck one keeps its thread, and every other folder tried on that mount adds one.
|
||||
const MAX_QUERIES: usize = 16;
|
||||
|
||||
/// A question in flight about a scratch folder (open it and/or measure it).
|
||||
struct Query {
|
||||
dir: PathBuf,
|
||||
prepare: bool,
|
||||
deadline: Instant,
|
||||
done: Arc<QueryDone>,
|
||||
}
|
||||
|
||||
/// Set once a query's answer has been applied (or discarded).
|
||||
#[derive(Default)]
|
||||
struct QueryDone {
|
||||
finished: Mutex<bool>,
|
||||
wake: Condvar,
|
||||
}
|
||||
|
||||
impl QueryDone {
|
||||
fn finish(&self) {
|
||||
*self.finished.lock().unwrap_or_else(PoisonError::into_inner) = true;
|
||||
self.wake.notify_all();
|
||||
}
|
||||
|
||||
fn finished(&self) -> bool {
|
||||
*self.finished.lock().unwrap_or_else(PoisonError::into_inner)
|
||||
}
|
||||
|
||||
/// Wait until the query finishes or `deadline` passes; a deadline already past returns at once.
|
||||
fn wait_until(&self, deadline: Instant) {
|
||||
let Some(left) = deadline.checked_duration_since(Instant::now()) else { return };
|
||||
let done = self.finished.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
drop(self.wake.wait_timeout_while(done, left, |done| !*done).unwrap_or_else(PoisonError::into_inner));
|
||||
}
|
||||
}
|
||||
|
||||
static QUERIES: Mutex<Vec<Query>> = Mutex::new(Vec::new());
|
||||
/// Query threads alive, counted until they finish, including closing any file they opened;
|
||||
/// `QUERIES` only dedupes questions.
|
||||
static QUERY_THREADS: AtomicUsize = AtomicUsize::new(0);
|
||||
static QUERY_HOOK: RwLock<Option<fn(&Path)>> = RwLock::new(None);
|
||||
|
||||
static M: Manager = Manager {
|
||||
failed: AtomicUsize::new(0),
|
||||
@@ -288,7 +341,7 @@ static M: Manager = Manager {
|
||||
recovering: AtomicBool::new(false),
|
||||
space_unmeasured: AtomicBool::new(false),
|
||||
registry: Mutex::new(Registry { cells: Vec::new(), hand: 0 }),
|
||||
config: Mutex::new(Config { dir: None, file: None, last_error: None, budget: 0, paused: None, backoff: RETRY_FIRST, size_cap: None }),
|
||||
config: Mutex::new(Config { dir: None, file: None, last_error: None, budget: 0, paused: None, backoff: RETRY_FIRST, size_cap: None, room: None }),
|
||||
evictions: AtomicU64::new(0),
|
||||
reloads: AtomicU64::new(0),
|
||||
written: AtomicU64::new(0),
|
||||
@@ -348,27 +401,73 @@ impl Manager {
|
||||
self.set_active(c.budget);
|
||||
}
|
||||
|
||||
/// Open the scratch file and check its volume's room; pause (and say why) if either fails.
|
||||
/// The caller holds the config lock. Returns whether spilling can go ahead.
|
||||
fn prepare(&self, c: &mut Config) -> bool {
|
||||
let Some(dir) = c.dir.clone() else { return false };
|
||||
if c.file.is_none() {
|
||||
match std::fs::create_dir_all(&dir).and_then(|()| ScratchFile::create(&dir)) {
|
||||
Ok(f) => c.file = Some(Arc::new(f)),
|
||||
Err(e) => {
|
||||
self.pause(c, &format!("could not create a scratch file in {}: {e}", dir.display()));
|
||||
return false;
|
||||
}
|
||||
/// Apply a finished folder query. `file` is the scratch file a `prepare` query opened (or the
|
||||
/// error), `sampled_used` the scratch bytes in use just before the volume was asked, `space`
|
||||
/// what the volume reported. Discarded if the folder changed meanwhile.
|
||||
fn settle(&self, dir: &Path, prepare: bool, file: Option<std::io::Result<ScratchFile>>, sampled_used: u64, space: Option<(u64, u64)>, done: &QueryDone) {
|
||||
// Declared before the guard so a rejected file closes after the lock is released.
|
||||
let mut _spare: Option<ScratchFile> = None;
|
||||
let mut c = self.config();
|
||||
// Under the config lock, so a caller holding the lock can tell answered from not.
|
||||
done.finish();
|
||||
if c.dir.as_deref() != Some(dir) {
|
||||
return;
|
||||
}
|
||||
match file {
|
||||
Some(Ok(f)) if c.file.is_none() => c.file = Some(Arc::new(f)),
|
||||
Some(Ok(f)) => _spare = Some(f),
|
||||
Some(Err(e)) if c.file.is_none() => {
|
||||
self.pause(&mut c, &format!("could not create a scratch file in {}: {e}", dir.display()));
|
||||
return;
|
||||
}
|
||||
Some(Err(_)) | None => {}
|
||||
}
|
||||
let used = c.file.as_ref().map_or(0, |f| f.used());
|
||||
refresh_space(c, &dir, used);
|
||||
if used >= self.space_limit.load(Ordering::Relaxed) {
|
||||
let msg = no_room(&dir, c.size_cap);
|
||||
self.pause(c, &msg);
|
||||
return false;
|
||||
apply_space(&mut c, sampled_used, space);
|
||||
if !prepare {
|
||||
return;
|
||||
}
|
||||
true
|
||||
if c.file.is_none() {
|
||||
// The folder changed away and back while this ran.
|
||||
self.pause(&mut c, ¬_responding(dir));
|
||||
return;
|
||||
}
|
||||
if used >= self.space_limit.load(Ordering::Relaxed) {
|
||||
let msg = no_room(dir, c.size_cap);
|
||||
self.pause(&mut c, &msg);
|
||||
return;
|
||||
}
|
||||
if c.paused.is_some() {
|
||||
self.resume(&mut c);
|
||||
}
|
||||
}
|
||||
|
||||
/// Open the scratch file and check its volume's room, waiting up to [`QUERY_WAIT`]; an answer
|
||||
/// that comes later is applied by [`Manager::settle`]. Pauses (and says why) on failure or
|
||||
/// silence. The caller holds no lock.
|
||||
fn prepare(&self, dir: &Path) {
|
||||
let done = ask(dir, true);
|
||||
let mut c = self.config();
|
||||
if c.dir.as_deref() != Some(dir) || done.as_ref().is_some_and(|d| d.finished()) {
|
||||
return;
|
||||
}
|
||||
// No answer in time: it still counts when it comes. Until then:
|
||||
let used = c.file.as_ref().map_or(0, |f| f.used());
|
||||
apply_space(&mut c, used, None);
|
||||
if c.file.is_none() || c.paused.is_some() {
|
||||
self.pause(&mut c, ¬_responding(dir));
|
||||
}
|
||||
}
|
||||
|
||||
/// Measure the volume's free space, waiting up to [`QUERY_WAIT`]. The caller holds no lock.
|
||||
fn measure(&self, dir: &Path) {
|
||||
let done = ask(dir, false);
|
||||
let mut c = self.config();
|
||||
if c.dir.as_deref() != Some(dir) || done.as_ref().is_some_and(|d| d.finished()) {
|
||||
return;
|
||||
}
|
||||
let used = c.file.as_ref().map_or(0, |f| f.used());
|
||||
apply_space(&mut c, used, None);
|
||||
}
|
||||
|
||||
/// Retry a paused scratch disk once its backoff has elapsed. Cheap when not paused.
|
||||
@@ -376,40 +475,44 @@ impl Manager {
|
||||
if !self.paused.load(Ordering::Acquire) {
|
||||
return;
|
||||
}
|
||||
let mut c = match self.config.try_lock() {
|
||||
Ok(c) => c,
|
||||
Err(std::sync::TryLockError::Poisoned(p)) => p.into_inner(),
|
||||
// Someone else is configuring or retrying.
|
||||
Err(std::sync::TryLockError::WouldBlock) => return,
|
||||
};
|
||||
let c = self.config();
|
||||
let due = c.paused.as_ref().is_some_and(|p| Instant::now() >= p.retry_at);
|
||||
if !due {
|
||||
return;
|
||||
}
|
||||
if c.dir.is_none() || c.budget == 0 {
|
||||
c.paused = None;
|
||||
self.paused.store(false, Ordering::Release);
|
||||
return;
|
||||
}
|
||||
if self.prepare(&mut c) {
|
||||
self.resume(&mut c);
|
||||
self.retry_now(c);
|
||||
}
|
||||
|
||||
/// Retry a paused scratch disk now (the guard is released before the folder is asked).
|
||||
fn retry_now(&self, mut c: std::sync::MutexGuard<'_, Config>) {
|
||||
let dir = match c.dir.clone() {
|
||||
Some(dir) if c.budget > 0 => dir,
|
||||
_ => {
|
||||
c.paused = None;
|
||||
self.paused.store(false, Ordering::Release);
|
||||
return;
|
||||
}
|
||||
};
|
||||
// Push the next retry out so other threads don't all wait on this one.
|
||||
let wait = c.backoff;
|
||||
if let Some(p) = &mut c.paused {
|
||||
p.retry_at = Instant::now().checked_add(wait).unwrap_or_else(Instant::now);
|
||||
}
|
||||
drop(c);
|
||||
self.prepare(&dir);
|
||||
}
|
||||
|
||||
/// The scratch file, if spilling is on and the volume still has room.
|
||||
fn file(&self) -> Option<Arc<ScratchFile>> {
|
||||
let mut c = self.config();
|
||||
if self.budget.load(Ordering::Relaxed) == 0 {
|
||||
return None;
|
||||
}
|
||||
if c.file.is_none() && !self.prepare(&mut c) {
|
||||
return None;
|
||||
}
|
||||
c.file.clone()
|
||||
self.config().file.clone()
|
||||
}
|
||||
|
||||
/// Whether the scratch file may grow further; re-reads free space every
|
||||
/// [`SPACE_CHECK_EVERY`] bytes and pauses spilling when the volume runs low.
|
||||
/// Whether the scratch file may grow further; asks for free space every
|
||||
/// [`SPACE_CHECK_EVERY`] bytes (in the background, never waiting) and pauses spilling when the
|
||||
/// volume runs low.
|
||||
fn has_room(&self, file: &ScratchFile) -> bool {
|
||||
let used = file.used();
|
||||
if used < self.next_space_check.load(Ordering::Relaxed) && used < self.space_limit.load(Ordering::Relaxed) {
|
||||
@@ -417,8 +520,20 @@ impl Manager {
|
||||
}
|
||||
let mut c = self.config();
|
||||
let Some(dir) = c.dir.clone() else { return false };
|
||||
refresh_space(&c, &dir, used);
|
||||
if used < self.space_limit.load(Ordering::Relaxed) {
|
||||
// Ask in the background: this can be any thread, the UI's too. The answer moves the limit.
|
||||
drop(c);
|
||||
let query = start_query(&dir, false);
|
||||
let c = self.config();
|
||||
// A rejected request must remain due. Don't overwrite a new folder's state or an
|
||||
// answer that arrived before this lock was reacquired.
|
||||
if c.dir.as_deref() == Some(dir.as_path()) && !query.as_ref().is_some_and(|(done, _)| done.finished()) {
|
||||
if query.is_some() {
|
||||
self.next_space_check.store(used.saturating_add(SPACE_CHECK_EVERY), Ordering::Relaxed);
|
||||
} else {
|
||||
self.space_unmeasured.store(true, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if c.paused.is_none() {
|
||||
@@ -491,18 +606,128 @@ impl Manager {
|
||||
}
|
||||
}
|
||||
|
||||
/// Free space on the volume holding `dir`, minus the reserve, sets how far the file may grow.
|
||||
/// The caller holds the config lock, so this is the current directory; it also records whether the
|
||||
/// volume answered, for [`free_space_unmeasured`].
|
||||
fn refresh_space(c: &Config, dir: &Path, used: u64) {
|
||||
let space = free_space(dir);
|
||||
/// Record a volume's answer (`None` = it didn't say) and derive the growth limit from it.
|
||||
fn apply_space(c: &mut Config, used: u64, space: Option<(u64, u64)>) {
|
||||
M.space_unmeasured.store(space.is_none(), Ordering::Relaxed);
|
||||
let room = space.map_or(u64::MAX, |(available, total)| used.saturating_add(available.saturating_sub(RESERVE_MIN.max(total / 20))));
|
||||
let limit = c.size_cap.map_or(room, |cap| cap.min(room));
|
||||
M.space_limit.store(limit, Ordering::Relaxed);
|
||||
c.room = space.map(|(available, total)| used.saturating_add(available.saturating_sub(RESERVE_MIN.max(total / 20))));
|
||||
store_limit(c);
|
||||
M.next_space_check.store(used.saturating_add(SPACE_CHECK_EVERY), Ordering::Relaxed);
|
||||
}
|
||||
|
||||
/// Publish the scratch size the volume and the size limit allow.
|
||||
fn store_limit(c: &Config) {
|
||||
let room = c.room.unwrap_or(u64::MAX);
|
||||
M.space_limit.store(c.size_cap.map_or(room, |cap| cap.min(room)), Ordering::Relaxed);
|
||||
}
|
||||
|
||||
fn not_responding(dir: &Path) -> String {
|
||||
format!("scratch disk {} is not responding", dir.display())
|
||||
}
|
||||
|
||||
/// Start (or join) a query about `dir`: the same question is never asked by a second thread, and
|
||||
/// at most [`MAX_QUERIES`] run at once. `None` if no thread could be started.
|
||||
fn start_query(dir: &Path, prepare: bool) -> Option<(Arc<QueryDone>, Instant)> {
|
||||
let mut queries = QUERIES.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
if let Some(q) = queries.iter().find(|q| q.prepare == prepare && q.dir.as_path() == dir) {
|
||||
return Some((q.done.clone(), q.deadline));
|
||||
}
|
||||
if QUERY_THREADS.load(Ordering::Acquire) >= MAX_QUERIES {
|
||||
return None;
|
||||
}
|
||||
let now = Instant::now();
|
||||
let deadline = now.checked_add(QUERY_WAIT).unwrap_or(now);
|
||||
let done = Arc::new(QueryDone::default());
|
||||
queries.push(Query { dir: dir.to_path_buf(), prepare, deadline, done: done.clone() });
|
||||
let (job_dir, job_done) = (dir.to_path_buf(), done.clone());
|
||||
QUERY_THREADS.fetch_add(1, Ordering::AcqRel);
|
||||
let spawned = std::thread::Builder::new().name("pc-scratch-query".into()).spawn(move || run_query(job_dir, prepare, job_done));
|
||||
if spawned.is_err() {
|
||||
QUERY_THREADS.fetch_sub(1, Ordering::AcqRel);
|
||||
queries.pop();
|
||||
return None;
|
||||
}
|
||||
Some((done, deadline))
|
||||
}
|
||||
|
||||
/// Ask about `dir` and wait up to [`QUERY_WAIT`] for the answer. The caller holds no lock.
|
||||
fn ask(dir: &Path, prepare: bool) -> Option<Arc<QueryDone>> {
|
||||
let (done, deadline) = start_query(dir, prepare)?;
|
||||
done.wait_until(deadline);
|
||||
Some(done)
|
||||
}
|
||||
|
||||
/// The query thread: touches the folder, then hands the answer to [`Manager::settle`].
|
||||
fn run_query(dir: PathBuf, prepare: bool, done: Arc<QueryDone>) {
|
||||
let hook = *QUERY_HOOK.read().unwrap_or_else(PoisonError::into_inner);
|
||||
if let Some(hook) = hook {
|
||||
hook(&dir);
|
||||
}
|
||||
// Before measuring: writes made during the query come off the free space and must not be
|
||||
// counted again on top of it. Another folder's file says nothing about this one.
|
||||
let (open, used) = {
|
||||
let c = M.config();
|
||||
let current = c.file.as_ref().filter(|_| c.dir.as_deref() == Some(dir.as_path()));
|
||||
(prepare && current.is_none(), current.map_or(0, |f| f.used()))
|
||||
};
|
||||
let file = open.then(|| std::fs::create_dir_all(&dir).and_then(|()| ScratchFile::create(&dir)));
|
||||
let space = free_space(&dir);
|
||||
// Forget it first: a caller that joined it can rely on the answer, and a later caller asks anew.
|
||||
QUERIES.lock().unwrap_or_else(PoisonError::into_inner).retain(|q| !Arc::ptr_eq(&q.done, &done));
|
||||
M.settle(&dir, prepare, file, used, space, &done);
|
||||
QUERY_THREADS.fetch_sub(1, Ordering::AcqRel);
|
||||
}
|
||||
|
||||
/// Whether a query about `dir` is past its wait without an answer.
|
||||
fn query_overdue(dir: Option<&Path>) -> bool {
|
||||
let Some(dir) = dir else { return false };
|
||||
let now = Instant::now();
|
||||
let queries = QUERIES.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
queries.iter().any(|q| q.dir.as_path() == dir && now >= q.deadline)
|
||||
}
|
||||
|
||||
/// Scratch files on their way out, oldest first.
|
||||
struct Retired {
|
||||
files: VecDeque<(Arc<ScratchFile>, Arc<QueryDone>)>,
|
||||
/// Closer threads alive.
|
||||
closers: usize,
|
||||
}
|
||||
|
||||
/// Most closer threads at once: enough that one close stuck on a dead mount doesn't hold up the
|
||||
/// files behind it, few enough that stuck closes can't pile up threads.
|
||||
const MAX_CLOSERS: usize = 4;
|
||||
|
||||
static RETIRED: Mutex<Retired> = Mutex::new(Retired { files: VecDeque::new(), closers: 0 });
|
||||
|
||||
/// Let go of a scratch file off this thread: the last reference closes it, which can flush to its
|
||||
/// folder, and a dead mount blocks that, while leaving a dead folder is how a user recovers. The
|
||||
/// returned handle finishes when this reference is gone.
|
||||
fn retire(file: Arc<ScratchFile>) -> Arc<QueryDone> {
|
||||
let done = Arc::new(QueryDone::default());
|
||||
let mut retired = RETIRED.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
retired.files.push_back((file, done.clone()));
|
||||
// If no thread can start, the file waits in the queue for the next one rather than closing here.
|
||||
if retired.closers < MAX_CLOSERS && std::thread::Builder::new().name("pc-scratch-close".into()).spawn(close_retired).is_ok() {
|
||||
retired.closers += 1;
|
||||
}
|
||||
done
|
||||
}
|
||||
|
||||
fn close_retired() {
|
||||
loop {
|
||||
let next = {
|
||||
let mut retired = RETIRED.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
let next = retired.files.pop_front();
|
||||
if next.is_none() {
|
||||
retired.closers = retired.closers.saturating_sub(1);
|
||||
}
|
||||
next
|
||||
};
|
||||
let Some((file, done)) = next else { return };
|
||||
drop(file);
|
||||
done.finish();
|
||||
}
|
||||
}
|
||||
|
||||
fn no_room(dir: &Path, cap: Option<u64>) -> String {
|
||||
match cap {
|
||||
Some(cap) => format!("scratch disk {} reached its size limit ({} MB) or is low on free space", dir.display(), cap >> 20),
|
||||
@@ -560,22 +785,41 @@ fn disk_free_space(dir: &str) -> Option<(u64, u64)> {
|
||||
/// use its available figure. A listed disk counts as this volume when the mount point's own
|
||||
/// `statvfs` gives the same numbers (APFS volumes in one container share them, and firmlinks give
|
||||
/// `/Users` and `/` different device ids, so ids can't be compared); a change between the two
|
||||
/// readings just keeps the `statvfs` figure. Never fails.
|
||||
/// readings just keeps the `statvfs` figure. sysinfo asks every mounted volume for its properties,
|
||||
/// so an unrelated dead mount must not hold up this measurement: the lookup runs on a helper thread
|
||||
/// that is waited for at most [`PURGEABLE_WAIT`]. Never fails.
|
||||
#[cfg(target_os = "macos")]
|
||||
fn with_purgeable_space(dir: &Path, available: u64, total: u64) -> u64 {
|
||||
sysinfo::Disks::new_with_refreshed_list_specifics(sysinfo::DiskRefreshKind::nothing().with_storage())
|
||||
.ok()
|
||||
.and_then(|disks| {
|
||||
static LISTING: AtomicBool = AtomicBool::new(false);
|
||||
// A listing already running may be stuck on some mount: don't start another.
|
||||
if LISTING.swap(true, Ordering::AcqRel) {
|
||||
return available;
|
||||
}
|
||||
let dir = dir.to_path_buf();
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
let spawned = std::thread::Builder::new().name("pc-scratch-disks".into()).spawn(move || {
|
||||
let found: Option<u64> = sysinfo::Disks::new_with_refreshed_list_specifics(sysinfo::DiskRefreshKind::nothing().with_storage()).ok().and_then(|disks| {
|
||||
disks
|
||||
.iter()
|
||||
.filter(|d| dir.starts_with(d.mount_point()))
|
||||
.max_by_key(|d| d.mount_point().as_os_str().len())
|
||||
.filter(|d| volume_space(d.mount_point()) == Some((available, total)))
|
||||
.map(|d| d.available_space())
|
||||
})
|
||||
.unwrap_or(available)
|
||||
});
|
||||
LISTING.store(false, Ordering::Release);
|
||||
let _ = tx.send(found);
|
||||
});
|
||||
if spawned.is_err() {
|
||||
LISTING.store(false, Ordering::Release);
|
||||
return available;
|
||||
}
|
||||
rx.recv_timeout(PURGEABLE_WAIT).ok().flatten().unwrap_or(available)
|
||||
}
|
||||
|
||||
/// How long a measurement waits for the mounted-volume listing before using the `statvfs` figure.
|
||||
#[cfg(target_os = "macos")]
|
||||
const PURGEABLE_WAIT: Duration = Duration::from_millis(500);
|
||||
|
||||
#[cfg(all(any(unix, windows), not(target_os = "macos")))]
|
||||
fn with_purgeable_space(_dir: &Path, available: u64, _total: u64) -> u64 {
|
||||
available
|
||||
@@ -590,7 +834,11 @@ fn free_space(_dir: &Path) -> Option<(u64, u64)> {
|
||||
fn maybe_evict() {
|
||||
let budget = M.budget.load(Ordering::Relaxed);
|
||||
if budget == 0 {
|
||||
M.retry();
|
||||
// The supervisor retries when there is one; this can be any thread, the UI's too, and a
|
||||
// retry waits for the folder.
|
||||
if MAINTENANCE.load(Ordering::Acquire) == 0 {
|
||||
M.retry();
|
||||
}
|
||||
return;
|
||||
}
|
||||
let resident = M.resident.load(Ordering::Relaxed);
|
||||
@@ -660,25 +908,28 @@ pub fn set_size_cap(cap: Option<u64>) {
|
||||
return;
|
||||
}
|
||||
c.size_cap = cap;
|
||||
store_limit(&c);
|
||||
M.next_space_check.store(0, Ordering::Relaxed);
|
||||
if let Some(p) = &mut c.paused {
|
||||
p.retry_at = Instant::now();
|
||||
if c.paused.is_some() {
|
||||
c.backoff = RETRY_FIRST;
|
||||
M.retry_now(c);
|
||||
}
|
||||
drop(c);
|
||||
M.retry();
|
||||
}
|
||||
|
||||
/// Turn the scratch disk on (`dir` and a non-zero `budget_bytes`) or off. Tiles already on disk
|
||||
/// stay readable either way. Changing the directory starts a new scratch file there; the old one
|
||||
/// is deleted once its last tile is freed or changed. A directory that can't be used is kept and
|
||||
/// retried with backoff (spilling stays paused meanwhile); the error is returned and reported.
|
||||
/// Waits up to 2 s for the folder; a folder that doesn't answer counts as unusable until it does.
|
||||
pub fn configure(dir: Option<&Path>, budget_bytes: usize) -> Result<(), String> {
|
||||
let mut c = M.config();
|
||||
let dir = dir.filter(|_| budget_bytes > 0).map(Path::to_path_buf);
|
||||
if dir != c.dir {
|
||||
c.file = None;
|
||||
}
|
||||
let mut c = M.config();
|
||||
let old = if dir != c.dir {
|
||||
c.room = None;
|
||||
c.file.take()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
c.dir = dir.clone();
|
||||
c.budget = if dir.is_some() { budget_bytes } else { 0 };
|
||||
c.backoff = RETRY_FIRST;
|
||||
@@ -686,7 +937,22 @@ pub fn configure(dir: Option<&Path>, budget_bytes: usize) -> Result<(), String>
|
||||
c.last_error = None;
|
||||
M.paused.store(false, Ordering::Release);
|
||||
M.next_space_check.store(0, Ordering::Relaxed);
|
||||
if dir.is_some() && !M.prepare(&mut c) {
|
||||
drop(c);
|
||||
let start = Instant::now();
|
||||
let closing = old.map(retire);
|
||||
if let Some(dir) = &dir {
|
||||
M.prepare(dir);
|
||||
}
|
||||
// One wait for the whole change: closing the old file gets what the new folder left over.
|
||||
if let Some(closing) = closing {
|
||||
closing.wait_until(start.checked_add(QUERY_WAIT).unwrap_or(start));
|
||||
}
|
||||
let c = M.config();
|
||||
if c.dir != dir {
|
||||
// A newer configure owns the state now.
|
||||
return Ok(());
|
||||
}
|
||||
if c.paused.is_some() {
|
||||
return Err(c.last_error.clone().unwrap_or_default());
|
||||
}
|
||||
M.set_active(c.budget);
|
||||
@@ -753,7 +1019,7 @@ pub fn stats() -> SpillStats {
|
||||
last_error: c.last_error.clone(),
|
||||
paused: c.paused.is_some(),
|
||||
size_cap_bytes: c.size_cap,
|
||||
free_space_unmeasured: free_space_unmeasured(),
|
||||
free_space_unmeasured: enabled() && (M.space_unmeasured.load(Ordering::Relaxed) || query_overdue(c.dir.as_deref())),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -765,25 +1031,24 @@ pub fn last_error() -> Option<String> {
|
||||
/// Uncompressed tile bytes the scratch disk can still take before it reaches its size limit
|
||||
/// ([`set_size_cap`]) or the volume's free-space reserve, so an import can admit tile pixels
|
||||
/// beyond physical RAM. `None` while spilling is off or paused, and when neither the free space
|
||||
/// nor a size limit is known (nothing to promise). Tiles are written compressed; this counts
|
||||
/// them uncompressed, less 1/64 for the allocator's block rounding, so it never over-promises.
|
||||
/// nor a size limit is known (nothing to promise), which includes a folder that doesn't answer
|
||||
/// within 2 s. Tiles are written compressed; this counts them uncompressed, less 1/64 for the allocator's block rounding, so it never over-promises.
|
||||
pub fn scratch_capacity() -> Option<u64> {
|
||||
if !enabled() || paused() {
|
||||
return None;
|
||||
}
|
||||
let (dir, used, cap) = {
|
||||
let c = M.config();
|
||||
(c.dir.clone()?, c.file.as_ref().map_or(0, |f| f.used()), c.size_cap)
|
||||
};
|
||||
let space = free_space(&dir);
|
||||
// A slow query must not overwrite the status of a directory configured meanwhile.
|
||||
// The guard is a named local: as a temporary in the `if` condition it would drop before the store.
|
||||
let dir = M.config().dir.clone()?;
|
||||
M.measure(&dir);
|
||||
let c = M.config();
|
||||
if c.dir.as_deref() == Some(dir.as_path()) {
|
||||
M.space_unmeasured.store(space.is_none(), Ordering::Relaxed);
|
||||
// A slow query must not describe a directory configured meanwhile, and a sweep can also have
|
||||
// paused spilling meanwhile.
|
||||
if c.dir.as_deref() != Some(dir.as_path()) || c.paused.is_some() {
|
||||
return None;
|
||||
}
|
||||
let used = c.file.as_ref().map_or(0, |f| f.used());
|
||||
let cap = c.size_cap;
|
||||
let room = c.room.map(|room| room.saturating_sub(used));
|
||||
drop(c);
|
||||
let room = space.map(|(available, total)| available.saturating_sub(RESERVE_MIN.max(total / 20)));
|
||||
let room = match (room, cap) {
|
||||
(Some(room), Some(cap)) => room.min(cap.saturating_sub(used)),
|
||||
(Some(room), None) => room,
|
||||
@@ -794,10 +1059,18 @@ pub fn scratch_capacity() -> Option<u64> {
|
||||
}
|
||||
|
||||
/// Whether spilling is on but the scratch directory's volume won't report its free space, so the
|
||||
/// reserve doesn't apply and imports earn scratch room only up to a size limit. A plain atomic
|
||||
/// read, safe to call every frame.
|
||||
/// reserve doesn't apply and imports earn scratch room only up to a size limit. Two short locks,
|
||||
/// neither ever held across a query, so it is safe to call every frame. A query past its wait
|
||||
/// counts as unmeasured even with nobody waiting on it.
|
||||
pub fn free_space_unmeasured() -> bool {
|
||||
enabled() && M.space_unmeasured.load(Ordering::Relaxed)
|
||||
if !enabled() {
|
||||
return false;
|
||||
}
|
||||
if M.space_unmeasured.load(Ordering::Relaxed) {
|
||||
return true;
|
||||
}
|
||||
let c = M.config();
|
||||
query_overdue(c.dir.as_deref())
|
||||
}
|
||||
|
||||
/// Whether spilling is on (tiles beyond the budget go to disk).
|
||||
@@ -849,6 +1122,18 @@ pub fn scratch_handle_for_tests() -> Option<std::fs::File> {
|
||||
M.config().file.as_ref().and_then(|f| f.duplicate_handle().ok())
|
||||
}
|
||||
|
||||
/// Runs at the start of every folder query, off every lock: tests stall it to simulate a dead mount.
|
||||
#[doc(hidden)]
|
||||
pub fn set_query_hook_for_tests(hook: Option<fn(&Path)>) {
|
||||
*QUERY_HOOK.write().unwrap_or_else(PoisonError::into_inner) = hook;
|
||||
}
|
||||
|
||||
/// Query threads still alive, so a test can wait until a stalled query has fully settled.
|
||||
#[doc(hidden)]
|
||||
pub fn query_threads_for_tests() -> usize {
|
||||
QUERY_THREADS.load(Ordering::Acquire)
|
||||
}
|
||||
|
||||
/// Evict down to the budget now, on this thread (tests, and before a measurement).
|
||||
pub fn settle() {
|
||||
let budget = M.budget.load(Ordering::Relaxed);
|
||||
@@ -1002,6 +1287,79 @@ mod tests {
|
||||
assert_eq!(known_space(5, 10), Some((5, 10)));
|
||||
}
|
||||
|
||||
static CAPPED_GATE: (Mutex<bool>, Condvar) = (Mutex::new(false), Condvar::new());
|
||||
|
||||
fn hold_capped(dir: &Path) {
|
||||
if dir.file_name().and_then(|n| n.to_str()).is_some_and(|n| n.starts_with("photocraft-capped-")) {
|
||||
let mut open = CAPPED_GATE.0.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
while !*open {
|
||||
open = CAPPED_GATE.1.wait(open).unwrap_or_else(PoisonError::into_inner);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn query_threads_stay_capped_and_single_flight() {
|
||||
set_query_hook_for_tests(Some(hold_capped));
|
||||
let base = std::env::temp_dir().join(format!("photocraft-capped-{}", std::process::id()));
|
||||
let first = start_query(&base, false).unwrap();
|
||||
let again = start_query(&base, false).unwrap();
|
||||
assert!(Arc::ptr_eq(&first.0, &again.0) && first.1 == again.1, "the same question must not start a second thread");
|
||||
let mut started = 1;
|
||||
for i in 0..2 * MAX_QUERIES {
|
||||
if start_query(&base.join(format!("photocraft-capped-{i}")), false).is_some() {
|
||||
started += 1;
|
||||
}
|
||||
}
|
||||
assert_eq!(started, MAX_QUERIES);
|
||||
// Exercise the real growth path while every query slot is occupied. A separate manager
|
||||
// keeps this check independent of other tests' process-wide scratch configuration.
|
||||
let file = ScratchFile::create(&base).unwrap();
|
||||
let manager = Manager {
|
||||
failed: AtomicUsize::new(0),
|
||||
read_errors: AtomicU64::new(0),
|
||||
resident: AtomicUsize::new(0),
|
||||
live: AtomicUsize::new(0),
|
||||
budget: AtomicUsize::new(1),
|
||||
paused: AtomicBool::new(false),
|
||||
epoch: AtomicU64::new(0),
|
||||
space_limit: AtomicU64::new(u64::MAX),
|
||||
next_space_check: AtomicU64::new(0),
|
||||
recovering: AtomicBool::new(false),
|
||||
space_unmeasured: AtomicBool::new(false),
|
||||
registry: Mutex::new(Registry { cells: Vec::new(), hand: 0 }),
|
||||
config: Mutex::new(Config {
|
||||
dir: Some(base.join("growth")),
|
||||
file: None,
|
||||
last_error: None,
|
||||
budget: 1,
|
||||
paused: None,
|
||||
backoff: RETRY_FIRST,
|
||||
size_cap: None,
|
||||
room: None,
|
||||
}),
|
||||
evictions: AtomicU64::new(0),
|
||||
reloads: AtomicU64::new(0),
|
||||
written: AtomicU64::new(0),
|
||||
};
|
||||
let allowed = manager.has_room(&file);
|
||||
let next_check = manager.next_space_check.load(Ordering::Relaxed);
|
||||
let unmeasured = manager.space_unmeasured.load(Ordering::Relaxed);
|
||||
*CAPPED_GATE.0.lock().unwrap_or_else(PoisonError::into_inner) = true;
|
||||
CAPPED_GATE.1.notify_all();
|
||||
let start = Instant::now();
|
||||
while query_threads_for_tests() != 0 {
|
||||
assert!(start.elapsed() < Duration::from_secs(30), "queries never finished");
|
||||
std::thread::sleep(Duration::from_millis(10));
|
||||
}
|
||||
set_query_hook_for_tests(None);
|
||||
assert!(allowed, "the last measured growth limit still applies");
|
||||
assert_eq!(next_check, 0, "a rejected growth query must stay due");
|
||||
assert!(unmeasured, "a rejected growth query must not report the old measurement as current");
|
||||
drop(file);
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_read_retains_backing_and_blocks_commit_until_repaired() {
|
||||
let dir = std::env::temp_dir().join(format!("photocraft-read-failure-{}", std::process::id()));
|
||||
|
||||
@@ -4,6 +4,23 @@
|
||||
|
||||
use photocraft_raster::spill;
|
||||
|
||||
/// The room, once no query is outstanding. A query that outlives its 2 s wait on a slow machine
|
||||
/// counts as no answer, and its late answer would change the status between two reads, so ask again
|
||||
/// until the room and the status describe the same answer.
|
||||
fn answered_capacity() -> Option<u64> {
|
||||
let mut room = None;
|
||||
for _ in 0..10 {
|
||||
room = spill::scratch_capacity();
|
||||
while spill::query_threads_for_tests() != 0 {
|
||||
std::thread::sleep(std::time::Duration::from_millis(10));
|
||||
}
|
||||
if room.is_none() == spill::free_space_unmeasured() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
room
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scratch_capacity_follows_spilling_the_size_limit_and_pauses() {
|
||||
let root = std::env::temp_dir().join(format!("photocraft-scratch-capacity-{}", std::process::id()));
|
||||
@@ -16,13 +33,15 @@ fn scratch_capacity_follows_spilling_the_size_limit_and_pauses() {
|
||||
spill::configure(Some(&dir), 1 << 20).unwrap();
|
||||
assert!(spill::enabled() && !spill::paused());
|
||||
// With no size limit, the flag says exactly when the capacity is unknown.
|
||||
let unknown = spill::scratch_capacity().is_none();
|
||||
let unknown = answered_capacity().is_none();
|
||||
assert_eq!(spill::free_space_unmeasured(), unknown);
|
||||
// The free-space reserve applies; an unknown volume without a size limit promises nothing.
|
||||
if let Some(room) = spill::scratch_capacity() {
|
||||
assert!(room < u64::MAX / 2, "the reserve and margin are applied: {room}");
|
||||
}
|
||||
|
||||
let measurable = answered_capacity().is_some();
|
||||
|
||||
// A folder that stops answering is flagged and clears once it answers again. Windows keeps the
|
||||
// open scratch file's folder, so this part needs a folder that can be removed.
|
||||
if std::fs::remove_dir_all(&dir).is_ok() {
|
||||
@@ -30,9 +49,12 @@ fn scratch_capacity_follows_spilling_the_size_limit_and_pauses() {
|
||||
assert!(spill::free_space_unmeasured());
|
||||
assert!(spill::stats().free_space_unmeasured);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let unknown = spill::scratch_capacity().is_none();
|
||||
let unknown = answered_capacity().is_none();
|
||||
assert_eq!(spill::free_space_unmeasured(), unknown);
|
||||
assert_eq!(spill::stats().free_space_unmeasured, unknown);
|
||||
if measurable {
|
||||
assert!(!unknown, "the folder answers again: its room must be known again");
|
||||
}
|
||||
}
|
||||
|
||||
// The Scratch Size Limit bounds it, less the 1/64 margin, whatever the volume has free.
|
||||
|
||||
@@ -0,0 +1,208 @@
|
||||
//! A scratch folder that stops answering (a dead hard-mounted network share) must not freeze
|
||||
//! anything: status reads stay prompt, opening and measuring wait a bounded time, retries do not
|
||||
//! pile up threads, leaving the folder works, and a late answer applies only to the folder it
|
||||
//! was about.
|
||||
//!
|
||||
//! One test function: the spill manager is process-wide, so its steps must not interleave.
|
||||
|
||||
use std::path::Path;
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||
use std::sync::{Condvar, Mutex};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use photocraft_color::{ColorMode, PixelFormat, SampleType};
|
||||
use photocraft_geom::{Rect, TILE_SIZE};
|
||||
use photocraft_raster::{Surface, spill};
|
||||
|
||||
const TILE_BYTES: usize = (TILE_SIZE * TILE_SIZE) as usize * 4;
|
||||
|
||||
/// Folders whose name starts with "hung" stall their queries while this is on.
|
||||
static STALL: AtomicBool = AtomicBool::new(false);
|
||||
/// Queries that reached the stall since the last reset.
|
||||
static ENTERED: AtomicUsize = AtomicUsize::new(0);
|
||||
/// `true` once the stalled queries may go on.
|
||||
static GATE: (Mutex<bool>, Condvar) = (Mutex::new(false), Condvar::new());
|
||||
|
||||
fn stall(dir: &Path) {
|
||||
let hung = dir.file_name().and_then(|n| n.to_str()).is_some_and(|n| n.starts_with("hung"));
|
||||
if !STALL.load(Ordering::SeqCst) || !hung {
|
||||
return;
|
||||
}
|
||||
ENTERED.fetch_add(1, Ordering::SeqCst);
|
||||
let mut open = GATE.0.lock().unwrap();
|
||||
while !*open {
|
||||
open = GATE.1.wait(open).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
fn arm() {
|
||||
ENTERED.store(0, Ordering::SeqCst);
|
||||
*GATE.0.lock().unwrap() = false;
|
||||
STALL.store(true, Ordering::SeqCst);
|
||||
}
|
||||
|
||||
fn release() {
|
||||
STALL.store(false, Ordering::SeqCst);
|
||||
*GATE.0.lock().unwrap() = true;
|
||||
GATE.1.notify_all();
|
||||
}
|
||||
|
||||
fn poll_until(what: &str, cond: impl Fn() -> bool) {
|
||||
let start = Instant::now();
|
||||
while !cond() {
|
||||
assert!(start.elapsed() < Duration::from_secs(30), "timed out waiting for {what}: {:?}", spill::stats());
|
||||
std::thread::sleep(Duration::from_millis(10));
|
||||
}
|
||||
}
|
||||
|
||||
/// Run `f` and require that it returns well before a query's wait.
|
||||
fn prompt<T>(what: &str, f: impl FnOnce() -> T) -> T {
|
||||
let start = Instant::now();
|
||||
let out = f();
|
||||
assert!(start.elapsed() < Duration::from_millis(1500), "{what} took {:?}", start.elapsed());
|
||||
out
|
||||
}
|
||||
|
||||
/// A query's 2 s wait plus a scheduling allowance.
|
||||
const QUERY_DEADLINE: Duration = Duration::from_secs(6);
|
||||
|
||||
/// Run `f` and require that it returns within [`QUERY_DEADLINE`].
|
||||
fn bounded<T>(what: &str, f: impl FnOnce() -> T) -> T {
|
||||
let start = Instant::now();
|
||||
let out = f();
|
||||
assert!(start.elapsed() < QUERY_DEADLINE, "{what} took {:?}", start.elapsed());
|
||||
out
|
||||
}
|
||||
|
||||
fn rgba8() -> PixelFormat {
|
||||
PixelFormat { mode: ColorMode::Rgb, sample: SampleType::U8, alpha: true }
|
||||
}
|
||||
|
||||
/// Incompressible bytes, different per seed.
|
||||
fn noise(seed: u32, len: usize) -> Vec<u8> {
|
||||
let mut x = seed.wrapping_mul(2_654_435_761).max(1);
|
||||
(0..len)
|
||||
.map(|_| {
|
||||
x ^= x << 13;
|
||||
x ^= x >> 17;
|
||||
x ^= x << 5;
|
||||
x as u8
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn surface(seed: u32, n: i32) -> Surface {
|
||||
let mut s = Surface::new(rgba8());
|
||||
for i in 0..n {
|
||||
let r = Rect::from_xywh(i * TILE_SIZE, 0, TILE_SIZE as u32, TILE_SIZE as u32);
|
||||
s.write_interleaved(r, &noise(seed + i as u32, TILE_BYTES));
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_folder_that_stops_answering_never_freezes_the_app() {
|
||||
let root = std::env::temp_dir().join(format!("photocraft-scratch-slow-{}", std::process::id()));
|
||||
let good = root.join("good");
|
||||
let good2 = root.join("good2");
|
||||
let hung1 = root.join("hung1");
|
||||
let hung2 = root.join("hung2");
|
||||
let hung3 = root.join("hung3");
|
||||
spill::set_query_hook_for_tests(Some(stall));
|
||||
|
||||
// A. A healthy folder.
|
||||
spill::configure(Some(&good), 1 << 20).unwrap();
|
||||
assert!(spill::enabled() && !spill::paused());
|
||||
let measurable = spill::scratch_capacity().is_some();
|
||||
|
||||
// B. Configuring a folder that never answers: status reads stay prompt meanwhile (they
|
||||
// blocked on the config lock before), and configure gives up after its wait.
|
||||
arm();
|
||||
let began = Instant::now();
|
||||
let configuring = {
|
||||
let hung1 = hung1.clone();
|
||||
std::thread::spawn(move || spill::configure(Some(&hung1), 1 << 20))
|
||||
};
|
||||
poll_until("the first query to start", || ENTERED.load(Ordering::SeqCst) == 1);
|
||||
prompt("last_error", spill::last_error);
|
||||
prompt("check_integrity", spill::check_integrity).ok();
|
||||
prompt("stats", spill::stats);
|
||||
prompt("free_space_unmeasured", spill::free_space_unmeasured);
|
||||
prompt("paused", spill::paused);
|
||||
let result = configuring.join().unwrap();
|
||||
assert!(began.elapsed() < QUERY_DEADLINE, "configure of a hung folder took {:?}", began.elapsed());
|
||||
assert!(result.as_ref().err().is_some_and(|e| e.contains("not responding")), "{result:?}");
|
||||
assert!(spill::paused() && !spill::enabled());
|
||||
assert_eq!(spill::stats().dir.as_deref(), Some(hung1.as_path()));
|
||||
|
||||
// C. Retries neither wait nor start more threads: the question is already being asked.
|
||||
for _ in 0..5 {
|
||||
spill::retry_now_for_tests();
|
||||
prompt("maintain_budget", spill::maintain_budget);
|
||||
}
|
||||
assert_eq!(ENTERED.load(Ordering::SeqCst), 1, "a retry started a second query");
|
||||
assert!(spill::paused());
|
||||
|
||||
// D. Leaving the dead folder works while its query is stuck.
|
||||
prompt("configure away from a hung folder", || spill::configure(Some(&good2), 1 << 20)).unwrap();
|
||||
assert!(spill::enabled() && !spill::paused());
|
||||
|
||||
// E. The stuck query's late answer is about a folder that is no longer current: discarded.
|
||||
release();
|
||||
poll_until("the stuck query to settle", || spill::query_threads_for_tests() == 0);
|
||||
let st = spill::stats();
|
||||
assert_eq!(st.dir.as_deref(), Some(good2.as_path()), "{st:?}");
|
||||
assert!(spill::enabled() && !spill::paused() && st.last_error.is_none(), "{st:?}");
|
||||
|
||||
// F. A late answer for the current folder applies: spilling resumes by itself.
|
||||
arm();
|
||||
let result = bounded("configure of a second hung folder", || spill::configure(Some(&hung2), 1 << 20));
|
||||
assert!(result.is_err());
|
||||
assert!(spill::paused());
|
||||
release();
|
||||
poll_until("the late answer to resume spilling", || spill::enabled() && !spill::paused() && spill::last_error().is_none());
|
||||
assert_eq!(spill::stats().dir.as_deref(), Some(hung2.as_path()));
|
||||
|
||||
// G. The open folder stops answering when measured: no answer counts as unknown, spilling
|
||||
// goes on, and asking again costs nothing.
|
||||
spill::configure(Some(&hung3), 1 << 20).unwrap();
|
||||
arm();
|
||||
assert_eq!(bounded("scratch_capacity", spill::scratch_capacity), None);
|
||||
assert!(spill::free_space_unmeasured());
|
||||
assert!(spill::stats().free_space_unmeasured);
|
||||
assert!(spill::enabled() && !spill::paused());
|
||||
for _ in 0..5 {
|
||||
assert_eq!(prompt("scratch_capacity", spill::scratch_capacity), None);
|
||||
}
|
||||
assert_eq!(ENTERED.load(Ordering::SeqCst), 1, "asking again started a second query");
|
||||
release();
|
||||
if measurable {
|
||||
poll_until("the late answer to measure the volume", || !spill::free_space_unmeasured());
|
||||
}
|
||||
|
||||
poll_until("the previous query to finish", || spill::query_threads_for_tests() == 0);
|
||||
|
||||
// H. The growth check never stalls eviction: tiles still spill while it is unanswered.
|
||||
spill::set_budget(8 * TILE_BYTES);
|
||||
arm();
|
||||
spill::set_size_cap(Some(1 << 50));
|
||||
let trigger_tiles = surface(1, 32);
|
||||
poll_until("the growth query to reach the stall", || ENTERED.load(Ordering::SeqCst) >= 1);
|
||||
let evictions = spill::stats().evictions;
|
||||
let tiles = surface(33, 32);
|
||||
prompt("settle", spill::settle);
|
||||
assert!(spill::stats().evictions > evictions, "{:?}", spill::stats());
|
||||
poll_until("the unanswered growth check to count as unknown", spill::free_space_unmeasured);
|
||||
release();
|
||||
if measurable {
|
||||
poll_until("the growth check to be answered", || !spill::free_space_unmeasured());
|
||||
}
|
||||
spill::set_size_cap(None);
|
||||
|
||||
// Cleanup.
|
||||
spill::set_query_hook_for_tests(None);
|
||||
drop(tiles);
|
||||
drop(trigger_tiles);
|
||||
spill::configure(None, 0).unwrap();
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
}
|
||||
+15
-3
@@ -54,9 +54,21 @@ total is zero, as with some FUSE and WebDAV mounts) counts as unknown: the free-
|
||||
doesn't apply, imports earn a scratch allowance only up to the Scratch Size Limit, and
|
||||
Preferences → Scratch Disks and the hard-limit refusal say so, with the hint to set a limit or
|
||||
use a local disk; `spill::stats` exposes it as `freeSpaceUnmeasured`. With neither free space nor
|
||||
a size limit known, imports get no scratch allowance. The query runs on the thread that checks
|
||||
room, as creating the scratch file does, so a dead hard-mounted network folder can block it the
|
||||
same way its scratch I/O would.
|
||||
a size limit known, imports get no scratch allowance. Opening and measuring the folder happen
|
||||
off every lock, on a short-lived thread (at most one per folder and question, 16 overall), and
|
||||
callers wait up to 2 s for the answer. A query that doesn't answer in time counts as unknown
|
||||
(warned as above) and, when the scratch file couldn't be opened, pauses spilling with "not
|
||||
responding" and retries later. A system call can't be cancelled, so a stuck query keeps its
|
||||
thread until the mount answers and no second one starts; a late answer applies if that folder is
|
||||
still the current one and is discarded otherwise. The free-space check every 64 MB of growth never
|
||||
waits, and with an engine session tile allocation leaves retries to its supervisor. The wait is on
|
||||
the calling thread: changing Scratch Disks preferences or checking room for an import may wait up
|
||||
to those 2 s once for a newly hung folder. A whole folder change waits at most 2 s in total, the new
|
||||
folder first, with closing the old scratch file getting what is left; old files close on at most
|
||||
four background threads, so one stuck close doesn't hold up the others. On macOS the purgeable-space lookup asks every mounted volume, so it runs
|
||||
separately for at most 0.5 s, and an unrelated dead mount only loses the purgeable figure, never
|
||||
the folder's own numbers. Limit: reading or writing tile pixels already spilled
|
||||
to a hung folder still blocks the thread doing it; only opening and measuring are bounded.
|
||||
A failed read retains its backing extent and blocks editing, saving and exporting of the
|
||||
document that holds the tile until it is readable again; other open documents keep working.
|
||||
Each commit checks the health of its own document (before and after an edit, the document a
|
||||
|
||||
Reference in new issue
Block a user