misterdownloadmanager: The ultimate open-source, clean-room download manager written from scratch in pure Rust for Windows, Mac, and Linux
Rust bittorrent-clientdebriddownload-acceleratordownload-managerdownloadmanagereguifile-downloadergallery-dlidmidm-alternativeinternet-download-managerm3u8-downloadermagnetplaylist-downloaderreal-debridrusttorrent-clientvideo-downloaderyoutube-downloaderyt-dlp
Downloads no longer stop and go when the disk is slow to flush (#361) (#425)
Ahmed Nefzaoui9ac7984c8782
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 5189e471..c43651cd 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,7 @@ Each release's notes on GitHub come from its section here (`## [X.Y.Z]`, see### Fixed+- Downloads at full speed no longer stop and go on a slow disk: writing the resume state flushed the file to the disk so often that every connection waited for it, and were then replaced as slow ([#361](https://github.com/anefzaoui/misterdownloadmanager/issues/361)).- A save folder shown as `Downloads/Video` could be taken literally, asking to create it in the wrong place or failing with "Read-only file system" on macOS.- macOS: **Download video** no longer stops with a Python error and a "Python.framework is damaged" alert on a copy of MDM downloaded by a browser ([#330](https://github.com/anefzaoui/misterdownloadmanager/issues/330), [#339](https://github.com/anefzaoui/misterdownloadmanager/issues/339)). MDM now installs yt-dlp's folder build, which it unpacks once and checks against yt-dlp's signed list, instead of the single-program build that unpacks Python afresh at every start. The video window then offers to install it in place of a copy that came with an earlier version.- Browser link import: tighter handling of site data.diff --git a/ROADMAP.md b/ROADMAP.mdindex 3c36f896..e14ef615 100644--- a/ROADMAP.md+++ b/ROADMAP.md@@ -13,6 +13,9 @@ Everything below ships together, in version 0.5.0.### Done+- ✅ Fast downloads keep going on a slow disk: flushing the resume state no longer holds up+ the connections, and a connection is never replaced for the disk's wait+ ([#361](https://github.com/anefzaoui/misterdownloadmanager/issues/361))- ✅ Freeze reports taken after the window has already answered say so, instead of blaming a deadlock(part of [#355](https://github.com/anefzaoui/misterdownloadmanager/issues/355))- ✅ **Move/Rename** works on several selected downloads at once: pick a folder and they all go therediff --git a/crates/engine/src/download.rs b/crates/engine/src/download.rsindex 86c198c0..b3ce5e75 100644--- a/crates/engine/src/download.rs+++ b/crates/engine/src/download.rs@@ -39,6 +39,11 @@ const BATCH: usize = 256 * 1024;/// Received bytes are written at least this often, so a slow connection shows progress and a/// pause keeps what arrived.const FLUSH_EVERY: Duration = Duration::from_secs(1);+/// While the resume save flushes the part file to the disk (`Shared::flushing`), the connections+/// keep what they receive in memory, at most this much for the whole download, instead of+/// waiting on the disk (#361): on NTFS a file's writes wait for its flush, whatever their+/// handle (measured: 8 connections' writes held for each whole flush, 3-10 s on a slow disk).+const FLUSH_HOLD_MAX: usize = 256 << 20;/// Shedding a slow connection (`Options::shed_slow`): after `SHED_GRACE` on a response, a/// connection under 1/`SHED_RATIO` of the best speed one connection reached, with more than@@ -132,6 +137,19 @@ pub struct Options {/// name, so whatever the hook attaches to it — Windows' Mark of the Web stream, macOS'/// quarantine attribute — stays with the finished file.pub on_file_created: Option<FileHook>,+ /// Tests: a disk whose flush takes this long and holds every write to the file meanwhile+ /// (as NTFS does, #361). None: the real disk.+ #[doc(hidden)]+ pub slow_flush: Option<SlowFlush>,+}++/// A disk whose flush takes `takes` and, as NTFS's does, makes every write to the file wait for+/// it: the gate is held exclusively for the flush and shared for each write (`Options::slow_flush`).+#[doc(hidden)]+#[derive(Clone, Debug)]+pub struct SlowFlush {+ pub takes: Duration,+ pub gate: Arc<std::sync::RwLock<()>>,}/// A callback given the path of a file the engine created (`Options::on_file_created`).@@ -209,6 +227,7 @@ impl Default for Options {extra_roots: Vec::new(),post: None,on_file_created: None,+ slow_flush: None,}}}@@ -965,6 +984,8 @@ impl Download {planner: Mutex::new(None),limiter: Limiter::new(None),best_speed: Default::default(),+ flushing: Default::default(),+ held: Default::default(),});let task = tokio::spawn(f(shared.clone(), cancel.clone()));Download {@@ -1048,6 +1069,12 @@ struct Shared {/// The best speed one connection of this download sustained (≥ 1 s), bytes/s: what a good/// connection to this server does, the yardstick for shedding.best_speed: std::sync::atomic::AtomicU64,+ /// The resume save is flushing the part file to the disk right now (`save_state`): the+ /// connections hold what they receive (`hold_cap`).+ flushing: std::sync::atomic::AtomicBool,+ /// Bytes received and claimed but not yet written, in the connections' buffers: shown as+ /// downloaded (they are), but never saved as such.+ held: std::sync::atomic::AtomicU64,}impl Shared {@@ -1055,7 +1082,7 @@ impl Shared {let mut s = self.view.lock().unwrap().clone();s.server = self.seen.get().and_then(|seen| seen.get());if let Some(p) = self.planner.lock().unwrap().as_ref() {- s.downloaded = p.downloaded();+ s.downloaded = p.downloaded() + self.held.load(Ordering::Relaxed);s.segments = p.segments().iter()@@ -2102,6 +2129,7 @@ async fn segmented(let mut synced = Synced {at: std::time::Instant::now(),bytes: 0,+ took: Duration::ZERO,};save_state(&ctx, &mut saved, &path, &mut synced, true).await?;let interval = ctx.opts.save_interval;@@ -2148,25 +2176,49 @@ async fn segmented(}/// While downloading, the part file is flushed (and the state rewritten) only after 1/16 of-/// the file (1–64 MiB) has arrived or this long, at most once per save tick, and always when+/// the file (1-64 MiB) has arrived or this long, at most once per save tick, and always when/// the connections stop. Flushing every tick stalled HDDs and churned antivirus; IDM saves-/// "several times per minute" [IDM-doc]. A crash costs at most that much re-download.+/// "several times per minute" [IDM-doc].+///+/// A flush holds every write to the file for as long as it runs (NTFS, measured, #361), so on a+/// disk where it takes seconds, flushing as often as the file grows left the download frozen+/// most of the time. The next flush therefore waits nine times as long as the last one took,+/// at most `SYNC_GAP_MAX`: at most a tenth of the time goes to flushing, and on a fast disk+/// (a flush of a few milliseconds) nothing changes. What it gives up: after a power cut or a+/// crash the bytes since the last flush are downloaded again, so on a slow disk up to+/// `SYNC_GAP_MAX` of them (at 1 Gbit/s about 7 GB) instead of about 2 s of them. The saved+/// positions still never claim bytes that weren't flushed.const SYNC_BYTES_MAX: u64 = 64 << 20;const SYNC_BYTES_MIN: u64 = 1 << 20;const SYNC_EVERY: Duration = Duration::from_secs(30);+const SYNC_GAP_MAX: Duration = Duration::from_secs(60);fn sync_bytes(size: u64) -> u64 {(size / 16).clamp(SYNC_BYTES_MIN, SYNC_BYTES_MAX)}-/// When the part file was last flushed, and how many bytes were on disk then.+/// How long to wait after a flush that took `took` before the next.+fn flush_gap(took: Duration) -> Duration {+ took.saturating_mul(9).min(SYNC_GAP_MAX)+}++/// When the part file was last flushed, how many bytes were on disk then, and how long it took.struct Synced {at: std::time::Instant,bytes: u64,+ took: Duration,+}++impl Synced {+ /// Is a flush due with `bytes` on disk now, `elapsed` since the last one ended?+ fn due(&self, bytes: u64, size: u64, elapsed: Duration) -> bool {+ elapsed >= flush_gap(self.took)+ && (bytes >= self.bytes.saturating_add(sync_bytes(size)) || elapsed >= SYNC_EVERY)+ }}/// Save the resume state if it is due (`force`: always). `saved` is the state last written; it-/// never claims bytes that weren't flushed (snapshot → flush → write).+/// never claims bytes that weren't flushed (snapshot -> flush -> write).async fn save_state(ctx: &Ctx,saved: &mut ResumeState,@@ -2182,9 +2234,7 @@ async fn save_state(|| final_url != saved.final_url|| final_get != saved.final_get;let bytes: u64 = segments.iter().map(|s| s.cursor - s.start).sum();- let due = force- || bytes >= synced.bytes.saturating_add(sync_bytes(ctx.size))- || synced.at.elapsed() >= SYNC_EVERY;+ let due = force || synced.due(bytes, ctx.size, synced.at.elapsed());let file = ctx.file.clone();let path = path.to_owned();if !due {@@ -2200,20 +2250,43 @@ async fn save_state(}return Ok(());}- *synced = Synced {- at: std::time::Instant::now(),- bytes,- };if segments == saved.segments && !meta_changed && path.exists() {- return Ok(()); // nothing new since the last write+ // Nothing new since the last write.+ *synced = Synced {+ at: std::time::Instant::now(),+ bytes,+ took: Duration::ZERO,+ };+ return Ok(());}let mut s = saved.clone();s.validators = validators;s.final_url = final_url;s.final_get = final_get;- *saved = tokio::task::spawn_blocking(move || s.save(&path, segments, &file).map(|()| s))- .await- .expect("state writer panicked")?;+ // Tests' slow disk (not for the forced saves at the start and the end).+ let slow = if force {+ None+ } else {+ ctx.opts.slow_flush.clone()+ };+ ctx.shared.flushing.store(true, Ordering::Release);+ let began = std::time::Instant::now();+ let done = tokio::task::spawn_blocking(move || {+ if let Some(slow) = slow {+ let _writes_wait = slow.gate.write().unwrap();+ std::thread::sleep(slow.takes);+ }+ s.save(&path, segments, &file).map(|()| s)+ })+ .await+ .expect("state writer panicked");+ ctx.shared.flushing.store(false, Ordering::Release);+ *saved = done?;+ *synced = Synced {+ at: std::time::Instant::now(),+ bytes,+ took: began.elapsed(),+ };Ok(())}@@ -2962,6 +3035,9 @@ async fn receive(let mut buf = Vec::with_capacity(BATCH);let mut flushed_at = std::time::Instant::now();let started = std::time::Instant::now();+ // Time spent waiting for the disk (`flush`): a connection isn't slow for that (#361), it+ // wasn't reading the network then.+ let mut on_disk = Duration::ZERO;let mut received = 0u64;let mut checked = started;loop {@@ -3054,17 +3130,23 @@ async fn receive(limits.working(ctx.target().url.host_str().unwrap_or(""), now);}buf.extend_from_slice(&chunk[..n as usize]);+ ctx.shared.held.fetch_add(n, Ordering::Relaxed);received += n;if checked.elapsed() >= Duration::from_millis(500) {checked = std::time::Instant::now();- let speed = (received as f64 / started.elapsed().as_secs_f64()) as u64;+ let on_net = started.elapsed().saturating_sub(on_disk);+ let speed = (received as f64 / on_net.as_secs_f64()) as u64;ctx.shared.conn(id, |c| c.speed = speed);- note_best(ctx, received, started);- if should_shed(ctx, id, speed, started.elapsed(), pos + buf.len() as u64) {+ note_best(ctx, received, on_net);+ if should_shed(ctx, id, speed, on_net, pos + buf.len() as u64) {flush(ctx, id, &mut pos, &mut buf).await?;return Err(Error::SlowConnection);}}+ if !full && buf.len() < hold_cap(ctx) && ctx.shared.flushing.load(Ordering::Acquire)+ {+ continue; // the disk is busy flushing: this waits in memory (#361)+ }if buf.len() < BATCH && !full && flushed_at.elapsed() < FLUSH_EVERY {continue;}@@ -3073,10 +3155,13 @@ async fn receive(Err(e) => (true, Some(e)),Ok(None) => (true, None),};+ let waiting = std::time::Instant::now();+ let flushed = flush(ctx, id, &mut pos, &mut buf).await?;flushed_at = std::time::Instant::now();- match flush(ctx, id, &mut pos, &mut buf).await? {+ on_disk += flushed_at - waiting;+ match flushed {Some(end) => {- note_best(ctx, received, started);+ note_best(ctx, received, started.elapsed().saturating_sub(on_disk));// A segment another connection shortened leaves an HTTP body far from its end// (dropped); an FTP session is kept either way.if err.is_none() && (pos >= to || matches!(body, Body::Ftp(_))) {@@ -3094,10 +3179,16 @@ async fn receive(}}+/// How much a connection may hold in memory while the part file is flushed (`FLUSH_HOLD_MAX`,+/// shared by the connections this download may open).+fn hold_cap(ctx: &Ctx) -> usize {+ FLUSH_HOLD_MAX / ctx.opts.connections().max(1) as usize+}+/// Remember the speed of a response that carried enough (256 KiB) to say what this server can-/// do over one connection.-fn note_best(ctx: &Ctx, received: u64, started: std::time::Instant) {- let secs = started.elapsed().as_secs_f64();+/// do over one connection (`on_net`: the time it took, waits for the disk not counted).+fn note_best(ctx: &Ctx, received: u64, on_net: Duration) {+ let secs = on_net.as_secs_f64();if received >= 256 * 1024 && secs > 0.0 {ctx.shared.best_speed@@ -3134,9 +3225,15 @@ async fn flush(if n > 0 {let file = ctx.file.clone();let at = *pos;- tokio::task::spawn_blocking(move || write_all_at(&file, at, &data))- .await- .expect("writer panicked")?;+ let slow = ctx.opts.slow_flush.clone();+ let written = tokio::task::spawn_blocking(move || {+ let _waits_for_flush = slow.as_ref().map(|s| s.gate.read().unwrap());+ write_all_at(&file, at, &data)+ })+ .await+ .expect("writer panicked");+ ctx.shared.held.fetch_sub(n, Ordering::Relaxed);+ written?;}*pos += n;ctx.shared.conn(id, |c| c.downloaded += n);@@ -3329,6 +3426,28 @@ mod tests {);}+ /// #361: a flush that took a while pushes the next one back, on a fast disk nothing changes.+ #[test]+ fn a_slow_flush_makes_the_next_one_wait() {+ let s = Duration::from_secs;+ let at = |took| Synced {+ at: std::time::Instant::now(),+ bytes: 0,+ took,+ };+ let gb = 1u64 << 30;+ // 3 s for the last flush: not again before 27 s, however many bytes came.+ assert!(!at(s(3)).due(gb, 45 * gb, s(20)));+ assert!(at(s(3)).due(gb, 45 * gb, s(27)));+ // At most SYNC_GAP_MAX, whatever the flush took.+ assert!(at(s(30)).due(gb, 45 * gb, s(61)));+ // A flush of a few milliseconds: the old rules (each tick once 64 MiB have arrived).+ assert!(at(Duration::from_millis(8)).due(128 << 20, 45 * gb, s(2)));+ assert!(!at(Duration::from_millis(8)).due(1 << 20, 45 * gb, s(2)));+ // Few bytes on a fast disk: still the long wait.+ assert!(at(Duration::from_millis(8)).due(1 << 20, 45 * gb, s(30)));+ }+#[test]fn flush_step_scales_with_the_file() {assert_eq!(sync_bytes(24 << 20), 3 << 19);diff --git a/crates/engine/tests/e2e.rs b/crates/engine/tests/e2e.rsindex 174c6439..23461e10 100644--- a/crates/engine/tests/e2e.rs+++ b/crates/engine/tests/e2e.rs@@ -1547,3 +1547,49 @@ async fn a_one_time_link_is_read_from_the_kept_answer() {};assert!(Download::start(req, fast_opts()).wait().await.is_err());}++/// #361: a flush that holds up every write (as on NTFS) must not hold up the download: the+/// connections keep receiving meanwhile and none of them reconnects for it. Before: the+/// connections stood still during each 1.5 s flush, one per save tick, and were then told apart+/// as slow (reconnections, on top of the stalls).+#[tokio::test]+async fn a_slow_flush_does_not_stop_the_transfer() {+ let s = server("rate=20M").await; // 8 connections, about 160 MB/s together+ let dir = tempfile::tempdir().unwrap();+ let opts = Options {+ save_interval: std::time::Duration::from_millis(100),+ slow_flush: Some(mdm_engine::download::SlowFlush {+ takes: std::time::Duration::from_millis(1500),+ gate: Default::default(),+ }),+ ..fast_opts()+ };+ let started = std::time::Instant::now();+ let out = Download::start(+ Request {+ url: s.url("/gen/160M/a.bin?seed=361").parse().unwrap(),+ headers: Default::default(),+ dest: Destination::Dir(dir.path().to_owned()),+ },+ opts,+ )+ .wait()+ .await+ .expect("download succeeds");+ let took = started.elapsed();+ assert_eq!(out.size, 160 << 20);+ let data = std::fs::read(&out.path).unwrap();+ let mut want = vec![0; data.len()];+ content::fill(361, 0, &mut want);+ assert!(data == want, "content mismatch");+ tokio::time::timeout(SETTLE, s.settled())+ .await+ .expect("every request ends");+ let log = s.log();+ // Unobstructed: about a second, flush included. Before the fix: every tick's flush stalled+ // all writes for 1.5 s, well over 5 s in all.+ assert!(took < std::time::Duration::from_secs(4), "took {took:?}");+ // No connection reconnected because of the disk's stall: the probe, seven more, and the+ // planner's closing splits only.+ assert!(log.len() < 24, "{} requests: {log:?}", log.len());+}