Skip to content

Commit ea957a2

Browse files
committed
[rust] Check lock folder at the end of SM execution and clear it if required
1 parent 014129f commit ea957a2

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

rust/src/lock.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717

1818
use crate::logger::Logger;
1919
use anyhow::Error;
20+
use std::cell::RefCell;
2021
use std::fs::File;
2122
use std::path::{Path, PathBuf};
2223

2324
use crate::files::{create_parent_path_if_not_exists, create_path_if_not_exists};
2425
use fs2::FileExt;
2526
use std::fs;
2627

28+
thread_local!(static LOCK_PATH: RefCell<Option<PathBuf>> = RefCell::new(None));
29+
2730
const LOCK_FILE: &str = "sm.lock";
2831

2932
pub struct Lock {
@@ -51,15 +54,36 @@ impl Lock {
5154
log.debug(format!("Acquiring lock: {}", path.display()));
5255
file.lock_exclusive().unwrap_or_default();
5356

57+
LOCK_PATH.with(|option| {
58+
*option.borrow_mut() = Some(lock_folder.to_path_buf());
59+
});
60+
5461
Ok(Self { file, path })
5562
}
5663

5764
pub fn release(&mut self) {
5865
fs::remove_file(&self.path).unwrap_or_default();
5966
self.file.unlock().unwrap_or_default();
67+
68+
LOCK_PATH.with(|option| {
69+
*option.borrow_mut() = None;
70+
});
6071
}
6172

6273
pub fn exists(&mut self) -> bool {
6374
self.path.exists()
6475
}
6576
}
77+
78+
pub fn clear_lock_if_required() {
79+
let mut lock_path: Option<PathBuf> = None;
80+
LOCK_PATH.with(|option| {
81+
let optional_path = &*option.borrow();
82+
if optional_path.is_some() {
83+
lock_path = Some(optional_path.as_ref().unwrap().to_path_buf());
84+
}
85+
});
86+
if lock_path.is_some() {
87+
fs::remove_dir_all(lock_path.unwrap()).unwrap_or_default();
88+
}
89+
}

rust/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use exitcode::OK;
2222
use exitcode::UNAVAILABLE;
2323
use selenium_manager::config::{BooleanKey, StringKey, CACHE_PATH_KEY};
2424
use selenium_manager::grid::GridManager;
25+
use selenium_manager::lock::clear_lock_if_required;
2526
use selenium_manager::logger::{Logger, BROWSER_PATH, DRIVER_PATH};
2627
use selenium_manager::metadata::clear_metadata;
2728
use selenium_manager::TTL_SEC;
@@ -318,5 +319,6 @@ fn flush_and_exit(code: i32, log: &Logger, err: Option<Error>) -> ! {
318319
}
319320
log.set_code(code);
320321
log.flush();
322+
clear_lock_if_required();
321323
exit(code);
322324
}

0 commit comments

Comments
 (0)