Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "selenium-manager"
version = "0.4.52-nightly" # don't forget to update rust/BUILD.bazel
edition = "2024"
rust-version = "1.89"
authors = ["Selenium <selenium-developers@googlegroups.com"]
license = "Apache-2.0"
homepage = "https://www.selenium.dev/"
Expand Down Expand Up @@ -42,7 +43,6 @@ debpkg = "0.6.0"
anyhow = { version = "1.0.104", default-features = false, features = ["backtrace", "std"] }
apple-flat-package = "0.20.0"
which = "8.0.6"
fs2 = "0.4.3"
fs_extra = "1.3.0"

[target.'cfg(windows)'.dependencies]
Expand Down
2 changes: 2 additions & 0 deletions rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Selenium Manager has been implemented as a CLI (Command-Line Interface) tool usi
curl https://sh.rustup.rs -sSf | sh
```

The minimum supported Rust version is 1.89.

Alternatively, you can build it using [Bazel](https://bazel.build) by executing `bazel build //rust:selenium-manager` from the top-level directory of the Selenium repo (the same one where the `WORKSPACE` file is).

## Usage
Expand Down
40 changes: 36 additions & 4 deletions rust/src/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use std::fs::File;
use std::path::{Path, PathBuf};

use crate::files::{create_parent_path_if_not_exists, create_path_if_not_exists};
use fs2::FileExt;
use std::fs;

thread_local!(static LOCK_PATH: RefCell<Option<PathBuf>> = const { RefCell::new(None) });
Expand Down Expand Up @@ -52,7 +51,7 @@ impl Lock {
let file = File::create(&path)?;

log.debug(format!("Acquiring lock: {}", path.display()));
file.lock_exclusive().unwrap_or_default();
file.lock().unwrap_or_default();
Comment thread
qodo-code-review[bot] marked this conversation as resolved.
Resolved
Comment thread
qodo-code-review[bot] marked this conversation as resolved.
Resolved
set_lock_path(Some(path.to_path_buf()));

Ok(Self {
Expand All @@ -63,7 +62,7 @@ impl Lock {

pub fn release(&mut self) {
if let Some(file) = self.file.take() {
FileExt::unlock(&file).unwrap_or_default();
file.unlock().unwrap_or_default();
fs::remove_file(&self.path).unwrap_or_default();
}
set_lock_path(None);
Expand Down Expand Up @@ -104,7 +103,7 @@ mod tests {
use crate::logger::Logger;
use std::fs;
use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

fn create_test_dir(prefix: &str) -> PathBuf {
let unique = SystemTime::now()
Expand Down Expand Up @@ -148,4 +147,37 @@ mod tests {
assert!(!lock_path.exists());
fs::remove_dir_all(root).unwrap();
}

#[test]
fn acquire_blocks_until_lock_is_released() {
let root = create_test_dir("lock_blocked");
let logger = Logger::new();
let mut lock = Lock::acquire(&logger, &root, None).unwrap();

let (ready_tx, ready_rx) = std::sync::mpsc::channel();
let (acquired_tx, acquired_rx) = std::sync::mpsc::channel();
let root_clone = root.clone();
let handle = std::thread::spawn(move || {
let logger = Logger::new();
ready_tx.send(()).unwrap();
let _competing = Lock::acquire(&logger, &root_clone, None).unwrap();
acquired_tx.send(()).unwrap();
});

ready_rx.recv().unwrap();
assert!(
acquired_rx
.recv_timeout(Duration::from_millis(200))
.is_err(),
"competing acquisition must remain blocked while the lock is held"
);

lock.release();
acquired_rx
.recv_timeout(Duration::from_secs(5))
.expect("competing acquisition must succeed after the lock is released");
handle.join().unwrap();

fs::remove_dir_all(root).unwrap();
}
}
Loading