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
Windows: the clipboard read and written one at a time, no more heap corruption (#629) (#639)
Ahmed Nefzaouiba2cbabd68d1
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex cc46719a..cb11a362 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -45,6 +45,7 @@ Each release's notes on GitHub come from its section here (`## [X.Y.Z]`, see- Windows: closing the window hides MDM in the notification area again when Windows keeps its icon among the hidden icons (behind ^), where a new icon goes. MDM took the icon for missing there and left the window in the taskbar instead; it now does that only when Windows has no icon of MDM's at all ([#545](https://github.com/anefzaoui/misterdownloadmanager/issues/545), [#534](https://github.com/anefzaoui/misterdownloadmanager/issues/534), [#527](https://github.com/anefzaoui/misterdownloadmanager/issues/527)).- The browser's **video button** over YouTube's thumbnail previews keeps one place, so it can be clicked: it moved to another corner when the preview showed its sound and captions buttons, back when they hid as the pointer reached it, and away again, and it followed the preview as it shrank. Over the same video it now only moves on to a free corner, never back, and it stays put while the pointer is on it ([#560](https://github.com/anefzaoui/misterdownloadmanager/issues/560)).- Chrome, Edge, Brave and the other Chromium browsers: a download the extension leaves to the browser keeps the name and folder another extension gives it (Save In's folders, or any extension's own download into a folder). The extension's answer for it counted as a name of its own, an empty one, and when the extension was the newest installed it won: the file landed in the Downloads folder, and the browser could warn of a conflict between the two extensions ([#578](https://github.com/anefzaoui/misterdownloadmanager/issues/578)).+- Windows: MDM no longer crashes ("crash in ntdll.dll", the heap corrupted) when the clipboard is read in two places at once, as when **Add URL** opened while the clipboard monitor was looking for a copied link. Windows lets the parts of one program open the clipboard together, and their reads spoiled each other's data; MDM's reads and writes of the clipboard, a paste or copy in a text box included, now go one at a time ([#629](https://github.com/anefzaoui/misterdownloadmanager/issues/629)).- macOS: downloads to a network volume (a folder under `/Volumes` on an SMB, AFP or NFS share) start again. Each one was refused with "Not enough disk space ... 0 bytes free", however much the share had: macOS answers 0 when asked a network volume's space "for important usage" (the free space plus what the system can clear, as Finder counts it on the Mac's own disks), and MDM took that 0. On a network volume, or whenever that answer is 0, MDM now counts the free space the volume reports ([#623](https://github.com/anefzaoui/misterdownloadmanager/issues/623)).### Addeddiff --git a/Cargo.lock b/Cargo.lockindex 62fb9135..4ff494ad 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -1908,7 +1908,7 @@ checksum = "d0881ea181b1df73ff77ffaaf9c7544ecc11e82fba9b5f27b262a3c73a332555"[[package]]name = "ecolor"version = "0.37.0"-source = "git+https://github.com/anefzaoui/egui.git?rev=d110a3ccf2f1710f3e67db3f910b18003732c1df#d110a3ccf2f1710f3e67db3f910b18003732c1df"+source = "git+https://github.com/anefzaoui/egui.git?rev=4ad60e5365470a6d12166a8075478f8aea44c9f8#4ad60e5365470a6d12166a8075478f8aea44c9f8"dependencies = ["bytemuck","emath",@@ -1917,7 +1917,7 @@ dependencies = [[[package]]name = "eframe"version = "0.37.0"-source = "git+https://github.com/anefzaoui/egui.git?rev=d110a3ccf2f1710f3e67db3f910b18003732c1df#d110a3ccf2f1710f3e67db3f910b18003732c1df"+source = "git+https://github.com/anefzaoui/egui.git?rev=4ad60e5365470a6d12166a8075478f8aea44c9f8#4ad60e5365470a6d12166a8075478f8aea44c9f8"dependencies = ["ahash","bytemuck",@@ -1953,7 +1953,7 @@ dependencies = [[[package]]name = "egui"version = "0.37.0"-source = "git+https://github.com/anefzaoui/egui.git?rev=d110a3ccf2f1710f3e67db3f910b18003732c1df#d110a3ccf2f1710f3e67db3f910b18003732c1df"+source = "git+https://github.com/anefzaoui/egui.git?rev=4ad60e5365470a6d12166a8075478f8aea44c9f8#4ad60e5365470a6d12166a8075478f8aea44c9f8"dependencies = ["accesskit","ahash",@@ -1972,7 +1972,7 @@ dependencies = [[[package]]name = "egui-wgpu"version = "0.37.0"-source = "git+https://github.com/anefzaoui/egui.git?rev=d110a3ccf2f1710f3e67db3f910b18003732c1df#d110a3ccf2f1710f3e67db3f910b18003732c1df"+source = "git+https://github.com/anefzaoui/egui.git?rev=4ad60e5365470a6d12166a8075478f8aea44c9f8#4ad60e5365470a6d12166a8075478f8aea44c9f8"dependencies = ["ahash","bytemuck",@@ -1991,7 +1991,7 @@ dependencies = [[[package]]name = "egui-winit"version = "0.37.0"-source = "git+https://github.com/anefzaoui/egui.git?rev=d110a3ccf2f1710f3e67db3f910b18003732c1df#d110a3ccf2f1710f3e67db3f910b18003732c1df"+source = "git+https://github.com/anefzaoui/egui.git?rev=4ad60e5365470a6d12166a8075478f8aea44c9f8#4ad60e5365470a6d12166a8075478f8aea44c9f8"dependencies = ["accesskit_winit","arboard",@@ -2016,7 +2016,7 @@ dependencies = [[[package]]name = "egui_extras"version = "0.37.0"-source = "git+https://github.com/anefzaoui/egui.git?rev=d110a3ccf2f1710f3e67db3f910b18003732c1df#d110a3ccf2f1710f3e67db3f910b18003732c1df"+source = "git+https://github.com/anefzaoui/egui.git?rev=4ad60e5365470a6d12166a8075478f8aea44c9f8#4ad60e5365470a6d12166a8075478f8aea44c9f8"dependencies = ["ahash","egui",@@ -2032,7 +2032,7 @@ dependencies = [[[package]]name = "egui_glow"version = "0.37.0"-source = "git+https://github.com/anefzaoui/egui.git?rev=d110a3ccf2f1710f3e67db3f910b18003732c1df#d110a3ccf2f1710f3e67db3f910b18003732c1df"+source = "git+https://github.com/anefzaoui/egui.git?rev=4ad60e5365470a6d12166a8075478f8aea44c9f8#4ad60e5365470a6d12166a8075478f8aea44c9f8"dependencies = ["bytemuck","egui",@@ -2060,7 +2060,7 @@ dependencies = [[[package]]name = "emath"version = "0.37.0"-source = "git+https://github.com/anefzaoui/egui.git?rev=d110a3ccf2f1710f3e67db3f910b18003732c1df#d110a3ccf2f1710f3e67db3f910b18003732c1df"+source = "git+https://github.com/anefzaoui/egui.git?rev=4ad60e5365470a6d12166a8075478f8aea44c9f8#4ad60e5365470a6d12166a8075478f8aea44c9f8"dependencies = ["bytemuck",]@@ -2129,7 +2129,7 @@ dependencies = [[[package]]name = "epaint"version = "0.37.0"-source = "git+https://github.com/anefzaoui/egui.git?rev=d110a3ccf2f1710f3e67db3f910b18003732c1df#d110a3ccf2f1710f3e67db3f910b18003732c1df"+source = "git+https://github.com/anefzaoui/egui.git?rev=4ad60e5365470a6d12166a8075478f8aea44c9f8#4ad60e5365470a6d12166a8075478f8aea44c9f8"dependencies = ["ahash","bytemuck",@@ -2155,7 +2155,7 @@ dependencies = [[[package]]name = "epaint_default_fonts"version = "0.37.0"-source = "git+https://github.com/anefzaoui/egui.git?rev=d110a3ccf2f1710f3e67db3f910b18003732c1df#d110a3ccf2f1710f3e67db3f910b18003732c1df"+source = "git+https://github.com/anefzaoui/egui.git?rev=4ad60e5365470a6d12166a8075478f8aea44c9f8#4ad60e5365470a6d12166a8075478f8aea44c9f8"[[package]]name = "equivalent"@@ -4442,6 +4442,7 @@ dependencies = ["eframe","egui","egui-wgpu",+ "egui-winit","egui_extras","image","libc",diff --git a/Cargo.toml b/Cargo.tomlindex f9fc8aa3..0b51c03e 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -33,6 +33,8 @@ craft-i18n = { git = "https://github.com/storytold/craft-libs", rev = "1a11a4897# a clipboard of its own, and Ctrl+V / Cmd+V never sees what other programs copied).eframe = { version = "0.37.0", default-features = false, features = ["glow", "default_fonts", "accesskit", "clipboard"] }egui = "0.37.0"+# The lock that keeps MDM's own clipboard reads and writes apart from egui's (main.rs, #629).+egui-winit = { version = "0.37.0", default-features = false }egui_extras = { version = "0.37.0", features = ["svg", "image"] }image = { version = "0.25.10", default-features = false, features = ["png"] }dirs = "7.0.0"@@ -186,7 +188,9 @@ debug = "line-tables-only"# can ask for a pass (Context::request_ui_pass: a dialog opened while minimized, #162); OpenGL# without a usable configuration is an error, not a panic, so the next renderer is tried; a main# window built hidden stays hidden after its first frame (a start with the session waits in the-# notification area, #226); a monitor that has just gone away has no size instead of a panic (#335). craft-libs can take the same commits to share one egui again.+# notification area, #226); a monitor that has just gone away has no size instead of a panic (#335); the clipboard used+# by one thread at a time, egui's copy and paste and MDM's own reads (`egui_winit::clipboard::+# lock_system_clipboard`, #629). craft-libs can take the same commits to share one egui again.[patch.crates-io]# librqbit 9.1.0 with per-torrent rate limits that change while a torrent runs (#39), and a# magnet lookup's progress counted (#92), and every socket bound to one address (`bind_ip`,@@ -204,14 +208,14 @@ librqbit-peer-protocol = { git = "https://github.com/anefzaoui/rqbit.git", rev =librqbit-sha1-wrapper = { git = "https://github.com/anefzaoui/rqbit.git", rev = "ef50d87f4a9d5be9e3951b74f722433f9b46bd76" }librqbit-tracker-comms = { git = "https://github.com/anefzaoui/rqbit.git", rev = "ef50d87f4a9d5be9e3951b74f722433f9b46bd76" }librqbit-upnp = { git = "https://github.com/anefzaoui/rqbit.git", rev = "ef50d87f4a9d5be9e3951b74f722433f9b46bd76" }-ecolor = { git = "https://github.com/anefzaoui/egui.git", rev = "d110a3ccf2f1710f3e67db3f910b18003732c1df" }-emath = { git = "https://github.com/anefzaoui/egui.git", rev = "d110a3ccf2f1710f3e67db3f910b18003732c1df" }-epaint = { git = "https://github.com/anefzaoui/egui.git", rev = "d110a3ccf2f1710f3e67db3f910b18003732c1df" }-epaint_default_fonts = { git = "https://github.com/anefzaoui/egui.git", rev = "d110a3ccf2f1710f3e67db3f910b18003732c1df" }-egui = { git = "https://github.com/anefzaoui/egui.git", rev = "d110a3ccf2f1710f3e67db3f910b18003732c1df" }-egui-winit = { git = "https://github.com/anefzaoui/egui.git", rev = "d110a3ccf2f1710f3e67db3f910b18003732c1df" }-egui_glow = { git = "https://github.com/anefzaoui/egui.git", rev = "d110a3ccf2f1710f3e67db3f910b18003732c1df" }-egui-wgpu = { git = "https://github.com/anefzaoui/egui.git", rev = "d110a3ccf2f1710f3e67db3f910b18003732c1df" }-egui_extras = { git = "https://github.com/anefzaoui/egui.git", rev = "d110a3ccf2f1710f3e67db3f910b18003732c1df" }-eframe = { git = "https://github.com/anefzaoui/egui.git", rev = "d110a3ccf2f1710f3e67db3f910b18003732c1df" }+ecolor = { git = "https://github.com/anefzaoui/egui.git", rev = "4ad60e5365470a6d12166a8075478f8aea44c9f8" }+emath = { git = "https://github.com/anefzaoui/egui.git", rev = "4ad60e5365470a6d12166a8075478f8aea44c9f8" }+epaint = { git = "https://github.com/anefzaoui/egui.git", rev = "4ad60e5365470a6d12166a8075478f8aea44c9f8" }+epaint_default_fonts = { git = "https://github.com/anefzaoui/egui.git", rev = "4ad60e5365470a6d12166a8075478f8aea44c9f8" }+egui = { git = "https://github.com/anefzaoui/egui.git", rev = "4ad60e5365470a6d12166a8075478f8aea44c9f8" }+egui-winit = { git = "https://github.com/anefzaoui/egui.git", rev = "4ad60e5365470a6d12166a8075478f8aea44c9f8" }+egui_glow = { git = "https://github.com/anefzaoui/egui.git", rev = "4ad60e5365470a6d12166a8075478f8aea44c9f8" }+egui-wgpu = { git = "https://github.com/anefzaoui/egui.git", rev = "4ad60e5365470a6d12166a8075478f8aea44c9f8" }+egui_extras = { git = "https://github.com/anefzaoui/egui.git", rev = "4ad60e5365470a6d12166a8075478f8aea44c9f8" }+eframe = { git = "https://github.com/anefzaoui/egui.git", rev = "4ad60e5365470a6d12166a8075478f8aea44c9f8" }winit = { git = "https://github.com/storytold/winit.git", rev = "a517b3846ee8457d84ee4975110bfda6797c44dc" }diff --git a/ROADMAP.md b/ROADMAP.mdindex 2492764b..78245448 100644--- a/ROADMAP.md+++ b/ROADMAP.md@@ -561,6 +561,9 @@ These ship in a later version, when they're ready.### Bugs being looked into+- 🔄 Windows: a crash when the clipboard was read in two places at once (the clipboard monitor and+ Add URL) ([#629](https://github.com/anefzaoui/misterdownloadmanager/issues/629)): every read and+ write of the clipboard now goes one at a time; fix in review- 🔄 Categories without their own folder following General's folder (`Video`, `Music`, `Torrents`inside it), and Add torrent starting with the Torrents category's folder instead of flippingfrom General when metadata arrives ([#556](https://github.com/anefzaoui/misterdownloadmanager/issues/556),diff --git a/crates/app/Cargo.toml b/crates/app/Cargo.tomlindex b1fcf392..76897329 100644--- a/crates/app/Cargo.toml+++ b/crates/app/Cargo.toml@@ -27,6 +27,7 @@ craft-atomic.workspace = truedirs.workspace = trueeframe.workspace = trueegui.workspace = true+egui-winit.workspace = trueegui_extras.workspace = trueimage.workspace = truecraft-i18n.workspace = truediff --git a/crates/app/src/main.rs b/crates/app/src/main.rsindex 0e0b6bc8..8790b207 100644--- a/crates/app/src/main.rs+++ b/crates/app/src/main.rs@@ -121,8 +121,29 @@ pub fn clipboard_text() -> Option<String> {})}+/// How long a read or write of the clipboard waits for the one under way in this process.+/// Ours take milliseconds; one that waits on a program that owns the clipboard and hangs+/// (#418) is given up on, so the window's thread is never held by it for longer.+const CLIPBOARD_WAIT: std::time::Duration = std::time::Duration::from_millis(500);++/// One use of the clipboard at a time in the whole process, egui's copy and paste included+/// (the same lock, `egui_winit::clipboard::lock_system_clipboard`): Windows lets the threads+/// of a process open it at the same time, and two reads at once (the clipboard monitor's and+/// Add URL's) corrupted the heap and ended MDM (#629). None: the wait ran out.+fn clipboard_lock() -> Option<egui_winit::clipboard::SystemClipboardLock> {+ let lock = egui_winit::clipboard::lock_system_clipboard(CLIPBOARD_WAIT);+ if lock.is_none() {+ tracing::warn!("clipboard: still in use elsewhere in MDM, not read or written");+ }+ lock+}+fn read_clipboard(off: bool, read: impl FnOnce() -> Option<String>) -> Option<String> {- if off { None } else { read() }+ if off {+ return None;+ }+ let _lock = clipboard_lock()?;+ read()}/// Put `text` on the clipboard (egui hands it to the system after the frame). An off-screen@@ -137,9 +158,11 @@ pub fn copy_text(ctx: &egui::Context, text: String) {/// and true).pub fn set_clipboard(text: &str) -> bool {match write_clipboard(offscreen(), text.to_owned()) {- Some(text) => arboard::Clipboard::new()- .and_then(|mut c| c.set_text(text))- .is_ok(),+ Some(text) => clipboard_lock().is_some_and(|_lock| {+ arboard::Clipboard::new()+ .and_then(|mut c| c.set_text(text))+ .is_ok()+ }),None => true,}}@@ -469,4 +492,57 @@ mod tests {Some("magnet:?xt=urn:btih:ab".into()));}++ /// #629: two reads of the clipboard at the same moment (the clipboard monitor's thread and+ /// Add URL on the window's) corrupted the heap. Every read waits for the one under way.+ #[test]+ fn clipboard_reads_are_one_at_a_time() {+ use std::sync::atomic::{AtomicUsize, Ordering};+ let inside = std::sync::Arc::new(AtomicUsize::new(0));+ let most = std::sync::Arc::new(AtomicUsize::new(0));+ let threads: Vec<_> = (0..8)+ .map(|_| {+ let (inside, most) = (inside.clone(), most.clone());+ std::thread::spawn(move || {+ for _ in 0..25 {+ super::read_clipboard(false, || {+ let now = inside.fetch_add(1, Ordering::SeqCst) + 1;+ most.fetch_max(now, Ordering::SeqCst);+ std::thread::sleep(std::time::Duration::from_millis(1));+ inside.fetch_sub(1, Ordering::SeqCst);+ None+ });+ }+ })+ })+ .collect();+ for t in threads {+ t.join().unwrap();+ }+ assert_eq!(most.load(Ordering::SeqCst), 1, "reads at the same moment");+ }++ /// #629 on the real clipboard: several threads read it at once for a few seconds, and the+ /// process lives. Ignored by default (it reads the clipboard of the person at the computer;+ /// it never writes it, and what it reads is dropped unseen):+ /// `cargo test -p mdm-app --bin misterdownloadmanager -- --ignored real_clipboard`.+ #[test]+ #[ignore = "reads the real clipboard"]+ fn real_clipboard_reads_from_many_threads_live() {+ let until = std::time::Instant::now() + std::time::Duration::from_secs(3);+ let threads: Vec<_> = (0..6)+ .map(|_| {+ std::thread::spawn(move || {+ let mut reads = 0u32;+ while std::time::Instant::now() < until {+ std::hint::black_box(super::clipboard_text().map(|t| t.len()));+ reads += 1;+ }+ reads+ })+ })+ .collect();+ let reads: u32 = threads.into_iter().map(|t| t.join().unwrap()).sum();+ assert!(reads > 0);+ }}