Skip to content

Commit 9309af4

Browse files
authored
Fix cache directory lock (#157)
After [fs4 update](#152), it looks like it started returning `Ok(false)` if it couldn't acquire the lock. This made protofetch ignore the locks completely. This PR makes it handle the `Ok(false)` response correctly.
1 parent 8ba5bf2 commit 9309af4

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/flock.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,26 @@ impl FileLock {
2121
let file = File::create(path)?;
2222
let start = Instant::now();
2323
loop {
24-
match file.try_lock_exclusive() {
25-
Ok(_) => {
24+
match file.try_lock_exclusive().or_else(|error| {
25+
if error.raw_os_error() == fs4::lock_contended_error().raw_os_error() {
26+
Ok(false)
27+
} else {
28+
Err(error)
29+
}
30+
}) {
31+
Ok(true) => {
2632
return Ok(Self { _file: file });
2733
}
28-
Err(error)
29-
if error.raw_os_error() == fs4::lock_contended_error().raw_os_error()
30-
&& start.elapsed().as_secs() < 300 =>
31-
{
34+
Ok(false) if start.elapsed().as_secs() < 300 => {
3235
debug!("Failed to acquire a lock on {}, retrying", path.display());
3336
std::thread::sleep(Duration::from_secs(1));
3437
}
38+
Ok(false) => {
39+
return Err(Error(std::io::Error::other(format!(
40+
"Failed to acquire a lock on {}",
41+
path.display()
42+
))))
43+
}
3544
Err(error) => return Err(error.into()),
3645
}
3746
}

0 commit comments

Comments
 (0)