Skip to content

Commit

Permalink
Fix #1870: Race condition with some virus scanners
Browse files Browse the repository at this point in the history
Some virus scanners take out handles on files we are going to
rename, causing access errors and preventing the rename. Typically
this is short lived. Retry rather than erroring.

No feedback given at the moment, and depends on an unreleased
retry, so WIP...
  • Loading branch information
rbtcollins committed May 25, 2019
1 parent d58e5c8 commit fdd7e74
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Cargo.toml
Expand Up @@ -55,6 +55,10 @@ wait-timeout = "0.2"
walkdir = "2"
xz2 = "0.1.3"

[dependencies.retry]
version = "0.4"
default-features = false

[target."cfg(windows)".dependencies]
cc = "1"
winreg = "0.6"
Expand Down Expand Up @@ -100,3 +104,6 @@ path = "src/cli/main.rs"
[profile.release]
lto = true
codegen-units = 1

[patch.crates-io]
retry = { path = "../retry" }
22 changes: 21 additions & 1 deletion src/utils/utils.rs
Expand Up @@ -11,6 +11,9 @@ use std::path::{Path, PathBuf};
use std::process::Command;
use url::Url;

use retry::{retry, OperationResult};
use retry::delay::{Fibonacci, jitter};

#[cfg(windows)]
use winapi::shared::minwindef::DWORD;

Expand Down Expand Up @@ -651,7 +654,24 @@ pub fn toolchain_sort<T: AsRef<str>>(v: &mut Vec<T>) {
}

fn rename(name: &'static str, src: &Path, dest: &Path) -> Result<()> {
fs::rename(src, dest).chain_err(|| ErrorKind::RenamingFile {
// https://github.com/rust-lang/rustup.rs/issues/1870
// 21 fib steps from 1 sums to ~28 seconds, hopefully more than enough
// for our previous poor performance that avoided the race condition with
// McAfee and Norton.
retry(
Fibonacci::from_millis(1).map(jitter).take(21), || {
match fs::rename(src, dest) {
Ok(v) => OperationResult::Ok(v),
Err(e) => {
match e.kind() {
io::ErrorKind::PermissionDenied => OperationResult::Retry(e),
_ => OperationResult::Err(e)
}
}
}
}
)
.chain_err(|| ErrorKind::RenamingFile {
name,
src: PathBuf::from(src),
dest: PathBuf::from(dest),
Expand Down

0 comments on commit fdd7e74

Please sign in to comment.