From b4ce02395fb7fe17c900d3393474c6429299e614 Mon Sep 17 00:00:00 2001 From: Luis Araya <35828647+Totopo27@users.noreply.github.com> Date: Fri, 2 Oct 2026 10:22:49 -0600 Subject: [PATCH] fix(plugins): writable uninstall and bounded sidecar shutdown --- .../src/plugins/installer.rs | 74 +++++++++++++++++-- .../dbx-plugin-runtime/src/plugins/runtime.rs | 10 ++- 2 files changed, 77 insertions(+), 7 deletions(-) diff --git a/crates/dbx-plugin-runtime/src/plugins/installer.rs b/crates/dbx-plugin-runtime/src/plugins/installer.rs index 1cf98a53cc..8a90cbc918 100644 --- a/crates/dbx-plugin-runtime/src/plugins/installer.rs +++ b/crates/dbx-plugin-runtime/src/plugins/installer.rs @@ -468,8 +468,16 @@ impl PluginPackageInstaller { // and discovery does not see it any more. An antivirus scanner holding a freshly // extracted binary may fail this rename with error 5 / 32, hence the retry, and a // rename that keeps failing leaves the container untouched for a clean failure. - retry_transient_lock(delays, || rename(&plugin_dir, &tombstone)) - .map_err(|error| format!("Failed to uninstall plugin '{plugin_id}': {error}"))?; + retry_transient_lock(delays, || { + let res = rename(&plugin_dir, &tombstone); + if let Err(ref err) = res { + if cfg!(windows) && err.raw_os_error() == Some(5) { + ensure_tree_writable(&plugin_dir); + } + } + res + }) + .map_err(|error| format!("Failed to uninstall plugin '{plugin_id}': {error}"))?; } self.sweep_plugin_trash(delays, &mut remove_dir_all); Ok(()) @@ -498,6 +506,12 @@ impl PluginPackageInstaller { let removed = retry_transient_lock(delays, || match remove_dir_all(&tombstone) { // Already gone (or not a directory): nothing left to clean up. Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()), + Err(error) => { + if cfg!(windows) && error.raw_os_error() == Some(5) { + ensure_tree_writable(&tombstone); + } + Err(error) + } result => result, }); if let Err(error) = removed { @@ -1138,8 +1152,13 @@ fn open_install_lock(root_dir: &Path) -> Result { // Antivirus scanners briefly hold handles on freshly extracted plugin binaries, which fails // directory renames with ERROR_ACCESS_DENIED (5) or ERROR_SHARING_VIOLATION (32); the scan // normally finishes well inside this retry window. -const TRANSIENT_LOCK_RETRY_DELAYS: [Duration; 3] = - [Duration::from_millis(200), Duration::from_millis(400), Duration::from_millis(800)]; +const TRANSIENT_LOCK_RETRY_DELAYS: [Duration; 5] = [ + Duration::from_millis(200), + Duration::from_millis(400), + Duration::from_millis(800), + Duration::from_millis(1500), + Duration::from_millis(2000), +]; pub(crate) fn rename_with_transient_lock_retry(src: &Path, dst: &Path) -> std::io::Result<()> { retry_transient_lock(&TRANSIENT_LOCK_RETRY_DELAYS, || std::fs::rename(src, dst)) @@ -1152,7 +1171,11 @@ fn retry_transient_lock( for delay in delays { match operation() { Ok(value) => return Ok(value), - Err(error) if cfg!(windows) && is_windows_lock_error(&error) => std::thread::sleep(*delay), + Err(error) if cfg!(windows) && is_windows_lock_error(&error) => { + // If a file in the directory has the read-only attribute set on Windows, + // renames and removals fail with ERROR_ACCESS_DENIED (5). Ensure write permissions. + std::thread::sleep(*delay); + } Err(error) => return Err(error), } } @@ -1163,6 +1186,27 @@ fn is_windows_lock_error(error: &std::io::Error) -> bool { matches!(error.raw_os_error(), Some(5 | 32)) } +#[allow(clippy::permissions_set_readonly_false)] +fn ensure_tree_writable(path: &Path) { + if !path.exists() { + return; + } + if let Ok(metadata) = std::fs::metadata(path) { + let mut permissions = metadata.permissions(); + if permissions.readonly() { + permissions.set_readonly(false); + let _ = std::fs::set_permissions(path, permissions); + } + } + if path.is_dir() { + if let Ok(entries) = std::fs::read_dir(path) { + for entry in entries.flatten() { + ensure_tree_writable(&entry.path()); + } + } + } +} + /// Tombstone path for one logical uninstall: `.trash/`, or `.trash/-`, the /// same suffix convention `install_bytes_locked` uses for replaced versions, when an earlier /// tombstone of that plugin is still waiting for its physical delete. @@ -1294,7 +1338,7 @@ mod tests { use zip::write::SimpleFileOptions; use super::{ - is_activation_record_file, read_install_identity, retry_transient_lock, sha256_hex, + ensure_tree_writable, is_activation_record_file, read_install_identity, retry_transient_lock, sha256_hex, validate_package_expectation, write_json_atomically, PluginInstallPolicy, PluginPackageExpectation, PluginPackageInstaller, PluginSignatureStatus, PluginTrustStore, ACTIVATIONS_DIR, INSTALL_LOCK_FILE, PLUGIN_CHECKSUMS_FILE, PLUGIN_SIGNATURE_FILE, PLUGIN_TRASH_DIR, VERSIONS_DIR, @@ -2368,4 +2412,22 @@ mod tests { installer.install_bytes(&package("1.0.0", None, false), PluginInstallPolicy::LocalDevelopment).unwrap(); assert_eq!(registry.list_installed().unwrap().len(), 1); } + + #[test] + fn ensure_tree_writable_clears_readonly_flags_recursively() { + let temp = tempfile::tempdir().unwrap(); + let dir = temp.path().join("readonly_dir"); + let sub = dir.join("nested"); + std::fs::create_dir_all(&sub).unwrap(); + let file = sub.join("binary.exe"); + std::fs::write(&file, b"test").unwrap(); + + let mut perms = std::fs::metadata(&file).unwrap().permissions(); + perms.set_readonly(true); + std::fs::set_permissions(&file, perms).unwrap(); + assert!(std::fs::metadata(&file).unwrap().permissions().readonly()); + + ensure_tree_writable(&dir); + assert!(!std::fs::metadata(&file).unwrap().permissions().readonly()); + } } diff --git a/crates/dbx-plugin-runtime/src/plugins/runtime.rs b/crates/dbx-plugin-runtime/src/plugins/runtime.rs index 2c43301f49..b6c4bedbd5 100644 --- a/crates/dbx-plugin-runtime/src/plugins/runtime.rs +++ b/crates/dbx-plugin-runtime/src/plugins/runtime.rs @@ -358,7 +358,15 @@ impl PluginSidecarSession { self.status.send_replace(PluginSessionStatus::new(PluginSessionState::Stopping, None)); // Let any open user prompt resolve and close its dialog. self.prompts.close(); - let kill_result = self.child.lock().await.kill().await; + let mut child = self.child.lock().await; + let kill_result = child.kill().await; + // tokio's kill() is start_kill() + wait(), so a successful kill has + // already reaped the child; this bounded wait only covers the rare + // kill-failure path where the process is still alive. The OS releases + // the executable image lock when the session (holding kill_on_drop + // Child) drops, which uninstall performs before renaming the plugin + // directory (fixes os error 5 / access denied on uninstall). + let _ = tokio::time::timeout(Duration::from_secs(5), child.wait()).await; let message = kill_result.err().map(|error| error.to_string()); fail_pending(&self.pending, "Plugin session stopped").await; self.status.send_replace(PluginSessionStatus::new(PluginSessionState::Stopped, message));