Skip to content

Commit dbea547

Browse files
committed
add CrossProcessLockError::LockDirtied
1 parent 87b64e5 commit dbea547

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

crates/matrix-sdk-common/src/cross_process_lock.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ where
240240
// function, to avoid multiple reentrant calls.
241241
let mut _attempt = self.locking_attempt.lock().await;
242242

243+
// The lock is already dirtied? Let's stop here.
244+
if self.is_dirty() {
245+
return Err(CrossProcessLockError::LockDirtied);
246+
}
247+
243248
// If another thread obtained the lock, make sure to only superficially increase
244249
// the number of holders, and carry on.
245250
if self.num_holders.load(Ordering::SeqCst) > 0 {
@@ -274,6 +279,8 @@ where
274279
"The lock has been acquired, but it's been dirtied!"
275280
);
276281
self.is_dirty.store(true, Ordering::SeqCst);
282+
283+
return Err(CrossProcessLockError::LockDirtied);
277284
}
278285

279286
// This was the same generation, no problem.
@@ -340,6 +347,11 @@ where
340347
}
341348
}
342349

350+
// The lock has been dirtied. Exit the loop.
351+
if this.is_dirty() {
352+
break;
353+
}
354+
343355
sleep(Duration::from_millis(EXTEND_LEASE_EVERY_MS)).await;
344356

345357
match this
@@ -459,6 +471,11 @@ pub enum CrossProcessLockError {
459471
#[error("a lock timed out")]
460472
LockTimeout,
461473

474+
/// The lock has been dirtied, i.e. it means another process took the lock
475+
/// while this process was holding it.
476+
#[error("a lock has been dirtied")]
477+
LockDirtied,
478+
462479
#[error(transparent)]
463480
#[cfg(not(target_family = "wasm"))]
464481
TryLockError(#[from] Box<dyn Error + Send + Sync>),

0 commit comments

Comments
 (0)