Skip to content

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

The window no longer waits on the clipboard or on a slow disk (#418, #423) (#432)

Ahmed Nefzaoui4ad5d822cf54
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c43651cd..865840bb 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
+- The window no longer freezes while watching the clipboard when the program holding it hangs, or while a cancelled download's files are removed from a slow disk ([#418](https://github.com/anefzaoui/misterdownloadmanager/issues/418), [#423](https://github.com/anefzaoui/misterdownloadmanager/issues/423)).
- 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.
diff --git a/ROADMAP.md b/ROADMAP.md
index e14ef615..6b7047b6 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -13,6 +13,9 @@ Everything below ships together, in version 0.5.0.
### Done
+- ✅ The window keeps answering while reading the clipboard or removing a cancelled download's
+ files from a slow disk ([#418](https://github.com/anefzaoui/misterdownloadmanager/issues/418),
+ [#423](https://github.com/anefzaoui/misterdownloadmanager/issues/423))
- ✅ 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))
diff --git a/crates/app/src/app.rs b/crates/app/src/app.rs
index a70b892a..20a9eb32 100644
--- a/crates/app/src/app.rs
+++ b/crates/app/src/app.rs
@@ -557,6 +557,8 @@ pub struct App {
/// yet) and when.
clip_seen: Option<Option<String>>,
clip_checked: Option<Instant>,
+ /// The clipboard read under way, off the window thread (#418).
+ clip_reading: Option<ClipRead>,
confirm_delete: Option<dialogs::ConfirmDelete>,
/// Queue right-click → Delete, asking first: the queue and the question.
confirm_queue_delete: Option<(usize, dialogs::Confirm)>,
@@ -749,6 +751,38 @@ fn seconds_now() -> u32 {
chrono::Local::now().num_seconds_from_midnight()
}
+/// A read of the clipboard on a thread of its own, its answer taken when it has come (#418).
+struct ClipRead(std::sync::Arc<std::sync::Mutex<Option<Option<String>>>>);
+
+impl ClipRead {
+ fn start(read: impl FnOnce() -> Option<String> + Send + 'static) -> Self {
+ let slot = std::sync::Arc::new(std::sync::Mutex::new(None));
+ let answer = slot.clone();
+ let _ = std::thread::Builder::new()
+ .name("clipboard read".into())
+ .spawn(move || {
+ *answer.lock().unwrap() = Some(read());
+ });
+ ClipRead(slot)
+ }
+
+ /// The text, once the read has answered; else the read, still waiting.
+ fn answer(self) -> Result<Option<String>, Self> {
+ let got = self.0.lock().unwrap().take();
+ got.ok_or(self)
+ }
+}
+
+/// A stopped download's files: its state and part files, and its finished file when it was
+/// complete (a Stop-then-remove of a download that finished meanwhile).
+fn remove_files_of(dest: &std::path::Path, complete: bool) {
+ let _ = std::fs::remove_file(state_path(dest));
+ let _ = std::fs::remove_file(mdm_engine::naming::part_path(dest));
+ if complete {
+ let _ = std::fs::remove_file(dest);
+ }
+}
+
/// The icon for a file name's extension (empty: none).
fn file_icon(name: &str) -> IconKey {
match name.rsplit_once('.') {
@@ -1045,6 +1079,7 @@ impl App {
limit_notice: None,
clip_seen: None,
clip_checked: None,
+ clip_reading: None,
tray: None,
tray_limiter: None,
// Started with the session (#226): hidden from the first frame.
@@ -1495,21 +1530,30 @@ impl App {
}
let Some(item) = self.item(id) else { continue };
let old = item.dest.clone();
- let _ = std::fs::remove_file(state_path(&old));
- let _ = std::fs::remove_file(mdm_engine::naming::part_path(&old));
match then {
None => {
- // Cancelled: a file that finished meanwhile goes too, with its windows.
- if item.status == Status::Complete {
- let _ = std::fs::remove_file(&old);
- }
+ // Cancelled: a file that finished meanwhile goes too, with its windows. On
+ // another thread: a slow or stuck disk took the window with it (#423).
+ let complete = item.status == Status::Complete;
+ self.rt.spawn_blocking({
+ let old = old.clone();
+ move || remove_files_of(&old, complete)
+ });
self.complete.retain(|c| c.id != id);
self.delete(id);
}
Some((dest, start)) => {
- if item.status == Status::Complete {
- let _ = std::fs::remove_file(&old);
+ // A restart under another name: the old files go off the window thread
+ // (#423). Under the same name they must be gone before the new start.
+ let complete = item.status == Status::Complete;
+ if dest == old {
+ remove_files_of(&old, complete);
+ } else {
+ let old = old.clone();
+ self.rt
+ .spawn_blocking(move || remove_files_of(&old, complete));
}
+ let item = self.item(id).expect("the download just found");
if let Some(name) = dest.file_name() {
item.file_name = name.to_string_lossy().into_owned();
}
@@ -2948,14 +2992,31 @@ impl App {
return;
}
ctx.request_repaint_after(Duration::from_secs(1));
- if self
- .clip_checked
- .is_some_and(|t| t.elapsed() < Duration::from_secs(1))
- {
- return;
- }
- self.clip_checked = Some(Instant::now());
- let text = crate::clipboard_text().map(|t| t.trim().to_owned());
+ // One read at a time, on its own thread: the program that owns the clipboard may hang,
+ // and a read that waits for it took the window with it (#418). A read under way
+ // that never answers just means no new look at the clipboard.
+ let reading = self.clip_reading.take();
+ let text = match reading {
+ Some(read) => match read.answer() {
+ Ok(text) => text,
+ Err(still) => {
+ self.clip_reading = Some(still);
+ return;
+ }
+ },
+ None => {
+ if self
+ .clip_checked
+ .is_some_and(|t| t.elapsed() < Duration::from_secs(1))
+ {
+ return;
+ }
+ self.clip_checked = Some(Instant::now());
+ self.clip_reading = Some(ClipRead::start(crate::clipboard_text));
+ return;
+ }
+ };
+ let text = text.map(|t| t.trim().to_owned());
// The first look only notes what is there: turning it on doesn't grab an old link.
let Some(before) = self.clip_seen.replace(text.clone()) else {
return;
@@ -8799,3 +8860,50 @@ mod page_data_tests {
assert_eq!(page_data_for("https://a.test/x", Some("nope"), DATA), None);
}
}
+
+#[cfg(test)]
+mod window_thread_tests {
+ use super::*;
+
+ /// #418: a clipboard read that never answers (its owner hung) doesn't hold up the window:
+ /// its answer just isn't there yet; one that answers is taken.
+ #[test]
+ fn a_stuck_clipboard_owner_does_not_hold_the_window() {
+ let (release, wait) = std::sync::mpsc::channel::<()>();
+ let stuck = ClipRead::start(move || {
+ let _ = wait.recv();
+ Some("late".to_owned())
+ });
+ let started = Instant::now();
+ let Err(mut waiting) = stuck.answer() else {
+ panic!("no answer yet");
+ };
+ assert!(started.elapsed() < Duration::from_millis(500));
+ release.send(()).unwrap();
+ let text = loop {
+ match waiting.answer() {
+ Ok(text) => break text,
+ Err(still) => waiting = still,
+ }
+ std::thread::sleep(Duration::from_millis(2));
+ };
+ assert_eq!(text.as_deref(), Some("late"));
+ }
+
+ /// #423: the files of a stopped download go as a group, off the window's thread.
+ #[test]
+ fn a_stopped_downloads_files_are_removed_together() {
+ let dir = tempfile::tempdir().unwrap();
+ let dest = dir.path().join("file.bin");
+ let part = mdm_engine::naming::part_path(&dest);
+ let state = mdm_engine::state::state_path(&dest);
+ for f in [&dest, &part, &state] {
+ std::fs::write(f, b"x").unwrap();
+ }
+ remove_files_of(&dest, false);
+ assert!(!part.exists() && !state.exists());
+ assert!(dest.exists(), "a file that wasn't complete stays");
+ remove_files_of(&dest, true);
+ assert!(!dest.exists());
+ }
+}