Skip to content

Commit a3424a7

Browse files
committed
feat(base): Explicitly handle the CrossProcessLockKind::Dirty case in MediaStore.
This patch replaces the `into_guard()` call by a `match` over `CrossProcessLockKind` so that the `Dirty` case is explicitly handled. The mid-term idea is to remove the `into_guard()` method because it is “dangerous” as it hides the `Dirty` case.
1 parent fa3ca98 commit a3424a7

File tree

1 file changed

+15
-2
lines changed
  • crates/matrix-sdk-base/src/media/store

1 file changed

+15
-2
lines changed

crates/matrix-sdk-base/src/media/store/mod.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use std::{ops::Deref, sync::Arc};
3333

3434
use matrix_sdk_common::cross_process_lock::{
3535
CrossProcessLock, CrossProcessLockError, CrossProcessLockGeneration, CrossProcessLockGuard,
36-
TryLock,
36+
CrossProcessLockKind, TryLock,
3737
};
3838
use matrix_sdk_store_encryption::Error as StoreEncryptionError;
3939
pub use traits::{DynMediaStore, IntoMediaStore, MediaStore, MediaStoreInner};
@@ -133,7 +133,20 @@ impl MediaStoreLock {
133133

134134
/// Acquire a spin lock (see [`CrossProcessLock::spin_lock`]).
135135
pub async fn lock(&self) -> Result<MediaStoreLockGuard<'_>, CrossProcessLockError> {
136-
let cross_process_lock_guard = self.cross_process_lock.spin_lock(None).await??.into_guard();
136+
let cross_process_lock_guard = match self.cross_process_lock.spin_lock(None).await?? {
137+
// The lock is clean: no other hold acquired it, all good!
138+
CrossProcessLockKind::Clean(guard) => guard,
139+
140+
// The lock is dirty: another holder acquired it since the last time we acquired it.
141+
// It's not a problem in the case of the `MediaStore` because this API is “stateless” at
142+
// the time of writing (2025-11-11). There is nothing that can be out-of-sync: all the
143+
// state is in the database, nothing in memory.
144+
CrossProcessLockKind::Dirty(guard) => {
145+
self.cross_process_lock.clear_dirty();
146+
147+
guard
148+
}
149+
};
137150

138151
Ok(MediaStoreLockGuard { cross_process_lock_guard, store: self.store.deref() })
139152
}

0 commit comments

Comments
 (0)