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.

Phone items found one by one no longer read as a stalled engine (903)

gelenmedya48412ef43452
diff --git a/crates/rf-win/src/engine_guard.rs b/crates/rf-win/src/engine_guard.rs
index 6e618362..5532988b 100644
--- a/crates/rf-win/src/engine_guard.rs
+++ b/crates/rf-win/src/engine_guard.rs
@@ -32,7 +32,7 @@
//! real (on files the test creates in a temp folder) while Explorer is running.
use std::sync::Arc;
-use std::sync::atomic::{AtomicBool, Ordering};
+use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::time::{Duration, Instant};
/// The kinds of operation the shell engine does for the app.
@@ -192,22 +192,40 @@ pub fn not_run_without_shell(test: &str) -> bool {
/// How long the engine may stay without a first sign of life before the caller is told.
pub const START_LIMIT: Duration = Duration::from_secs(60);
-/// Raised by the engine's progress sink; read by [`supervise`].
-#[derive(Default)]
-pub struct Life(AtomicBool);
+/// Raised by the engine's progress sink; read by [`supervise`]. Before the engine starts, the job's own preparation beats too
+/// ([`Life::beat`]): every item it finds restarts the wait for the start.
+pub struct Life {
+ started: AtomicBool,
+ /// When the job last made progress of its own, in ms after `born`.
+ beat_ms: AtomicU64,
+ born: Instant,
+}
impl Life {
pub fn new() -> Arc<Life> {
- Arc::new(Life::default())
+ Arc::new(Life { started: AtomicBool::new(false), beat_ms: AtomicU64::new(0), born: Instant::now() })
}
/// The engine did something.
pub fn signal(&self) {
- self.0.store(true, Ordering::Release);
+ self.started.store(true, Ordering::Release);
}
pub fn alive(&self) -> bool {
- self.0.load(Ordering::Acquire)
+ self.started.load(Ordering::Acquire)
+ }
+
+ /// The job found one more of its items before handing them to the engine. WHY: a phone's items (MTP) are looked up one by one
+ /// on the device, and a few thousand of them took longer than [`START_LIMIT`] before the engine had anything to start: the
+ /// delete was reported as a stalled engine while it was still being prepared (issue 903). The limit now counts from the last
+ /// item found, so only a job that stops making progress at all is given up on.
+ pub fn beat(&self) {
+ self.beat_ms.store(self.born.elapsed().as_millis() as u64, Ordering::Release);
+ }
+
+ /// How long nothing has happened: since the job's last beat, or since it began.
+ fn quiet_for(&self) -> Duration {
+ self.born.elapsed().saturating_sub(Duration::from_millis(self.beat_ms.load(Ordering::Acquire)))
}
}
@@ -228,7 +246,7 @@ pub fn supervise_with<T: Send + 'static>(op: Op, life: Arc<Life>, limit: Duratio
match rx.recv_timeout(poll) {
Ok(r) => return r,
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
- if !life.alive() && began.elapsed() >= limit {
+ if !life.alive() && began.elapsed() >= limit && life.quiet_for() >= limit {
let waited = began.elapsed();
log::warn!("shell engine: {} did not start in {} s; the caller is told, the engine's thread is left", op.what(), waited.as_secs());
return Err(EngineStalled { op, waited }.into());
@@ -311,6 +329,21 @@ mod tests {
assert_eq!(stalled.op, Op::Rename);
}
+ #[test]
+ fn a_job_that_keeps_finding_items_is_not_given_up_on() {
+ // (issue 903: a phone's thousands of items took longer than the limit to look up, before the engine had anything to start)
+ let life = Life::new();
+ let beating = life.clone();
+ let r = supervise_with(Op::PermanentDelete, life, Duration::from_millis(100), Duration::from_millis(5), move || {
+ for _ in 0..10 {
+ std::thread::sleep(Duration::from_millis(40));
+ beating.beat();
+ }
+ Ok(7)
+ });
+ assert_eq!(r.expect("no stall while the items keep coming"), 7);
+ }
+
#[test]
fn a_panic_and_a_job_error_come_back_as_errors() {
let r = supervise_with::<()>(Op::Move, Life::new(), Duration::from_secs(5), Duration::from_millis(5), || panic!("boom"));
diff --git a/crates/rf-win/src/fileops.rs b/crates/rf-win/src/fileops.rs
index ab93fe20..d5e757fa 100644
--- a/crates/rf-win/src/fileops.rs
+++ b/crates/rf-win/src/fileops.rs
@@ -95,6 +95,7 @@ pub fn shell_copy(sources: &[PathBuf], dest_dir: &Path, flags: u32) -> anyhow::R
let dest = item(&dest_dir)?;
for s in &sources {
let it = item(s)?;
+ life.beat();
unsafe { op.CopyItem(&it, &dest, PCWSTR::null(), None)? };
}
anyhow::ensure!(perform_tracked(&op, quiet_tracker(), life)?, "the copy was aborted");
@@ -126,6 +127,7 @@ fn transfer_through_engine(kind: Transfer, sources: &[PathBuf], dest_dir: &Path,
let dest = item(&dest_dir)?;
for s in &sources {
let it = item(s)?;
+ life.beat();
unsafe {
match kind {
Transfer::Copy => op.CopyItem(&it, &dest, PCWSTR::null(), None)?,
@@ -204,6 +206,7 @@ fn delete_through_engine(paths: &[PathBuf], permanent: bool, flags: u32, tracker
let op = new_op(flags | extra)?;
for p in &paths {
unsafe { op.DeleteItem(&item(p)?, None)? };
+ life.beat();
}
perform_tracked(&op, tracker, life)
})