bark/lock_manager/mod.rs
1//! Named locks usable across async tasks, threads, processes, or browser
2//! tabs — depending on the backend you pick.
3//!
4//! # What it is
5//!
6//! bark needs to coordinate access to a shared dataset (e.g. a wallet
7//! database) so that two callers don't trample each other. The
8//! [`LockManager`] trait is where you plug in *how that coordination is
9//! enforced* on the target platform.
10//!
11//! Pick a manager whose enforcement scope matches the reach of the
12//! dataset bark is opening:
13//!
14//! - A wallet that only ever runs in a single process? An in-memory
15//! manager is enough.
16//! - A wallet on disk that another process might also open? You need a
17//! cross-process file-based manager.
18//! - A wallet running in the browser, possibly opened in multiple tabs?
19//! You need the Web Locks backend.
20//!
21//! Pick the wrong scope and bark will silently allow concurrent access.
22//! The rest of this page is the picking guide.
23//!
24//! # Platform support
25//!
26//! | Backend | Linux | macOS | iOS | Android | Windows | Web (wasm32) |
27//! |----------------------------------------------------------|:-----:|:-----:|:---:|:-------:|:-------:|:------------:|
28//! | [`MemoryLockManager`](memory::MemoryLockManager) | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
29//! | [`FlockPidLockManager`](pid_flock::FlockPidLockManager) | ✓ | ✓ | | ✓ | ✓ | |
30//! | [`FcntlPidLockManager`](pid_fcntl::FcntlPidLockManager) | ✓ | ✓ | ✓ | ✓ | | |
31//! | [`WebLockManager`](web_locks::WebLockManager) | | | | | | ✓ |
32//!
33//! # Safety scope
34//!
35//! Each backend prevents concurrent access by callers under a different
36//! scope. Pick the one that matches the threat you actually have:
37//!
38//! | Backend | Same async runtime | Same OS process | Across processes | Across machines (NFS/SMB) | Across browser tabs |
39//! |------------------|:------------------:|:---------------:|:----------------:|:-------------------------:|:-------------------:|
40//! | `Memory` | ✓ | ✓ | | | |
41//! | `FlockPidLock` | ✓ | ✓ | refuses 2nd | ⚠ | |
42//! | `FcntlPidLock` | ✓ | ✓ | refuses 2nd | ✓ (POSIX-compliant NFS) | |
43//! | `WebLocks` | ✓ | (n/a) | (n/a) | (n/a) | ✓ |
44//!
45//! ⚠ `FlockPidLock` uses `flock(2)` on Unix, whose behavior over networked
46//! filesystems is implementation-defined; use `FcntlPidLock` there.
47//!
48//! # Picking a backend
49//!
50//! - **Don't want to think about it?** Call [`platform_default`] —
51//! it returns the sensible PidLock-family backend for your build
52//! target (wasm gets Web Locks). Override with a specific backend
53//! only when you have a non-default deployment shape (e.g.
54//! multi-process access to the same datadir).
55//! - **Single-process apps and tests** —
56//! [`MemoryLockManager`](memory::MemoryLockManager) is the safe
57//! default: every instance in the process shares one key map, so two
58//! callers cannot accidentally end up with disjoint lock universes.
59//! - **Single-process-per-datadir CLIs / daemons** — pick a `PidLock`
60//! variant: [`FlockPidLockManager`](pid_flock::FlockPidLockManager)
61//! on Linux/macOS/Android/Windows desktops, or
62//! [`FcntlPidLockManager`](pid_fcntl::FcntlPidLockManager) when the
63//! datadir may live on networked storage. One OS-level lock on
64//! `<datadir>/LOCK` guarantees single-process exclusivity; per-key
65//! locking is in-memory.
66//! - **Web (wasm32)** — only [`WebLockManager`](web_locks::WebLockManager)
67//! (which delegates to `navigator.locks`) is available. Prevents
68//! concurrent access across same-origin tabs in the same browser;
69//! gives no guarantees across different browsers or incognito
70//! sessions.
71//!
72//! # What callers must guarantee
73//!
74//! - **Use one backend per dataset, forever.** Two distinct managers do
75//! not exclude each other; mixing backends or directories on the same
76//! data is silently unsafe.
77//! - **Use the same lock directory in every instance** for a given
78//! dataset.
79
80mod key;
81mod internal_memory;
82pub mod memory;
83#[cfg(target_arch = "wasm32")]
84pub mod web_locks;
85#[cfg(all(any(unix, windows), not(target_arch = "wasm32")))]
86pub mod pid_flock;
87#[cfg(all(any(unix), not(target_arch = "wasm32")))]
88pub mod pid_fcntl;
89
90use std::time::Duration;
91use std::path::PathBuf;
92use anyhow::bail;
93
94use crate::utils::time;
95
96const POLL_INTERVAL: Duration = Duration::from_millis(50);
97
98/// Errors from constructing a pid-lock-based [`LockManager`]
99/// ([`pid_flock::FlockPidLockManager`] or [`pid_fcntl::FcntlPidLockManager`]).
100///
101/// Pattern-match on this when you want to surface "another process is
102/// already using this datadir" differently from setup-failure cases.
103#[derive(thiserror::Error, Debug)]
104pub enum PidLockError {
105 /// Another instance — same process or otherwise — already holds
106 /// the pid lock for this datadir. The `pid` is the value that
107 /// instance wrote into the LOCK file (best-effort; may be absent
108 /// or stale).
109 #[error("another process is already using datadir {datadir}{}",
110 match pid {
111 Some(p) => format!(" (holder PID: {})", p),
112 None => String::new(),
113 })]
114 AlreadyHeld {
115 datadir: PathBuf,
116 pid: Option<u32>,
117 },
118
119 /// Anything else that went wrong setting up the datadir or
120 /// opening the lock file (filesystem permission, ENOENT, etc.).
121 #[error("failed to set up datadir {datadir}")]
122 SetupFailed {
123 datadir: PathBuf,
124 #[source]
125 source: anyhow::Error,
126 },
127}
128
129/// A handle that holds a named lock until dropped.
130///
131/// Trait objects are returned from [`LockManager`] methods so callers do
132/// not need to spell the backend's concrete guard type.
133pub trait LockGuard: Send + Sync + std::fmt::Debug {}
134
135/// Acquire and release named locks.
136///
137/// Implementations only need to provide [`try_lock`](Self::try_lock); the
138/// default [`lock`](Self::lock) polls it under a [`tokio::time::timeout`].
139#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
140#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
141pub trait LockManager: Send + Sync + std::fmt::Debug {
142 /// Try to acquire the named lock without waiting. Returns `None` if
143 /// it is already held, the key is rejected by [`validate_key`], or
144 /// the backend cannot acquire the lock for any other reason.
145 async fn try_lock(&self, key: &str) -> Option<Box<dyn LockGuard>>;
146
147 /// Acquire the named lock, polling [`try_lock`](Self::try_lock) until
148 /// it succeeds or `timeout` elapses.
149 ///
150 /// `timeout` is mandatory to make accidental deadlocks impossible at
151 /// the API level. Pass [`Duration::MAX`] if you really want to wait
152 /// indefinitely.
153 async fn lock(&self, key: &str, timeout: Duration)
154 -> anyhow::Result<Box<dyn LockGuard>>
155 {
156 let result = time::timeout(timeout, async {
157 loop {
158 if let Some(g) = self.try_lock(key).await {
159 return g;
160 }
161 time::sleep(POLL_INTERVAL).await;
162 }
163 }).await;
164 match result {
165 Ok(g) => Ok(g),
166 Err(_) => bail!("timed out acquiring lock {:?} after {:?}", key, timeout),
167 }
168 }
169}
170
171/// Return the recommended [`LockManager`] backend for the current
172/// build target. Most platforms will result a `LockManager` that
173/// can only be instantiated once.
174pub fn platform_default(datadir: impl Into<PathBuf>) -> anyhow::Result<Box<dyn LockManager>> {
175 #[cfg(target_arch = "wasm32")]
176 {
177 // Use navigator.locks via WebLockManager. An in-memory variant
178 // wouldn't be safe — the user can open the app in multiple
179 // tabs, each a separate wasm instance. navigator.locks is the
180 // only cross-tab coordination primitive in the browser.
181 // `datadir` is ignored.
182 let _ = datadir;
183 return Ok(Box::new(web_locks::WebLockManager::new()));
184 }
185
186 #[cfg(all(unix, not(target_arch = "wasm32")))]
187 {
188 // Use fcntl: it has wider support than flock across the unix
189 // family.
190 //
191 // We pick a PidLock variant over per-key fcntl files because:
192 // 1. It doesn't pollute the datadir with `<key>.lock` files.
193 // 2. It's faster — one OS-level lock at construction, then
194 // in-memory locking per key (no syscall per try_lock).
195 // 3. It avoids cross-process footguns like notifications not
196 // firing when a second process is doing the work.
197 return Ok(Box::new(pid_fcntl::FcntlPidLockManager::new(datadir)?));
198 }
199
200 #[cfg(all(windows, not(target_arch = "wasm32")))]
201 {
202 // Use std::fs::File::try_lock (LockFileEx under the hood):
203 // fcntl doesn't exist on Windows, and LockFileEx is the
204 // direct equivalent.
205 //
206 // We pick a PidLock variant over per-key file locks because:
207 // 1. It doesn't pollute the datadir with `<key>.lock` files.
208 // 2. It's faster — one OS-level lock at construction, then
209 // in-memory locking per key (no syscall per try_lock).
210 // 3. It avoids cross-process footguns like notifications not
211 // firing when a second process is doing the work.
212 return Ok(Box::new(pid_flock::FlockPidLockManager::new(datadir)?));
213 }
214
215 #[cfg(not(any(target_arch = "wasm32", unix, windows)))]
216 panic!("lock_manager::platform_default: no default backend for this target");
217}
218
219// The shared test harness uses `tokio::spawn` / `tokio::sync::Barrier`
220// / `tokio::time::timeout`, all of which require the `rt` feature that
221// is desktop-only. The web_locks backend has its own wasm-bindgen-test
222// suite in its module.
223#[cfg(all(test, not(target_arch = "wasm32")))]
224mod test {
225 use super::*;
226
227 use std::path::PathBuf;
228 use std::fs;
229 use std::sync::Arc;
230
231 const TEST_TIMEOUT: Duration = Duration::from_secs(5);
232
233 struct TestBackend {
234 name: &'static str,
235 mgr: Arc<dyn LockManager>,
236 // `None` for backends that don't use a directory (Memory).
237 dir: Option<PathBuf>,
238 }
239
240 impl Drop for TestBackend {
241 fn drop(&mut self) {
242 if let Some(d) = &self.dir {
243 let _ = fs::remove_dir_all(d);
244 }
245 }
246 }
247
248 fn tmp_dir() -> PathBuf {
249 let dir = std::env::temp_dir()
250 .join(format!("bark-lock-test-{}", rand::random::<u64>()));
251 fs::create_dir_all(&dir).unwrap();
252 dir
253 }
254
255 /// Every backend available on this target.
256 fn managers() -> Vec<TestBackend> {
257 let mut v = Vec::new();
258
259 v.push(TestBackend {
260 name: "InternalMemory",
261 mgr: Arc::new(internal_memory::InternalMemoryLockManager::new()),
262 dir: None,
263 });
264
265 v.push(TestBackend {
266 name: "Memory",
267 mgr: Arc::new(memory::MemoryLockManager::new()),
268 dir: None,
269 });
270
271 #[cfg(all(any(unix, windows), not(target_arch = "wasm32")))]
272 {
273 let dir = tmp_dir();
274 v.push(TestBackend {
275 name: "FlockPidLock",
276 mgr: Arc::new(pid_flock::FlockPidLockManager::new(&dir).unwrap()),
277 dir: Some(dir),
278 });
279 }
280
281 #[cfg(all(unix, not(target_arch = "wasm32")))]
282 {
283 let dir = tmp_dir();
284 v.push(TestBackend {
285 name: "FcntlPidLock",
286 mgr: Arc::new(pid_fcntl::FcntlPidLockManager::new(&dir).unwrap()),
287 dir: Some(dir),
288 });
289 }
290
291 #[cfg(target_arch = "wasm32")]
292 {
293 v.push(TestBackend {
294 name: "Web",
295 mgr: Arc::new(web_locks::WebLockManager::new()),
296 dir: None,
297 });
298 }
299
300 v
301 }
302
303 #[tokio::test]
304 async fn acquire_and_release() {
305 for tb in managers() {
306 let g = tb.mgr.lock("bark.ln_receive.1", TEST_TIMEOUT).await.unwrap();
307 drop(g);
308 let _g2 = tb.mgr.lock("bark.ln_receive.1", TEST_TIMEOUT).await.unwrap();
309 }
310 }
311
312 #[tokio::test]
313 async fn try_lock_returns_none_when_held() {
314 for tb in managers() {
315 let g = tb.mgr.lock("k", TEST_TIMEOUT).await.unwrap();
316 let busy = tb.mgr.try_lock("k").await;
317 assert!(busy.is_none(), "{}: second try_lock should be blocked", tb.name);
318 drop(g);
319 let g2 = tb.mgr.try_lock("k").await;
320 assert!(g2.is_some(), "{}: try_lock should succeed after release", tb.name);
321 }
322 }
323
324 #[tokio::test]
325 async fn distinct_keys_dont_block() {
326 for tb in managers() {
327 let _g1 = tb.mgr.lock("a", TEST_TIMEOUT).await.unwrap();
328 let _g2 = tb.mgr.lock("b", TEST_TIMEOUT).await.unwrap();
329 }
330 }
331
332 #[tokio::test]
333 async fn lock_returns_timeout_error() {
334 for tb in managers() {
335 let _held = tb.mgr.lock("k", TEST_TIMEOUT).await.unwrap();
336
337 // Acquire from another task so holding `_held` doesn't block
338 // the test on its own memory-mutex wait.
339 let mgr = Arc::clone(&tb.mgr);
340 let result = tokio::spawn(async move {
341 mgr.lock("k", Duration::from_millis(150)).await
342 }).await.unwrap();
343
344 assert!(result.is_err(), "{}: expected timeout, got {:?}", tb.name, result);
345 assert!(result.unwrap_err().to_string().contains("timed out"));
346 }
347 }
348
349 #[tokio::test]
350 async fn waiter_unblocks_after_drop() {
351 for tb in managers() {
352 let g = tb.mgr.lock("k", TEST_TIMEOUT).await.unwrap();
353
354 let mgr = Arc::clone(&tb.mgr);
355 let waiter = tokio::spawn(async move {
356 mgr.lock("k", TEST_TIMEOUT).await.unwrap()
357 });
358
359 tokio::time::sleep(Duration::from_millis(150)).await;
360 drop(g);
361
362 let result = time::timeout(Duration::from_secs(2), waiter).await;
363 assert!(result.is_ok(), "{}: waiter should succeed after holder dropped", tb.name);
364 }
365 }
366
367 #[tokio::test]
368 async fn ten_concurrent_try_lock_only_one_wins() {
369 // Asserts that `try_lock` is atomic under contention: when N
370 // callers race for the same key, exactly one observes it as free.
371 //
372 // Force 10 tasks to call try_lock at the same point via a barrier.
373 // Whichever the executor polls first will hold the guard for
374 // 100 ms; that is long enough for the other 9 tasks to be polled
375 // and observe the lock as held.
376 use tokio::sync::Barrier;
377 const N: usize = 10;
378
379 for tb in managers() {
380 let barrier = Arc::new(Barrier::new(N));
381 let mut handles = Vec::with_capacity(N);
382
383 for _ in 0..N {
384 let mgr = Arc::clone(&tb.mgr);
385 let barrier = Arc::clone(&barrier);
386 handles.push(tokio::spawn(async move {
387 barrier.wait().await;
388 let guard = mgr.try_lock("contested").await;
389 let acquired = guard.is_some();
390 if acquired {
391 tokio::time::sleep(Duration::from_millis(100)).await;
392 }
393 acquired
394 }));
395 }
396
397 let mut successes = 0usize;
398 for h in handles {
399 successes += h.await.unwrap() as usize;
400 }
401 assert_eq!(
402 successes, 1,
403 "{}: expected exactly 1 successful try_lock out of {}, got {}",
404 tb.name, N, successes,
405 );
406 }
407 }
408
409 #[tokio::test]
410 async fn reject_bad_keys() {
411 for tb in managers() {
412 // Empty.
413 assert!(tb.mgr.try_lock("").await.is_none(), "{}: empty", tb.name);
414 // Disallowed character (path separator).
415 assert!(tb.mgr.try_lock("a/b").await.is_none(), "{}: slash", tb.name);
416 // Disallowed character (angle bracket).
417 assert!(tb.mgr.try_lock("a<b>").await.is_none(), "{}: angle", tb.name);
418 // Disallowed start (dot).
419 assert!(tb.mgr.try_lock(".abc").await.is_none(), "{}: leading dot", tb.name);
420 // Disallowed start (underscore).
421 assert!(tb.mgr.try_lock("_abc").await.is_none(), "{}: leading underscore", tb.name);
422 // Disallowed end (dash).
423 assert!(tb.mgr.try_lock("abc-").await.is_none(), "{}: trailing dash", tb.name);
424 // Disallowed end (dot).
425 assert!(tb.mgr.try_lock("abc.").await.is_none(), "{}: trailing dot", tb.name);
426 // Path-traversal sentinels.
427 assert!(tb.mgr.try_lock(".").await.is_none(), "{}: dot", tb.name);
428 assert!(tb.mgr.try_lock("..").await.is_none(), "{}: dotdot", tb.name);
429
430 // Allowed: bark's actual key shapes.
431 assert!(tb.mgr.try_lock("bark.lightning.send.42").await.is_some(),
432 "{}: bark.lightning.send.42 should be valid", tb.name);
433 // Allowed: digit start (hex wallet fingerprint).
434 assert!(tb.mgr.try_lock("01abcdef.round.7").await.is_some(),
435 "{}: 01abcdef.round.7 should be valid", tb.name);
436 }
437 }
438
439 #[test]
440 fn managers_covers_every_compiled_backend() {
441 // If a backend is dropped from `managers()`, this assertion goes red.
442 let names: Vec<_> = managers().iter().map(|tb| tb.name).collect();
443 assert!(names.contains(&"Memory"), "missing Memory: {:?}", names);
444 #[cfg(target_arch = "wasm32")]
445 assert!(names.contains(&"Web"), "missing Web: {:?}", names);
446 }
447
448 #[tokio::test]
449 async fn platform_default_returns_a_working_manager() {
450 let dir = tmp_dir();
451 let mgr = super::platform_default(&dir)
452 .expect("platform_default should construct a manager");
453 let g = mgr.try_lock("bark.platform.default.test").await;
454 assert!(g.is_some(), "platform_default's manager should grant a fresh lock");
455 drop(g);
456 let _ = fs::remove_dir_all(&dir);
457 }
458}