Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1257d6d

Browse files
committedDec 14, 2024·
[rust] Acquire file lock before downloading driver and browser
1 parent 74435f0 commit 1257d6d

File tree

3 files changed

+43
-31
lines changed

3 files changed

+43
-31
lines changed
 

‎rust/src/files.rs

-18
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
use crate::config::OS;
1919
use crate::config::OS::WINDOWS;
20-
use crate::lock::Lock;
2120
use crate::{
2221
format_one_arg, format_three_args, run_shell_command_by_os, Command, Logger, CP_VOLUME_COMMAND,
2322
HDIUTIL_ATTACH_COMMAND, HDIUTIL_DETACH_COMMAND, MACOS, MSIEXEC_INSTALL_COMMAND,
@@ -122,22 +121,6 @@ pub fn uncompress(
122121
extension
123122
));
124123

125-
// Acquire file lock to prevent race conditions accessing the cache folder by concurrent SM processes
126-
let mut lock = Lock::acquire(log, target, single_file.clone())?;
127-
if !lock.exists() {
128-
let num_files_in_target = WalkDir::new(target).into_iter().count();
129-
if (single_file.is_some() && num_files_in_target == 1)
130-
|| (single_file.is_none() && num_files_in_target > 1)
131-
{
132-
log.trace(format!(
133-
"{} file(s) in {}",
134-
num_files_in_target,
135-
target.display()
136-
));
137-
return Ok(());
138-
}
139-
}
140-
141124
if extension.eq_ignore_ascii_case(ZIP) {
142125
unzip(compressed_file, target, log, single_file)?
143126
} else if extension.eq_ignore_ascii_case(GZ) {
@@ -177,7 +160,6 @@ pub fn uncompress(
177160
)));
178161
}
179162

180-
lock.release();
181163
Ok(())
182164
}
183165

‎rust/src/lib.rs

+40-11
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ use crate::config::{str_to_os, ManagerConfig};
2121
use crate::downloads::download_to_tmp_folder;
2222
use crate::edge::{EdgeManager, EDGEDRIVER_NAME, EDGE_NAMES, WEBVIEW2_NAME};
2323
use crate::files::{
24-
capitalize, collect_files_from_cache, create_parent_path_if_not_exists,
25-
create_path_if_not_exists, default_cache_folder, find_latest_from_cache, get_binary_extension,
26-
path_to_string,
24+
capitalize, collect_files_from_cache, create_path_if_not_exists, default_cache_folder,
25+
find_latest_from_cache, get_binary_extension, path_to_string,
2726
};
2827
use crate::files::{parse_version, uncompress, BrowserPath};
2928
use crate::firefox::{FirefoxManager, FIREFOX_NAME, GECKODRIVER_NAME};
3029
use crate::grid::GRID_NAME;
3130
use crate::iexplorer::{IExplorerManager, IEDRIVER_NAME, IE_NAMES};
31+
use crate::lock::Lock;
3232
use crate::logger::Logger;
3333
use crate::metadata::{
3434
create_browser_metadata, create_stats_metadata, get_browser_version_from_metadata,
@@ -59,6 +59,7 @@ pub mod files;
5959
pub mod firefox;
6060
pub mod grid;
6161
pub mod iexplorer;
62+
pub mod lock;
6263
pub mod logger;
6364
pub mod metadata;
6465
pub mod mirror;
@@ -184,6 +185,22 @@ pub trait SeleniumManager {
184185
// ----------------------------------------------------------
185186

186187
fn download_driver(&mut self) -> Result<(), Error> {
188+
let driver_path_in_cache = self.get_driver_path_in_cache()?;
189+
let driver_name_with_extension = self.get_driver_name_with_extension();
190+
191+
let mut lock = Lock::acquire(
192+
&self.get_logger(),
193+
&driver_path_in_cache,
194+
Some(driver_name_with_extension.clone()),
195+
)?;
196+
if !lock.exists() && driver_path_in_cache.exists() {
197+
self.get_logger().debug(format!(
198+
"Driver already in cache: {}",
199+
driver_path_in_cache.display()
200+
));
201+
return Ok(());
202+
}
203+
187204
let driver_url = self.get_driver_url()?;
188205
self.get_logger().debug(format!(
189206
"Downloading {} {} from {}",
@@ -196,20 +213,20 @@ pub trait SeleniumManager {
196213

197214
if self.is_grid() {
198215
let driver_path_in_cache = self.get_driver_path_in_cache()?;
199-
create_parent_path_if_not_exists(&driver_path_in_cache)?;
200-
Ok(fs::rename(driver_zip_file, driver_path_in_cache)?)
216+
fs::rename(driver_zip_file, driver_path_in_cache)?;
201217
} else {
202-
let driver_path_in_cache = self.get_driver_path_in_cache()?;
203-
let driver_name_with_extension = self.get_driver_name_with_extension();
204-
Ok(uncompress(
218+
uncompress(
205219
&driver_zip_file,
206220
&driver_path_in_cache,
207221
self.get_logger(),
208222
self.get_os(),
209223
Some(driver_name_with_extension),
210224
None,
211-
)?)
225+
)?;
212226
}
227+
228+
lock.release();
229+
Ok(())
213230
}
214231

215232
fn download_browser(
@@ -304,6 +321,17 @@ pub trait SeleniumManager {
304321
)));
305322
}
306323

324+
let browser_path_in_cache = self.get_browser_path_in_cache()?;
325+
let mut lock = Lock::acquire(&self.get_logger(), &browser_path_in_cache, None)?;
326+
if !lock.exists() && browser_binary_path.exists() {
327+
self.get_logger().debug(format!(
328+
"Browser already in cache: {}",
329+
browser_binary_path.display()
330+
));
331+
self.set_browser_path(path_to_string(&browser_binary_path));
332+
return Ok(Some(browser_binary_path.clone()));
333+
}
334+
307335
let browser_url = self.get_browser_url_for_download(original_browser_version)?;
308336
self.get_logger().debug(format!(
309337
"Downloading {} {} from {}",
@@ -318,12 +346,13 @@ pub trait SeleniumManager {
318346
self.get_browser_label_for_download(original_browser_version)?;
319347
uncompress(
320348
&driver_zip_file,
321-
&self.get_browser_path_in_cache()?,
322-
self.get_logger(),
349+
&browser_path_in_cache,
350+
&self.get_logger(),
323351
self.get_os(),
324352
None,
325353
browser_label_for_download,
326354
)?;
355+
lock.release();
327356
}
328357
if browser_binary_path.exists() {
329358
self.set_browser_path(path_to_string(&browser_binary_path));

‎rust/src/lock.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub struct Lock {
1515
}
1616

1717
impl Lock {
18+
// Acquire file lock to prevent race conditions accessing the cache folder by concurrent SM processes
1819
pub fn acquire(
1920
log: &Logger,
2021
target: &Path,
@@ -30,15 +31,15 @@ impl Lock {
3031
let path = lock_folder.join(LOCK_FILE);
3132
let file = File::create(&path)?;
3233

33-
log.trace(format!("Using lock file at {}", path.display()));
34+
log.debug(format!("Acquiring lock: {}", path.display()));
3435
file.lock_exclusive().unwrap_or_default();
3536

3637
Ok(Self { file, path })
3738
}
3839

3940
pub fn release(&mut self) {
40-
self.file.unlock().unwrap_or_default();
4141
fs::remove_file(&self.path).unwrap_or_default();
42+
self.file.unlock().unwrap_or_default();
4243
}
4344

4445
pub fn exists(&mut self) -> bool {

0 commit comments

Comments
 (0)
Please sign in to comment.