Skip to content

Commit 2f3da8d

Browse files
Copilotfengmk2
andcommitted
Fix race condition by removing directory deletion before rename
Remove the remove_dir_all_force call before rename to avoid race condition where: 1. Thread A removes target_dir 2. Thread B removes target_dir (ignored due to NotFound) 3. Thread B renames tmp to target_dir 4. Thread A tries to rename and gets AlreadyExists Now we just attempt the rename directly, which is more atomic. If it fails with AlreadyExists, we know another thread succeeded and we handle it gracefully by validating/fixing the installation. Co-authored-by: fengmk2 <156269+fengmk2@users.noreply.github.com>
1 parent b537b34 commit 2f3da8d

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

crates/vite_install/src/package_manager.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,11 +381,11 @@ async fn download_package_manager(
381381
}
382382

383383
// rename $target_dir_tmp to $target_dir
384+
// We don't remove target_dir first to avoid race conditions where another thread
385+
// might be in the middle of creating it. Instead, we rely on the rename operation
386+
// to fail if the directory already exists, which we handle gracefully.
384387
tracing::debug!("Rename {:?} to {:?}", target_dir_tmp, target_dir);
385-
remove_dir_all_force(&target_dir).await?;
386388

387-
// Handle potential race condition where another concurrent process may have
388-
// already created the target directory
389389
match tokio::fs::rename(&target_dir_tmp, &target_dir).await {
390390
Ok(()) => {
391391
// create shim file

0 commit comments

Comments
 (0)