1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//! Runtime lock mechanism to prevent concurrent BoxliteRuntime instances.
//!
//! Uses file locking (flock/fcntl) to ensure only one BoxliteRuntime can access
//! a given BOXLITE_HOME directory at a time.
use std::fs::{File, OpenOptions};
use std::path::{Path, PathBuf};
use boxlite_shared::errors::{BoxliteError, BoxliteResult};
/// A lock guard that holds an exclusive lock on the runtime directory.
///
/// The lock is automatically released when this guard is dropped,
/// or when the process exits/crashes.
#[derive(Debug)]
pub struct RuntimeLock {
#[allow(dead_code)] // Held for lifetime, not directly accessed
file: File,
path: PathBuf,
}
impl RuntimeLock {
/// Attempt to acquire an exclusive lock on the runtime directory.
///
/// # Arguments
/// * `home_dir` - The BOXLITE_HOME directory to lock
///
/// # Returns
/// * `Ok(RuntimeLock)` - Successfully acquired lock
/// * `Err(...)` - Another runtime is already using this directory
///
/// # Example
/// ```rust,no_run
/// use boxlite_runtime::lock::RuntimeLock;
/// use std::path::PathBuf;
///
/// let lock = RuntimeLock::acquire(&PathBuf::from("/tmp/test"))?;
/// // Lock is held until `lock` is dropped
/// # Ok::<(), boxlite_runtime::errors::BoxliteError>(())
/// ```
pub fn acquire(home_dir: &Path) -> BoxliteResult<Self> {
// Ensure the directory exists
std::fs::create_dir_all(home_dir)
.map_err(|e| BoxliteError::Storage(format!("failed to create home dir: {}", e)))?;
let lock_path = home_dir.join(".lock");
// Open or create the lock file
let file = OpenOptions::new()
.create(true)
.write(true)
.truncate(false)
.open(&lock_path)
.map_err(|e| BoxliteError::Storage(format!("failed to open lock file: {}", e)))?;
// Try to acquire exclusive lock (non-blocking)
#[cfg(unix)]
{
use std::os::unix::io::AsRawFd;
let fd = file.as_raw_fd();
let result = unsafe { libc::flock(fd, libc::LOCK_EX | libc::LOCK_NB) };
if result != 0 {
let err = std::io::Error::last_os_error();
if err.kind() == std::io::ErrorKind::WouldBlock {
return Err(BoxliteError::Internal(format!(
"Another BoxliteRuntime is already using directory: {}\n\
Only one runtime instance can use a BOXLITE_HOME directory at a time.",
home_dir.display()
)));
} else {
return Err(BoxliteError::Storage(format!(
"failed to acquire lock: {}",
err
)));
}
}
}
#[cfg(not(unix))]
{
// Windows: Use LockFile API
// TODO: Implement Windows file locking if needed
compile_error!("Windows file locking not yet implemented");
}
tracing::debug!(lock_path = %lock_path.display(), "Acquired runtime lock");
Ok(RuntimeLock {
file,
path: lock_path,
})
}
#[allow(dead_code)]
pub fn path(&self) -> &Path {
&self.path
}
}
impl Drop for RuntimeLock {
fn drop(&mut self) {
// Lock is automatically released by OS when file is closed
// We explicitly unlock for clarity
#[cfg(unix)]
{
use std::os::unix::io::AsRawFd;
let fd = self.file.as_raw_fd();
unsafe {
libc::flock(fd, libc::LOCK_UN);
}
}
tracing::debug!(lock_path = %self.path.display(), "Released runtime lock");
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Arc;
use std::thread;
use tempfile::TempDir;
#[test]
fn test_acquire_lock() {
let temp_dir = TempDir::new().unwrap();
let lock = RuntimeLock::acquire(temp_dir.path()).unwrap();
assert!(lock.path().exists());
assert!(lock.path().ends_with(".lock"));
}
#[test]
fn test_lock_prevents_concurrent_access() {
let temp_dir = TempDir::new().unwrap();
let dir_path = temp_dir.path().to_path_buf();
// Acquire first lock
let _lock1 = RuntimeLock::acquire(&dir_path).unwrap();
// Try to acquire second lock (should fail)
let result = RuntimeLock::acquire(&dir_path);
assert!(result.is_err());
let err_msg = result.unwrap_err().to_string();
assert!(err_msg.contains("Another BoxliteRuntime"));
}
#[test]
fn test_lock_released_on_drop() {
let temp_dir = TempDir::new().unwrap();
let dir_path = temp_dir.path().to_path_buf();
// Acquire and immediately drop lock
{
let _lock = RuntimeLock::acquire(&dir_path).unwrap();
} // Lock dropped here
// Should be able to acquire again
let _lock2 = RuntimeLock::acquire(&dir_path).unwrap();
}
#[test]
fn test_lock_across_threads() {
let temp_dir = TempDir::new().unwrap();
let dir_path = Arc::new(temp_dir.path().to_path_buf());
// Acquire lock in main thread
let _lock1 = RuntimeLock::acquire(&dir_path).unwrap();
// Try to acquire in another thread (should fail)
let dir_clone = Arc::clone(&dir_path);
let handle = thread::spawn(move || RuntimeLock::acquire(&dir_clone));
let result = handle.join().unwrap();
assert!(result.is_err());
}
#[test]
fn test_different_directories_independent() {
let temp_dir1 = TempDir::new().unwrap();
let temp_dir2 = TempDir::new().unwrap();
// Locks on different directories should not conflict
let _lock1 = RuntimeLock::acquire(temp_dir1.path()).unwrap();
let _lock2 = RuntimeLock::acquire(temp_dir2.path()).unwrap();
// Both should be held simultaneously
assert!(_lock1.path().exists());
assert!(_lock2.path().exists());
}
#[test]
fn test_lock_file_location() {
let temp_dir = TempDir::new().unwrap();
let lock = RuntimeLock::acquire(temp_dir.path()).unwrap();
assert_eq!(lock.path(), temp_dir.path().join(".lock"));
}
}