Skip to content

incr. comp.: Let compiler retry finalizing session directory a few times. #95304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions compiler/rustc_incremental/src/persist/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ use rustc_fs_util::{link_or_copy, LinkOrCopy};
use rustc_session::{Session, StableCrateId};

use std::fs as std_fs;
use std::io;
use std::io::{self, ErrorKind};
use std::mem;
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
Expand Down Expand Up @@ -371,7 +371,7 @@ pub fn finalize_session_directory(sess: &Session, svh: Svh) {
let new_path = incr_comp_session_dir.parent().unwrap().join(new_sub_dir_name);
debug!("finalize_session_directory() - new path: {}", new_path.display());

match std_fs::rename(&*incr_comp_session_dir, &new_path) {
match rename_path_with_retry(&*incr_comp_session_dir, &new_path, 3) {
Ok(_) => {
debug!("finalize_session_directory() - directory renamed successfully");

Expand Down Expand Up @@ -961,3 +961,24 @@ fn safe_remove_file(p: &Path) -> io::Result<()> {
result => result,
}
}

// On Windows the compiler would sometimes fail to rename the session directory because
// the OS thought something was still being accessed in it. So we retry a few times to give
// the OS time to catch up.
// See https://github.com/rust-lang/rust/issues/86929.
fn rename_path_with_retry(from: &Path, to: &Path, mut retries_left: usize) -> std::io::Result<()> {
loop {
match std_fs::rename(from, to) {
Ok(()) => return Ok(()),
Err(e) => {
if retries_left > 0 && e.kind() == ErrorKind::PermissionDenied {
// Try again after a short waiting period.
std::thread::sleep(Duration::from_millis(50));
retries_left -= 1;
} else {
return Err(e);
}
}
}
}
}