Skip to content

Craft Anything: understand how any software works, and craft your own.

Rust

Wait for browser shutdown before cleaning private profiles (#827)

Agapenorfc93bd091e78
diff --git a/crates/engine/src/tools/web_common.rs b/crates/engine/src/tools/web_common.rs
index 7606fb2d..8c0502f9 100644
--- a/crates/engine/src/tools/web_common.rs
+++ b/crates/engine/src/tools/web_common.rs
@@ -58,11 +58,21 @@ pub struct Browser {
struct BrowserProfile(std::path::PathBuf);
+fn remove_profile_with(mut remove: impl FnMut() -> std::io::Result<()>) -> std::io::Result<()> {
+ let started = std::time::Instant::now();
+ loop {
+ match remove() {
+ Ok(()) => return Ok(()),
+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()),
+ Err(e) if started.elapsed() >= Duration::from_secs(5) => return Err(e),
+ Err(_) => std::thread::sleep(Duration::from_millis(50)),
+ }
+ }
+}
+
impl Drop for BrowserProfile {
fn drop(&mut self) {
- if let Err(e) = std::fs::remove_dir_all(&self.0)
- && e.kind() != std::io::ErrorKind::NotFound
- {
+ if let Err(e) = remove_profile_with(|| std::fs::remove_dir_all(&self.0)) {
log::warn!("couldn't remove the private browser profile {}: {e}", self.0.display());
}
}
@@ -1081,6 +1091,21 @@ mod tests {
assert!(!profile.exists(), "the private browser profile survived its owner");
}
+ #[test]
+ fn profile_cleanup_retries_while_the_browser_releases_files() {
+ let root = tempfile::tempdir().unwrap();
+ let profile = root.path().join("private-profile");
+ std::fs::create_dir(&profile).unwrap();
+ let mut attempts = 0;
+ super::remove_profile_with(|| {
+ attempts += 1;
+ if attempts < 3 { Err(std::io::Error::new(std::io::ErrorKind::PermissionDenied, "still locked")) } else { std::fs::remove_dir_all(&profile) }
+ })
+ .unwrap();
+ assert_eq!(attempts, 3);
+ assert!(!profile.exists());
+ }
+
/// #747: a Flatpak or Snap browser keeps its profile where its sandbox can reach it and we can
/// read its port; any other browser uses the temp folder.
#[test]
diff --git a/crates/platform/src/process.rs b/crates/platform/src/process.rs
index 70501180..c297e05b 100644
--- a/crates/platform/src/process.rs
+++ b/crates/platform/src/process.rs
@@ -758,7 +758,7 @@ impl Supervisor {
let _ = self.child.kill();
} else {
// The leader exited; take any leftover group members with it so they can't hold the pipes.
- let _ = self.child.start_kill();
+ let _ = self.child.kill();
}
self.drain();
let duration = self.start.elapsed();