diff --git a/crates/ui-egui/src/update.rs b/crates/ui-egui/src/update.rs index fc302f532..16d249fc0 100644 --- a/crates/ui-egui/src/update.rs +++ b/crates/ui-egui/src/update.rs @@ -889,6 +889,12 @@ fn poll_install(app: &mut SolveApp) { Err(TryRecvError::Disconnected) => Err("the update stopped".into()), }; app.updater.job = None; + // Cancel already cleared the job in the UI; ignore a late "cancelled" result so Offer returns + // without an error banner (#8908). + if matches!(&result, Err(e) if e == "cancelled" || e.contains("cancelled")) { + app.updater.installed = None; + return; + } match &result { Ok(install::Outcome::Installer(program, args)) => { // The installer replaces this program: start it once SolidCraft has closed. On @@ -1084,9 +1090,12 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) { Some("restart") => restart(app), Some("close") => app.updater.open = false, Some("cancel_download") => { - if let Some((_, progress)) = &app.updater.job { + // Drop the job from the UI at once so Cancel leaves "Downloading" immediately; the + // worker still sees the flag and stops at the next feed/checksum/chunk check (#8908). + if let Some((_, progress)) = app.updater.job.take() { progress.cancel.store(true, std::sync::atomic::Ordering::Relaxed); } + app.updater.installed = None; } _ if closed => app.updater.open = false, _ => {} diff --git a/crates/ui-egui/src/update/install.rs b/crates/ui-egui/src/update/install.rs index 4eee68916..602a67e5d 100644 --- a/crates/ui-egui/src/update/install.rs +++ b/crates/ui-egui/src/update/install.rs @@ -172,6 +172,19 @@ pub struct Net { pub feed: Option, } +/// GET `url` unless Cancel was pressed; check again after the response so a cancel during the +/// wait still ends the install without starting the package download (#8908). +fn get_unless_cancelled(net: &Net, progress: &Progress, url: &str) -> Result, FetchError> { + if progress.cancel.load(Ordering::Relaxed) { + return Err(FetchError::Failed("cancelled".into())); + } + let body = (net.get)(url)?; + if progress.cancel.load(Ordering::Relaxed) { + return Err(FetchError::Failed("cancelled".into())); + } + Ok(body) +} + /// Refresh the feed, then download, verify and apply the newest release for `method`, falling /// back to `release` when the check fails. Runs on a background thread. `staging` holds downloads /// that are not written next to their target (the `.msi`, the `.dmg`). @@ -184,6 +197,11 @@ pub fn run( staging: &Path, progress: &Progress, ) -> Result { + // Honour Cancel before each network wait: feed refresh and checksum fetches used to run + // without looking at the flag, so Cancel looked dead until the body download started (#8908). + if progress.cancel.load(Ordering::Relaxed) { + return Err("cancelled".into()); + } // The release shown may be stale by now: install the newest one the feed names, or the one // shown when the feed can't be read. let (release, fallback) = match super::fetch_latest(net.feed.as_deref(), Some(&net.get)) { @@ -200,7 +218,7 @@ pub fn run( return Err(format!("{} is too large ({} bytes)", asset.name, asset.size)); } let sums_url = release.assets.iter().find(|a| a.name == SUMS).map(|a| a.url.clone()).ok_or_else(|| format!("this release has no {SUMS}"))?; - let sums = (net.get)(&sums_url).map_err(|e| format!("{SUMS}: {e}"))?; + let sums = get_unless_cancelled(net, progress, &sums_url).map_err(|e| format!("{SUMS}: {e}"))?; if let Some(key) = PUBLIC_KEY { let sig_url = release .assets @@ -208,7 +226,7 @@ pub fn run( .find(|a| a.name == SUMS_SIG) .map(|a| a.url.clone()) .ok_or_else(|| format!("this release isn't signed (no {SUMS_SIG})"))?; - let sig = (net.get)(&sig_url).map_err(|e| format!("{SUMS_SIG}: {e}"))?; + let sig = get_unless_cancelled(net, progress, &sig_url).map_err(|e| format!("{SUMS_SIG}: {e}"))?; verify_signature(&sums, &String::from_utf8_lossy(&sig), key, &release.tag)?; } let sums = String::from_utf8_lossy(&sums); diff --git a/crates/ui-egui/src/update/install_tests.rs b/crates/ui-egui/src/update/install_tests.rs index 2fa03c85c..10df00cbf 100644 --- a/crates/ui-egui/src/update/install_tests.rs +++ b/crates/ui-egui/src/update/install_tests.rs @@ -129,12 +129,41 @@ fn an_appimage_is_replaced_only_when_it_verifies() { let bare = release_with(&[(name, new.len())]); assert!(run(&bare, &kind, &m, "x86_64", &n, &dir, &Progress::default()).unwrap_err().contains(SUMS)); - // Cancelled: nothing changes. + // Cancelled before any network work: nothing changes. let good = format!("{} {name}\n", hex(&new)); let n = net(vec![(name.into(), new.clone()), (SUMS.into(), good.clone().into_bytes())]); let p = Progress::default(); p.cancel.store(true, std::sync::atomic::Ordering::Relaxed); - assert!(run(&r, &kind, &m, "x86_64", &n, &dir, &p).is_err()); + let err = run(&r, &kind, &m, "x86_64", &n, &dir, &p).unwrap_err(); + assert!(err.contains("cancelled"), "{err}"); + assert_eq!(std::fs::read(&target).unwrap(), b"old version"); + + // Cancel during the checksum GET: package download never starts (#8908). + let progress = Arc::new(Progress::default()); + let flag = progress.clone(); + let downloaded = Arc::new(std::sync::atomic::AtomicBool::new(false)); + let hit = downloaded.clone(); + let files = Arc::new(vec![(name.into(), new.clone()), (SUMS.into(), good.clone().into_bytes())]); + let f2 = files.clone(); + let find = |files: &[(String, Vec)], url: &str| files.iter().find(|(n, _)| url == format!("{BASE}{n}")).map(|(_, b)| b.clone()); + let n = Net { + feed: None, + get: Arc::new(move |url: &str| { + let body = find(&files, url).ok_or_else(|| FetchError::Failed(format!("404 {url}")))?; + if url.ends_with(SUMS) { + flag.cancel.store(true, std::sync::atomic::Ordering::Relaxed); + } + Ok(body) + }), + download: Arc::new(move |url: &str, to: &Path, _| { + hit.store(true, std::sync::atomic::Ordering::Relaxed); + let body = find(&f2, url).ok_or_else(|| FetchError::Failed(format!("404 {url}")))?; + std::fs::write(to, body).map_err(|e| FetchError::Failed(e.to_string())) + }), + }; + let err = run(&r, &kind, &m, "x86_64", &n, &dir, &progress).unwrap_err(); + assert!(err.contains("cancelled"), "{err}"); + assert!(!downloaded.load(std::sync::atomic::Ordering::Relaxed), "package must not start after Cancel"); assert_eq!(std::fs::read(&target).unwrap(), b"old version"); // Verified: renamed over the old one, executable, restart offered. diff --git a/crates/ui-egui/src/update/tests.rs b/crates/ui-egui/src/update/tests.rs index e192fdaee..0301405b2 100644 --- a/crates/ui-egui/src/update/tests.rs +++ b/crates/ui-egui/src/update/tests.rs @@ -750,3 +750,48 @@ fn install_selection_refreshes_the_button_and_progress_before_completion() { assert!(h.app.updater.installed.is_none()); tx.send(()).unwrap(); } + +#[test] +fn cancel_download_leaves_downloading_immediately() { + // Cancel must clear the job from the UI at once so the card leaves "Downloading" without + // waiting for a stalled feed or checksum GET (#8908). + let dir = scratch(); + let target = dir.0.join("SolidCraft.AppImage"); + let name = asset_name(&InstallKind::AppImage(target.clone()), std::env::consts::ARCH, "999.1.0").unwrap(); + let mut h = crate::scenario::Harness::new(); + h.app.preferences.check_updates = false; + let mut shown = release(); + shown.tag = "v999.0.0".into(); + h.app.updater.release = Some(shown); + h.app.updater.kind = Some(InstallKind::AppImage(target)); + h.app.updater.open = true; + let (gate_tx, gate_rx) = std::sync::mpsc::channel(); + let gate_rx = std::sync::Mutex::new(gate_rx); + h.app.services.http_get = Some(Arc::new(move |url| { + if url == LATEST_API { + return Ok(json!({"tag_name":"v999.1.0", "assets":[ + {"name": name, "browser_download_url":format!("https://github.com/tangency-labs/solidcraft/releases/download/v999.1.0/{name}")}, + {"name": install::SUMS, "browser_download_url":"https://github.com/tangency-labs/solidcraft/releases/download/v999.1.0/SHA256SUMS.txt"} + ]}).to_string().into_bytes()); + } + // Stall checksum GET until the test ends so Cancel has to work before the body download. + let _ = gate_rx.lock().unwrap().recv(); + Ok(format!("{} {name}\n", "a".repeat(64)).into_bytes()) + })); + h.app.services.http_download = Some(Arc::new(|_, _, _| Err(FetchError::Failed("download must not start".into())))); + start_install(&mut h.app, &egui::Context::default()); + assert!(h.app.updater.job.is_some()); + h.frames(2); + let cancel = h.widget("Cancel").expect("Cancel while Downloading"); + h.call("ui.click", json!({"x": cancel[0], "y": cancel[1]})); + assert!(h.app.updater.job.is_none(), "Cancel clears the job immediately"); + assert!(h.app.updater.installed.is_none()); + assert!(h.app.updater.open, "Cancel leaves the Update window open on Offer"); + h.frames(2); + assert!( + h.widget("Remind me later").is_some() || h.widget("Skip this version").is_some() || h.widget("Update now").is_some(), + "Offer returns after Cancel" + ); + assert!(h.widget("Updating to 999.1.0.").is_none()); + let _ = gate_tx.send(()); +}