Skip to content

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

A new download saves its progress at the first tick, even when its first save was slow (part of #518) (#542)

Ahmed Nefzaoui979db33633c0
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 663f44c5..199a24ca 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,7 @@ Each release's notes on GitHub come from its section here (`## [X.Y.Z]`, see
### Fixed
+- A download's progress is saved from its first seconds again: since 0.5.2, when the disk was slow to write the resume state as the download started (no data had arrived yet), the first save of its progress waited nine times as long, up to a minute, so a short download cut off by a crash or a power cut in that time started over from the beginning (part of [#518](https://github.com/anefzaoui/misterdownloadmanager/issues/518)).
- macOS: **Download File Info** opens in front of the browser that asked for it ([#484](https://github.com/anefzaoui/misterdownloadmanager/issues/484)).
- The browser's **video button** over a hover-to-play preview (YouTube's thumbnails) no longer vanishes as the pointer goes to it: it stays where it was while the pointer is on it, a click downloads that video, and it fades once the pointer leaves ([#503](https://github.com/anefzaoui/misterdownloadmanager/issues/503)).
- **Enter new address to download**: an address with a login in **Options → Sites Logins** shows that login under **Use authorization** (ticked, the user name and the password), the one the download uses; it was used but the box stayed empty. A login typed there still goes first ([#511](https://github.com/anefzaoui/misterdownloadmanager/issues/511)).
diff --git a/ROADMAP.md b/ROADMAP.md
index efe62a9e..231b1404 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -528,7 +528,8 @@ These ship in a later version, when they're ready.
Logins, though the download used it
([#511](https://github.com/anefzaoui/misterdownloadmanager/issues/511)): fix in review
- 🔄 Two tests that fail now and then on busy CI runners
- ([#518](https://github.com/anefzaoui/misterdownloadmanager/issues/518)): the slow-flush one in review
+ ([#518](https://github.com/anefzaoui/misterdownloadmanager/issues/518)): the slow-flush one fixed;
+ the crash-and-resume one found a real delay in saving a new download's progress, fix in review
- 🔄 The window not answering for a few seconds while Windows makes a snapshot of the disk
(a restore point or a backup) ([#513](https://github.com/anefzaoui/misterdownloadmanager/issues/513)):
the log and the RSS feeds are now written beside the window, as the list already was; fix in
diff --git a/crates/app/tests/cli_crash.rs b/crates/app/tests/cli_crash.rs
index 24a751de..32bb200e 100644
--- a/crates/app/tests/cli_crash.rs
+++ b/crates/app/tests/cli_crash.rs
@@ -41,6 +41,10 @@ async fn killed_download_resumes_correctly() {
{
break st.downloaded();
}
+ // A download that ends first never saves progress for this test to find (#518).
+ if let Some(status) = child.try_wait().unwrap() {
+ panic!("the download ended ({status}) before saving any progress");
+ }
tokio::time::sleep(Duration::from_millis(100)).await;
};
child.kill().unwrap();
diff --git a/crates/engine/src/download.rs b/crates/engine/src/download.rs
index 7361d3ad..ec9b586b 100644
--- a/crates/engine/src/download.rs
+++ b/crates/engine/src/download.rs
@@ -2237,12 +2237,12 @@ async fn segmented(
///
/// 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.
+/// most of the time. The next flush therefore waits nine times as long as the last flush during
+/// the transfer took (`forced_gap`), 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);
@@ -2340,11 +2340,19 @@ async fn save_state(
*synced = Synced {
at: std::time::Instant::now(),
bytes,
- took: began.elapsed(),
+ took: forced_gap(force, began.elapsed()),
};
Ok(())
}
+/// The flush time that spaces out the next flush. A forced save runs while no connection
+/// writes (before they start, after they stop), so it holds no transfer up and doesn't push the
+/// next save back: the first one at the start, which also pays for creating the part file, once
+/// delayed the first save of progress past the end of a short download (#518).
+fn forced_gap(force: bool, took: Duration) -> Duration {
+ if force { Duration::ZERO } else { took }
+}
+
/// A plain 403, which a browser's User-Agent may have brought on (#372). A site's bot check
/// isn't asked again: that one stays the site's call (#376).
fn agent_refused(e: &Error) -> bool {
@@ -3509,6 +3517,21 @@ mod tests {
assert!(at(Duration::from_millis(8)).due(1 << 20, 45 * gb, s(30)));
}
+ /// #518: when the forced save at the start (no connection writing yet) takes 0.5 s on a busy
+ /// disk, the first save of progress still comes at the first tick, not 4.5 s later.
+ #[test]
+ fn a_forced_save_does_not_push_the_next_back() {
+ let at = |took| Synced {
+ at: std::time::Instant::now(),
+ bytes: 0,
+ took,
+ };
+ let (slow, tick) = (Duration::from_millis(500), Duration::from_secs(2));
+ assert!(at(forced_gap(true, slow)).due(15 << 20, 24 << 20, tick));
+ // A save during the transfer still does.
+ assert!(!at(forced_gap(false, slow)).due(15 << 20, 24 << 20, tick));
+ }
+
#[test]
fn flush_step_scales_with_the_file() {
assert_eq!(sync_bytes(24 << 20), 3 << 19);