Launcher: a version removal retries a locked file and leaves the UI thread

remove_dir_all stops at the first file another process still holds and leaves
the rest deleted, so a removal that worked reported os error 5. Retry five times
250ms apart and treat a missing folder as done. The window froze because the
deletion ran from the update loop; it now runs on a thread and reports back
with Msg::Removed, as install already did.
This commit is contained in:
Alexander Krylov
2026-10-25 03:16:56 +03:00
parent 75d70a1a74
commit a93c1507f9
2 changed files with 46 additions and 8 deletions
+22 -1
View File
@@ -558,7 +558,26 @@ pub fn install(release: &Release, progress: impl Fn(f32)) -> Result<(), String>
}
pub fn remove(tag: &str) -> Result<(), String> {
fs::remove_dir_all(version_dir(tag)).map_err(|e| e.to_string())
let dir = version_dir(tag);
// Windows keeps a handle on a file for a moment after the game, a scan or the launcher's own
// reader lets go of it, and remove_dir_all stops at the first one: a removal that already
// took most of the folder reported os error 5 while the rest of it did go. Retry, and treat a
// folder that is already gone as done.
let mut last = None;
for attempt in 0..5 {
match fs::remove_dir_all(&dir) {
Ok(()) => return Ok(()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()),
Err(e) => last = Some(e),
}
if attempt < 4 {
std::thread::sleep(std::time::Duration::from_millis(250));
}
}
Err(match last {
Some(e) => e.to_string(),
None => format!("could not remove {}", dir.display()),
})
}
/// The Minecraft version a Horizon build targets, from its research-lock.json.
@@ -2170,6 +2189,8 @@ pub enum Msg {
Links(Links),
Progress(String, f32),
Installed(String, Result<(), String>),
/// A removal finished off the UI thread.
Removed(String, Result<(), String>),
Line(Job, String),
Exit(Job, Result<(), String>),
Updates(Result<Updates, String>),
+24 -7
View File
@@ -144,6 +144,9 @@ struct Launcher {
releases: Load<Vec<Release>>,
installed: Vec<String>,
installing: Option<(String, f32)>,
/// The version being deleted: the removal runs off the UI thread, so the window has to know
/// which row is busy until `Msg::Removed` arrives.
removing: Option<String>,
install_error: Option<(String, String)>,
confirm_remove: Option<String>,
picker_open: bool,
@@ -286,6 +289,7 @@ impl Launcher {
releases: Load::Loading,
installed,
installing: None,
removing: None,
install_error: None,
confirm_remove: None,
picker_open: false,
@@ -516,6 +520,16 @@ impl Launcher {
self.bar = (self.bar.1, p);
self.installing = Some((tag, p));
}
Msg::Removed(tag, result) => {
self.removing = None;
self.installed = backend::installed();
self.settings.selected = Settings::load()
.selected
.or_else(|| self.installed.first().cloned());
if let Err(e) = result {
self.install_error = Some((tag, e));
}
}
Msg::Installed(tag, result) => {
self.installing = None;
self.bar = (0., 0.);
@@ -725,14 +739,16 @@ impl Launcher {
}
fn remove(&mut self, tag: &str) {
if let Err(e) = backend::remove_version(tag) {
self.install_error = Some((tag.to_string(), e));
}
// Same shape as install: the folder is hundreds of megabytes and Windows may hold a file
// for a moment, so the deletion goes to a thread instead of stalling the window.
let (tag, tx) = (tag.to_string(), self.tx.clone());
self.removing = Some(tag.clone());
self.confirm_remove = None;
self.installed = backend::installed();
self.settings.selected = Settings::load()
.selected
.or_else(|| self.installed.first().cloned());
self.install_error = None;
std::thread::spawn(move || {
let result = backend::remove_version(&tag);
let _ = tx.send(Msg::Removed(tag, result));
});
}
fn prepare(&mut self) {
@@ -2921,6 +2937,7 @@ impl Launcher {
let keep = |i: usize| {
let tag = &releases[i].tag;
self.installing.as_ref().is_some_and(|(t, _)| t == tag)
|| self.removing.as_deref() == Some(tag.as_str())
|| self.install_error.as_ref().is_some_and(|(t, _)| t == tag)
|| self.confirm_remove.as_ref() == Some(tag)
};