Skip to content

Commit b009db0

Browse files
committed
Merge branch 'gix-lock-improvements'
2 parents 14e0763 + ae29826 commit b009db0

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

Diff for: gix-lock/src/acquire.rs

+22
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ impl fmt::Display for Fail {
3030
}
3131
}
3232

33+
impl From<Duration> for Fail {
34+
fn from(value: Duration) -> Self {
35+
if value.is_zero() {
36+
Fail::Immediately
37+
} else {
38+
Fail::AfterDurationWithBackoff(value)
39+
}
40+
}
41+
}
42+
3343
/// The error returned when acquiring a [`File`] or [`Marker`].
3444
#[derive(Debug, thiserror::Error)]
3545
#[allow(missing_docs)]
@@ -49,6 +59,12 @@ impl File {
4959
///
5060
/// If `boundary_directory` is given, non-existing directories will be created automatically and removed in the case of
5161
/// a rollback. Otherwise the containing directory is expected to exist, even though the resource doesn't have to.
62+
///
63+
/// ### Warning of potential resource leak
64+
///
65+
/// Please note that the underlying file will remain if destructors don't run, as is the case when interrupting the application.
66+
/// This results in the resource being locked permanently unless the lock file is removed by other means.
67+
/// See [the crate documentation](crate) for more information.
5268
pub fn acquire_to_update_resource(
5369
at_path: impl AsRef<Path>,
5470
mode: Fail,
@@ -70,6 +86,12 @@ impl Marker {
7086
///
7187
/// If `boundary_directory` is given, non-existing directories will be created automatically and removed in the case of
7288
/// a rollback.
89+
///
90+
/// ### Warning of potential resource leak
91+
///
92+
/// Please note that the underlying file will remain if destructors don't run, as is the case when interrupting the application.
93+
/// This results in the resource being locked permanently unless the lock file is removed by other means.
94+
/// See [the crate documentation](crate) for more information.
7395
pub fn acquire_to_hold_resource(
7496
at_path: impl AsRef<Path>,
7597
mode: Fail,

Diff for: gix-lock/src/lib.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
//! git-style registered lock files to make altering resources atomic.
22
//!
3-
//! In this model, reads are always atomic and can be performed directly while writes are facilitated by a locking mechanism
4-
//! implemented here.
3+
//! In this model, reads are always atomic and can be performed directly while writes are facilitated by the locking mechanism
4+
//! implemented here. Locks are acquired atomically, then written to, to finally atomically overwrite the actual resource.
55
//!
6-
//! Lock files mostly `gix-tempfile` with its auto-cleanup and the following:
6+
//! Lock files are wrapped [`gix-tempfile`](gix_tempfile)-handles and add the following:
77
//!
88
//! * consistent naming of lock files
99
//! * block the thread (with timeout) or fail immediately if a lock cannot be obtained right away
1010
//! * commit lock files to atomically put them into the location of the originally locked file
1111
//!
1212
//! # Limitations
1313
//!
14+
//! * [All limitations of `gix-tempfile`](gix_tempfile) apply. **A highlight of such a limitation is resource leakage
15+
//! which results in them being permanently locked unless there is user-intervention.**
1416
//! * As the lock file is separate from the actual resource, locking is merely a convention rather than being enforced.
15-
//! * The limitations of `gix-tempfile` apply.
1617
#![deny(missing_docs, rust_2018_idioms, unsafe_code)]
1718

19+
use gix_tempfile::handle::{Closed, Writable};
1820
use std::path::PathBuf;
1921

2022
pub use gix_tempfile as tempfile;
21-
use gix_tempfile::handle::{Closed, Writable};
2223

2324
const DOT_LOCK_SUFFIX: &str = ".lock";
2425

Diff for: gix-tempfile/src/handle.rs

+15
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ impl Handle<Closed> {
6969
///
7070
/// Depending on the `directory` configuration, intermediate directories will be created, and depending on `cleanup` empty
7171
/// intermediate directories will be removed.
72+
///
73+
/// ### Warning of potential leaks
74+
///
75+
/// Without [signal handlers](crate::signal) installed, tempfiles will remain once a termination
76+
/// signal is encountered as destructors won't run. See [the top-level documentation](crate) for more.
7277
pub fn at(path: impl AsRef<Path>, directory: ContainingDirectory, cleanup: AutoRemove) -> io::Result<Self> {
7378
Ok(Handle {
7479
id: Handle::<()>::at_path(path.as_ref(), directory, cleanup, Mode::Closed)?,
@@ -92,6 +97,11 @@ impl Handle<Writable> {
9297
///
9398
/// Depending on the `directory` configuration, intermediate directories will be created, and depending on `cleanup` empty
9499
/// intermediate directories will be removed.
100+
///
101+
/// ### Warning of potential leaks
102+
///
103+
/// Without [signal handlers](crate::signal) installed, tempfiles will remain once a termination
104+
/// signal is encountered as destructors won't run. See [the top-level documentation](crate) for more.
95105
pub fn at(path: impl AsRef<Path>, directory: ContainingDirectory, cleanup: AutoRemove) -> io::Result<Self> {
96106
Ok(Handle {
97107
id: Handle::<()>::at_path(path.as_ref(), directory, cleanup, Mode::Writable)?,
@@ -102,6 +112,11 @@ impl Handle<Writable> {
102112
/// Create a registered tempfile within `containing_directory` with a name that won't clash, and clean it up as specified with `cleanup`.
103113
/// Control how to deal with intermediate directories with `directory`.
104114
/// The temporary file is opened and can be written to using the [`with_mut()`][Handle::with_mut()] method.
115+
///
116+
/// ### Warning of potential leaks
117+
///
118+
/// Without [signal handlers](crate::signal) installed, tempfiles will remain once a termination
119+
/// signal is encountered as destructors won't run. See [the top-level documentation](crate) for more.
105120
pub fn new(
106121
containing_directory: impl AsRef<Path>,
107122
directory: ContainingDirectory,

Diff for: gix-tempfile/src/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
//!
1010
//! ### Initial Setup
1111
//!
12-
//! As no handlers for `TERMination` are installed, it is required to call [`signal::setup()`] before creating the first tempfile.
13-
//! This also allows to control how `git-tempfiles` integrates with other handlers under application control.
12+
//! As no handlers for `TERMination` are installed, it is required to call [`signal::setup()`] before creating
13+
//! the first tempfile. This also allows to control how this crate integrates with
14+
//! other handlers under application control.
1415
//!
1516
//! As a general rule of thumb, use `Default::default()` as argument to emulate the default behaviour and
1617
//! abort the process after cleaning temporary files. Read more about options in [`signal::handler::Mode`].
@@ -23,7 +24,7 @@
2324
//! * The application is performing a write operation on the tempfile when a signal arrives, preventing this tempfile to be removed,
2425
//! but not others. Any other operation dealing with the tempfile suffers from the same issue.
2526
//!
26-
//! [signal-hook]: https://docs.rs/signal-hook
27+
//! [`signal-hook`]: https://docs.rs/signal-hook
2728
//!
2829
//! ## Feature Flags
2930
#![cfg_attr(

0 commit comments

Comments
 (0)