Skip to content

Reason Network: your team's coding agents, talking to each other. The engine behind `reason network`.

A lock is let go of when dropped, not when its file closes (#86)

Lars Talian0c61c9bbcec0
diff --git a/src/render.rs b/src/render.rs
index c00af18..e5c1d5a 100644
--- a/src/render.rs
+++ b/src/render.rs
@@ -529,10 +529,11 @@ mod tests {
] {
assert!(defang(fake).starts_with('['), "{fake:?} -> {:?}", defang(fake));
}
- // A body of brackets takes no longer than any other.
+ // A body of brackets takes no longer than any other: well under a second in a debug build, where judging each
+ // bracket on all that follows it takes minutes. The bound leaves room for a loaded machine.
let t = std::time::Instant::now();
assert!(defang(&"<".repeat(1 << 16)).starts_with('['));
- assert!(t.elapsed() < std::time::Duration::from_secs(2), "{:?}", t.elapsed());
+ assert!(t.elapsed() < std::time::Duration::from_secs(10), "{:?}", t.elapsed());
// Everything else stays as written.
for text in [
"Deploy is done \u{2014} see 2010\u{2013}2015",
diff --git a/src/util.rs b/src/util.rs
index ebf2941..c964468 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -160,12 +160,28 @@ pub fn tilde(path: &str) -> String {
/// Takes an exclusive lock on `path` (waiting for it if `wait`) and writes our pid into it. `None` means
/// someone else holds it.
-pub fn lock(path: &Path, wait: bool) -> std::io::Result<Option<File>> {
+pub fn lock(path: &Path, wait: bool) -> std::io::Result<Option<Lock>> {
use std::io::Write;
- let Some(mut f) = acquire(path, wait)? else { return Ok(None) };
- f.set_len(0)?;
- write!(f, "{}", std::process::id())?;
- Ok(Some(f))
+ let Some(file) = acquire(path, wait)? else { return Ok(None) };
+ let mut lock = Lock { file };
+ lock.file.set_len(0)?;
+ write!(lock.file, "{}", std::process::id())?;
+ Ok(Some(lock))
+}
+
+/// A lock from `lock`, held until dropped.
+pub struct Lock {
+ file: File,
+}
+
+/// Let go of on drop, not when the file closes: a process another thread is starting shares the file until it runs
+/// its program, and the lock with it.
+#[cfg(unix)]
+impl Drop for Lock {
+ fn drop(&mut self) {
+ use std::os::fd::AsRawFd;
+ unsafe { libc::flock(self.file.as_raw_fd(), libc::LOCK_UN) };
+ }
}
#[cfg(unix)]
@@ -217,8 +233,10 @@ fn acquire(path: &Path, wait: bool) -> std::io::Result<Option<File>> {
#[cfg(unix)]
pub fn locked(path: &Path) -> bool {
use std::os::fd::AsRawFd;
- let Ok(f) = File::open(path) else { return false };
- unsafe { libc::flock(f.as_raw_fd(), libc::LOCK_SH | libc::LOCK_NB) != 0 }
+ let Ok(file) = File::open(path) else { return false };
+ // As a `Lock`, so the shared lock we may take here lasts only this instant.
+ let f = Lock { file };
+ unsafe { libc::flock(f.file.as_raw_fd(), libc::LOCK_SH | libc::LOCK_NB) != 0 }
}
#[cfg(windows)]