Log when FileLock::drop fails to unlock file (#2064)

* Add optional dependency tracing to fs-lock

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Add optional logging to FileLock::drop

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Enable fs-lock/tracing in binstalk-manifest

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Add FileLock::set_file_path

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Update create_if_not_exist to return FileLock on successs

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Update create_if_not_exist usage in mod binstall_crates_v1

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Simplify create_if_not_exist

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Refactor mod crates_manifests to use create_if_not_exist

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Set file path for FileLock

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Set file path for file lock in mod cargo_config

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Fix fs-lock impl

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Import Path in fs-lock lib.rs

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Fix fs-lock lib.rs

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Fix fs-lock lib.rs

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Fix typo in crates_manifests.rs

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Fix calling create_if_not_exist in crates_manifests.rs

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Fox fmt crates_manifests.rs

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Fix fmt in lib.rs

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

---------

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>
This commit is contained in:
Jiahao XU 2025-02-23 01:33:36 +11:00 committed by GitHub
parent 86a7e90175
commit 8ee6c537e4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 69 additions and 28 deletions

View file

@ -11,3 +11,4 @@ license = "Apache-2.0 OR MIT"
[dependencies]
fs4 = "0.12.0"
tracing = { version = "0.1", optional = true }

View file

@ -6,22 +6,33 @@ use std::{
fs::File,
io::{self, IoSlice, IoSliceMut, SeekFrom},
ops,
path::Path,
};
use fs4::fs_std::FileExt;
/// A locked file.
#[derive(Debug)]
pub struct FileLock(File);
pub struct FileLock(File, #[cfg(feature = "tracing")] Option<Box<Path>>);
impl FileLock {
#[cfg(not(feature = "tracing"))]
fn new(file: File) -> Self {
Self(file)
}
#[cfg(feature = "tracing")]
fn new(file: File) -> Self {
Self(file, None)
}
/// Take an exclusive lock on a [`File`].
///
/// Note that this operation is blocking, and should not be called in async contexts.
pub fn new_exclusive(file: File) -> io::Result<Self> {
FileExt::lock_exclusive(&file)?;
Ok(Self(file))
Ok(Self::new(file))
}
/// Try to take an exclusive lock on a [`File`].
@ -33,7 +44,7 @@ impl FileLock {
/// Note that this operation is blocking, and should not be called in async contexts.
pub fn new_try_exclusive(file: File) -> Result<Self, (File, Option<io::Error>)> {
match FileExt::try_lock_exclusive(&file) {
Ok(()) => Ok(Self(file)),
Ok(()) => Ok(Self::new(file)),
Err(e) if e.raw_os_error() == fs4::lock_contended_error().raw_os_error() => {
Err((file, None))
}
@ -47,7 +58,7 @@ impl FileLock {
pub fn new_shared(file: File) -> io::Result<Self> {
FileExt::lock_shared(&file)?;
Ok(Self(file))
Ok(Self::new(file))
}
/// Try to take a shared lock on a [`File`].
@ -59,18 +70,47 @@ impl FileLock {
/// Note that this operation is blocking, and should not be called in async contexts.
pub fn new_try_shared(file: File) -> Result<Self, (File, Option<io::Error>)> {
match FileExt::try_lock_shared(&file) {
Ok(()) => Ok(Self(file)),
Ok(()) => Ok(Self::new(file)),
Err(e) if e.raw_os_error() == fs4::lock_contended_error().raw_os_error() => {
Err((file, None))
}
Err(e) => Err((file, Some(e))),
}
}
/// Set path to the file for logging on unlock error, if feature tracing is enabled
pub fn set_file_path(mut self, path: impl Into<Box<Path>>) -> Self {
#[cfg(feature = "tracing")]
{
self.1 = Some(path.into());
}
self
}
}
impl Drop for FileLock {
fn drop(&mut self) {
let _ = FileExt::unlock(&self.0);
let _res = FileExt::unlock(&self.0);
#[cfg(feature = "tracing")]
if let Err(err) = _res {
use std::fmt;
struct OptionalPath<'a>(Option<&'a Path>);
impl fmt::Display for OptionalPath<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(path) = self.0 {
fmt::Display::fmt(&path.display(), f)
} else {
Ok(())
}
}
}
tracing::warn!(
"Failed to unlock file{}: {err}",
OptionalPath(self.1.as_deref()),
);
}
}
}