-
Notifications
You must be signed in to change notification settings - Fork 855
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
Avoid deadlocks when multiple uv processes lock resources #6790
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -314,8 +314,8 @@ pub fn is_temporary(path: impl AsRef<Path>) -> bool { | |
pub struct LockedFile(fs_err::File); | ||
|
||
impl LockedFile { | ||
pub fn acquire(path: impl AsRef<Path>, resource: impl Display) -> Result<Self, std::io::Error> { | ||
let file = fs_err::File::create(path.as_ref())?; | ||
/// Inner implementation for [`LockedFile::acquire_blocking`] and [`LockedFile::acquire`]. | ||
fn lock_file_blocking(file: fs_err::File, resource: &str) -> Result<Self, std::io::Error> { | ||
trace!("Checking lock for `{resource}`"); | ||
match file.file().try_lock_exclusive() { | ||
Ok(()) => { | ||
|
@@ -328,19 +328,42 @@ impl LockedFile { | |
warn_user!( | ||
"Waiting to acquire lock for {} (lockfile: {})", | ||
resource, | ||
path.user_display(), | ||
file.path().user_display(), | ||
); | ||
file.file().lock_exclusive().map_err(|err| { | ||
// Not an fs_err method, we need to build our own path context | ||
std::io::Error::new( | ||
std::io::ErrorKind::Other, | ||
format!("Could not lock {}: {}", path.as_ref().user_display(), err), | ||
format!("Could not lock {}: {}", file.path().user_display(), err), | ||
) | ||
})?; | ||
Ok(Self(file)) | ||
} | ||
} | ||
} | ||
|
||
/// The same as [`LockedFile::acquire`], but for synchronous contexts. Do not use from an async | ||
/// context, as this can block the runtime while waiting for another process to release the | ||
/// lock. | ||
pub fn acquire_blocking( | ||
path: impl AsRef<Path>, | ||
resource: impl Display, | ||
) -> Result<Self, std::io::Error> { | ||
let file = fs_err::File::create(path.as_ref())?; | ||
let resource = resource.to_string(); | ||
Self::lock_file_blocking(file, &resource) | ||
} | ||
|
||
/// Acquire a cross-process lock for a resource using a file at the provided path. | ||
#[cfg(feature = "tokio")] | ||
pub async fn acquire( | ||
path: impl AsRef<Path>, | ||
resource: impl Display, | ||
) -> Result<Self, std::io::Error> { | ||
let file = fs_err::File::create(path.as_ref())?; | ||
let resource = resource.to_string(); | ||
Comment on lines
+363
to
+364
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need to do this for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's ok, I believe it needs to have a static lifetime (or be owned) for this... |
||
tokio::task::spawn_blocking(move || Self::lock_file_blocking(file, &resource)).await? | ||
} | ||
} | ||
|
||
impl Drop for LockedFile { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could remove this entirely too.