Skip to content

misterdownloadmanager: The ultimate open-source, clean-room download manager written from scratch in pure Rust for Windows, Mac, and Linux

Engine: a download stops only once its progress save is done (#106) (#115)

Ahmed Nefzaouic6436be3dc60
diff --git a/CHANGELOG.md b/CHANGELOG.md
index cc7c56e6..b32ac3c1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -212,6 +212,11 @@ Each release's notes on GitHub come from its section here (`## [X.Y.Z]`, see
### Fixed
+- **A download's progress file could be written again just after it stopped**
+ ([#106](https://github.com/anefzaoui/misterdownloadmanager/issues/106)). MDM saves a
+ download's progress every few seconds, in a file beside it. A download paused or finished
+ during one of those saves could stop before the save was done. That save then wrote an old
+ progress file a moment later. A download now stops only once its save is done.
- **A video download could fail in MDM while yt-dlp went on and finished it**
([#79](https://github.com/anefzaoui/misterdownloadmanager/issues/79)). A line yt-dlp printed in
the system's code page rather than UTF-8 (a title in Turkish or Russian on some Windows) ended
diff --git a/ROADMAP.md b/ROADMAP.md
index b319371b..ee633ba9 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -13,6 +13,8 @@ Everything below ships together, in one release.
### Done
+- ✅ A paused or finished download no longer leaves its progress file to be written again a moment
+ later ([#106](https://github.com/anefzaoui/misterdownloadmanager/issues/106))
- ✅ Tests and test tools stay on 127.0.0.1: no Windows Firewall questions while developing
- ✅ Downloads through one network adapter (a VPN's), with a kill switch: nothing goes out
another way when it disconnects ([#35](https://github.com/anefzaoui/misterdownloadmanager/issues/35))
diff --git a/crates/engine/src/download.rs b/crates/engine/src/download.rs
index bc39c525..babe3a38 100644
--- a/crates/engine/src/download.rs
+++ b/crates/engine/src/download.rs
@@ -1,4 +1,4 @@
-//! One download: probe, parallel connections driven by the planner, positional writes, progress.
+//! One download: probe, parallel connections driven by the planner, positional writes, progress.
//!
//! The first connection is also the probe: a plain `GET` (no Range, as IDM does — D-007) that
//! keeps streaming from byte 0 while further connections split the file (notes/ENGINE.md §1).
@@ -1935,18 +1935,33 @@ async fn segmented(
save_state(&ctx, &mut saved, &path, &mut synced, true).await?;
let interval = ctx.opts.save_interval;
let result = {
- // Runs until a save fails; resolves to that error.
+ // Saves until told to stop or a save fails (resolving to that error). It stops only
+ // between saves: a save's blocking write can't be cancelled, so dropping the saver
+ // mid-save would leave that write running after the download has returned, writing a
+ // stale state file next to a finished or already resumed download (#106).
+ let stop = CancellationToken::new();
let saver = async {
loop {
- tokio::time::sleep(interval).await;
+ tokio::select! {
+ () = tokio::time::sleep(interval) => {}
+ () = stop.cancelled() => return None,
+ }
if let Err(e) = save_state(&ctx, &mut saved, &path, &mut synced, false).await {
- return e;
+ return Some(e);
}
}
};
+ let supervisor = supervise(ctx.clone(), first);
+ tokio::pin!(saver, supervisor);
tokio::select! {
- r = supervise(ctx.clone(), first) => r,
- e = saver => Err(e),
+ r = &mut supervisor => {
+ // The connections stopped: let a save under way finish. Its error, if any, is
+ // the final save's to report (it writes the same file, forced, just below).
+ stop.cancel();
+ let _ = saver.await;
+ r
+ }
+ e = &mut saver => Err(e.expect("the saver only stops when told to")),
}
};
match &result {