Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

refactor(profile): use standard library file locking

Donough Liu47512623315a
diff --git a/Cargo.lock b/Cargo.lock
index 17e46742d..8a7194f3d 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2446,11 +2446,9 @@ name = "moli-browser-profile"
version = "0.1.0"
dependencies = [
"anyhow",
- "libc",
"moli-cookie-cache",
"serde",
"serde_json",
- "windows-sys 0.61.2",
]
[[package]]
diff --git a/moli-browser-profile/Cargo.toml b/moli-browser-profile/Cargo.toml
index 5c417bfd0..a4ed15af7 100644
--- a/moli-browser-profile/Cargo.toml
+++ b/moli-browser-profile/Cargo.toml
@@ -6,13 +6,9 @@ edition = "2024"
[dependencies]
anyhow = "1.0.100"
-libc = "0.2"
moli-cookie-cache = { path = "../moli-cookie-cache" }
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0.145"
-[target.'cfg(windows)'.dependencies]
-windows-sys = { version = "0.61.2", features = ["Win32_Foundation", "Win32_Storage_FileSystem"] }
-
[lints]
workspace = true
diff --git a/moli-browser-profile/src/profile_lock.rs b/moli-browser-profile/src/profile_lock.rs
index 3a8a7408c..3ce688186 100644
--- a/moli-browser-profile/src/profile_lock.rs
+++ b/moli-browser-profile/src/profile_lock.rs
@@ -1,35 +1,18 @@
use std::{
- fs::{File, OpenOptions},
- io::{Read, Seek, SeekFrom, Write},
+ fs::{File, OpenOptions, TryLockError},
path::{Path, PathBuf},
- time::{SystemTime, UNIX_EPOCH},
-};
-
-#[cfg(unix)]
-use std::os::unix::io::AsRawFd;
-#[cfg(windows)]
-use std::os::windows::io::AsRawHandle;
-#[cfg(windows)]
-use windows_sys::Win32::{
- Foundation::{ERROR_LOCK_VIOLATION, HANDLE},
- Storage::FileSystem::{LockFile, UnlockFile},
};
use anyhow::{Context, Result, anyhow};
use crate::BrowserProfilePaths;
-const MAX_PROFILE_LOCK_METADATA_BYTES: usize = 1024;
-
#[derive(Debug)]
pub struct BrowserProfileLock {
path: PathBuf,
- /// Held on platforms with process-owned advisory locks. Keeping this
- /// handle alive keeps the profile exclusively owned; the OS releases the
- /// lock if the process exits without running Rust destructors.
- #[cfg(any(unix, windows))]
- file: File,
- remove_on_drop: bool,
+ /// Keeps the profile exclusively owned until this handle closes, including
+ /// when the process exits without running Rust destructors.
+ _file: File,
}
impl BrowserProfileLock {
@@ -42,40 +25,14 @@ impl BrowserProfileLock {
}
}
-impl Drop for BrowserProfileLock {
- fn drop(&mut self) {
- #[cfg(any(unix, windows))]
- let _ = unlock_profile_file(&self.file);
- if self.remove_on_drop {
- let _ = std::fs::remove_file(&self.path);
- }
- }
-}
-
pub fn acquire_profile_lock(paths: &BrowserProfilePaths) -> Result<BrowserProfileLock> {
if !paths.root.as_os_str().is_empty() {
std::fs::create_dir_all(&paths.root)
.with_context(|| format!("failed to create profile dir `{}`", paths.root.display()))?;
}
- let (mut file, remove_on_drop) = open_and_lock_profile_file(paths)?;
- if let Err(error) = write_lock_owner_metadata(&mut file, &paths.lock_path) {
- if remove_on_drop {
- let _ = std::fs::remove_file(&paths.lock_path);
- }
- return Err(error);
- }
-
- Ok(BrowserProfileLock {
- path: paths.lock_path.clone(),
- #[cfg(any(unix, windows))]
- file,
- remove_on_drop,
- })
-}
-
-#[cfg(unix)]
-fn open_and_lock_profile_file(paths: &BrowserProfilePaths) -> Result<(File, bool)> {
+ // Keep this file in place after release so contenders always lock the same
+ // file. Its contents do not participate in profile ownership.
let file = OpenOptions::new()
.read(true)
.write(true)
@@ -88,75 +45,17 @@ fn open_and_lock_profile_file(paths: &BrowserProfilePaths) -> Result<(File, bool
paths.lock_path.display()
)
})?;
- match try_lock_profile_file(&file) {
- Ok(()) => Ok((file, false)),
- Err(error) if is_advisory_lock_contention(&error) => {
- let owner = lock_owner_description(&paths.lock_path);
- Err(anyhow!(
- "browser profile `{}` is already locked by `{}` ({owner})",
- paths.root.display(),
- paths.lock_path.display()
- ))
- }
- Err(error) => Err(error).with_context(|| {
- format!(
- "failed to acquire browser profile lock `{}`",
- paths.lock_path.display()
- )
+ match file.try_lock() {
+ Ok(()) => Ok(BrowserProfileLock {
+ path: paths.lock_path.clone(),
+ _file: file,
}),
- }
-}
-
-#[cfg(windows)]
-fn open_and_lock_profile_file(paths: &BrowserProfilePaths) -> Result<(File, bool)> {
- let file = OpenOptions::new()
- .read(true)
- .write(true)
- .create(true)
- .truncate(false)
- .open(&paths.lock_path)
- .with_context(|| {
- format!(
- "failed to open browser profile lock `{}`",
- paths.lock_path.display()
- )
- })?;
- match try_lock_profile_file(&file) {
- Ok(()) => Ok((file, false)),
- Err(error) if is_advisory_lock_contention(&error) => {
- let owner = lock_owner_description(&paths.lock_path);
- Err(anyhow!(
- "browser profile `{}` is already locked by `{}` ({owner})",
- paths.root.display(),
- paths.lock_path.display()
- ))
- }
- Err(error) => Err(error).with_context(|| {
- format!(
- "failed to acquire browser profile lock `{}`",
- paths.lock_path.display()
- )
- }),
- }
-}
-
-#[cfg(not(any(unix, windows)))]
-fn open_and_lock_profile_file(paths: &BrowserProfilePaths) -> Result<(File, bool)> {
- match OpenOptions::new()
- .write(true)
- .create_new(true)
- .open(&paths.lock_path)
- {
- Ok(file) => Ok((file, true)),
- Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
- let owner = lock_owner_description(&paths.lock_path);
- Err(anyhow!(
- "browser profile `{}` is already locked by `{}` ({owner}); if no Moli process is using this profile, remove the stale lock file and retry",
- paths.root.display(),
- paths.lock_path.display()
- ))
- }
- Err(error) => Err(error).with_context(|| {
+ Err(TryLockError::WouldBlock) => Err(anyhow!(
+ "browser profile `{}` is already locked by `{}`",
+ paths.root.display(),
+ paths.lock_path.display()
+ )),
+ Err(TryLockError::Error(error)) => Err(error).with_context(|| {
format!(
"failed to acquire browser profile lock `{}`",
paths.lock_path.display()
@@ -165,165 +64,19 @@ fn open_and_lock_profile_file(paths: &BrowserProfilePaths) -> Result<(File, bool
}
}
-fn write_lock_owner_metadata(file: &mut File, path: &Path) -> Result<()> {
- file.set_len(0).with_context(|| {
- format!(
- "failed to truncate browser profile lock `{}`",
- path.display()
- )
- })?;
- file.seek(SeekFrom::Start(0))
- .with_context(|| format!("failed to seek browser profile lock `{}`", path.display()))?;
- let created_unix_ms = SystemTime::now()
- .duration_since(UNIX_EPOCH)
- .unwrap_or_default()
- .as_millis();
- writeln!(
- file,
- "pid={}\ncreated_unix_ms={created_unix_ms}",
- std::process::id()
- )
- .with_context(|| format!("failed to write browser profile lock `{}`", path.display()))?;
- file.flush()
- .with_context(|| format!("failed to flush browser profile lock `{}`", path.display()))?;
- Ok(())
-}
-
-#[cfg(unix)]
-fn try_lock_profile_file(file: &File) -> std::io::Result<()> {
- let rc = unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) };
- if rc == 0 {
- Ok(())
- } else {
- Err(std::io::Error::last_os_error())
- }
-}
-
-#[cfg(unix)]
-fn unlock_profile_file(file: &File) -> std::io::Result<()> {
- let rc = unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_UN) };
- if rc == 0 {
- Ok(())
- } else {
- Err(std::io::Error::last_os_error())
- }
-}
-
-#[cfg(unix)]
-fn is_advisory_lock_contention(error: &std::io::Error) -> bool {
- matches!(
- error.raw_os_error(),
- Some(code) if code == libc::EWOULDBLOCK || code == libc::EAGAIN
- )
-}
-
-#[cfg(windows)]
-const WINDOWS_PROFILE_LOCK_OFFSET: u64 = 1_u64 << 32;
-#[cfg(windows)]
-const WINDOWS_PROFILE_LOCK_LENGTH: u64 = 1;
-
-#[cfg(windows)]
-fn try_lock_profile_file(file: &File) -> std::io::Result<()> {
- let (offset_low, offset_high) = split_windows_u64(WINDOWS_PROFILE_LOCK_OFFSET);
- let (length_low, length_high) = split_windows_u64(WINDOWS_PROFILE_LOCK_LENGTH);
- // SAFETY: the handle is valid for the lifetime of `file`; the locked byte
- // lies beyond the bounded metadata prefix so other contenders can still
- // read owner diagnostics while the exclusive process lock is held.
- let result = unsafe {
- LockFile(
- file.as_raw_handle() as HANDLE,
- offset_low,
- offset_high,
- length_low,
- length_high,
- )
- };
- if result != 0 {
- Ok(())
- } else {
- Err(std::io::Error::last_os_error())
- }
-}
-
-#[cfg(windows)]
-fn unlock_profile_file(file: &File) -> std::io::Result<()> {
- let (offset_low, offset_high) = split_windows_u64(WINDOWS_PROFILE_LOCK_OFFSET);
- let (length_low, length_high) = split_windows_u64(WINDOWS_PROFILE_LOCK_LENGTH);
- // SAFETY: this uses the same valid handle and byte range as acquisition.
- let result = unsafe {
- UnlockFile(
- file.as_raw_handle() as HANDLE,
- offset_low,
- offset_high,
- length_low,
- length_high,
- )
- };
- if result != 0 {
- Ok(())
- } else {
- Err(std::io::Error::last_os_error())
- }
-}
-
-#[cfg(windows)]
-const fn split_windows_u64(value: u64) -> (u32, u32) {
- (value as u32, (value >> 32) as u32)
-}
-
-#[cfg(windows)]
-fn is_advisory_lock_contention(error: &std::io::Error) -> bool {
- error.raw_os_error() == Some(ERROR_LOCK_VIOLATION as i32)
-}
-
-fn lock_owner_description(path: &Path) -> String {
- let Ok(file) = File::open(path) else {
- return "lock owner metadata unavailable".to_owned();
- };
- let mut contents = String::new();
- let Ok(bytes_read) = file
- .take((MAX_PROFILE_LOCK_METADATA_BYTES + 1) as u64)
- .read_to_string(&mut contents)
- else {
- return "lock owner metadata unavailable".to_owned();
- };
- if bytes_read > MAX_PROFILE_LOCK_METADATA_BYTES {
- return "lock owner metadata unavailable".to_owned();
- }
- let mut fields = Vec::new();
- for line in contents.lines() {
- let line = line.trim();
- if line.is_empty() {
- continue;
- }
- if line.starts_with("pid=") || line.starts_with("created_unix_ms=") {
- fields.push(line.to_owned());
- }
- }
- if fields.is_empty() {
- "lock owner metadata unavailable".to_owned()
- } else {
- fields.join(", ")
- }
-}
-
#[cfg(test)]
mod tests {
use std::{
fs,
path::PathBuf,
- time::{SystemTime, UNIX_EPOCH},
- };
- #[cfg(windows)]
- use std::{
process::{Command, Stdio},
thread,
- time::Duration,
+ time::{Duration, SystemTime, UNIX_EPOCH},
};
use anyhow::Result;
- use super::{BrowserProfileLock, MAX_PROFILE_LOCK_METADATA_BYTES, lock_owner_description};
+ use super::BrowserProfileLock;
use crate::BrowserProfilePaths;
struct TempProfileDir {
@@ -356,44 +109,27 @@ mod tests {
let paths = BrowserProfilePaths::new(&profile.path);
let first = BrowserProfileLock::acquire(&paths)?;
- assert!(paths.lock_path.exists());
- let lock_contents = fs::read_to_string(&paths.lock_path)?;
- assert!(
- lock_contents.contains("pid="),
- "lock contents: {lock_contents}"
- );
+ assert_eq!(first.path(), paths.lock_path);
let error =
BrowserProfileLock::acquire(&paths).expect_err("second lock acquisition should fail");
let error = error.to_string();
assert!(error.contains("already locked"), "error: {error}");
- assert!(
- error.contains("pid=") && error.contains("created_unix_ms="),
- "error should include lock owner metadata: {error}"
- );
drop(first);
- #[cfg(any(unix, windows))]
assert!(
paths.lock_path.exists(),
- "advisory lock metadata file should remain reusable after drop"
- );
- #[cfg(not(any(unix, windows)))]
- assert!(
- !paths.lock_path.exists(),
- "sentinel lock file should be removed after drop"
+ "lock file should remain reusable after drop"
);
+ assert!(fs::read(&paths.lock_path)?.is_empty());
let _second = BrowserProfileLock::acquire(&paths)?;
Ok(())
}
- #[cfg(windows)]
const CHILD_PROFILE_ENV: &str = "MOLI_PROFILE_LOCK_TEST_CHILD_PROFILE";
- #[cfg(windows)]
const CHILD_READY_ENV: &str = "MOLI_PROFILE_LOCK_TEST_CHILD_READY";
- #[cfg(windows)]
#[test]
fn profile_lock_is_released_when_owner_process_is_terminated() -> Result<()> {
if let (Some(profile), Some(ready)) = (
@@ -436,46 +172,20 @@ mod tests {
}
let paths = BrowserProfilePaths::new(&profile.path);
- let error = BrowserProfileLock::acquire(&paths)
- .expect_err("live child must keep the profile exclusively locked");
- assert!(error.to_string().contains("already locked"));
- let child_pid = child.id();
-
+ let contention = BrowserProfileLock::acquire(&paths);
child.kill()?;
let _ = child.wait()?;
- let reopened = BrowserProfileLock::acquire(&paths)?;
- let metadata = fs::read_to_string(&paths.lock_path)?;
- assert!(metadata.contains(&format!("pid={}", std::process::id())));
- assert!(
- !metadata
- .lines()
- .any(|line| line == format!("pid={child_pid}")),
- "terminated owner metadata should be replaced: {metadata}"
- );
- drop(reopened);
+ let error = contention.expect_err("live child must keep the profile exclusively locked");
+ assert!(error.to_string().contains("already locked"));
assert!(paths.lock_path.exists());
- Ok(())
- }
- #[test]
- fn profile_lock_owner_metadata_read_is_bounded() -> Result<()> {
- let profile = TempProfileDir::new("bounded-owner-metadata");
- let paths = BrowserProfilePaths::new(&profile.path);
- fs::create_dir_all(&profile.path)?;
- fs::write(
- &paths.lock_path,
- "pid=1\ncreated_unix_ms=1\n".repeat(MAX_PROFILE_LOCK_METADATA_BYTES),
- )?;
-
- assert_eq!(
- lock_owner_description(&paths.lock_path),
- "lock owner metadata unavailable"
- );
+ let reopened = BrowserProfileLock::acquire(&paths)?;
+ drop(reopened);
+ assert!(fs::read(&paths.lock_path)?.is_empty());
Ok(())
}
- #[cfg(any(unix, windows))]
#[test]
fn profile_lock_reuses_existing_unlocked_lock_file() -> Result<()> {
let profile = TempProfileDir::new("stale-file");
@@ -484,17 +194,6 @@ mod tests {
fs::write(&paths.lock_path, "pid=1\ncreated_unix_ms=1\n")?;
let lock = BrowserProfileLock::acquire(&paths)?;
-
- let lock_contents = fs::read_to_string(&paths.lock_path)?;
- assert!(
- lock_contents.contains(&format!("pid={}", std::process::id())),
- "lock contents should be rewritten for the current owner: {lock_contents}"
- );
- assert!(
- !lock_contents.lines().any(|line| line == "pid=1"),
- "old owner metadata should not survive successful acquisition: {lock_contents}"
- );
-
drop(lock);
let _reopened = BrowserProfileLock::acquire(&paths)?;
Ok(())
diff --git a/moli/tests/profile_lock_cli.rs b/moli/tests/profile_lock_cli.rs
index cc37297ff..672db9889 100644
--- a/moli/tests/profile_lock_cli.rs
+++ b/moli/tests/profile_lock_cli.rs
@@ -83,8 +83,8 @@ fn cli_profile_dir_refuses_when_profile_lock_is_held() -> Result<()> {
"stderr start: {stderr} stderr end"
);
assert!(
- stderr.contains("pid=") && stderr.contains("created_unix_ms="),
- "stderr should include lock owner metadata: {stderr}"
+ stderr.contains(&profile_dir_arg),
+ "stderr should identify the locked profile: {stderr}"
);
Ok(())
}
@@ -114,8 +114,8 @@ fn cli_serve_profile_dir_refuses_when_profile_lock_is_held() -> Result<()> {
"stderr start: {stderr} stderr end"
);
assert!(
- stderr.contains("pid=") && stderr.contains("created_unix_ms="),
- "stderr should include lock owner metadata: {stderr}"
+ stderr.contains(&profile_dir_arg),
+ "stderr should identify the locked profile: {stderr}"
);
Ok(())
}