Skip to content

Commit d6e8dae

Browse files
committed
fixup
1 parent a836b72 commit d6e8dae

File tree

4 files changed

+77
-15
lines changed

4 files changed

+77
-15
lines changed

crates/matrix-sdk-base/src/client.rs

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -221,24 +221,50 @@ impl BaseClient {
221221
cross_process_store_locks_holder_name: &str,
222222
handle_verification_events: bool,
223223
) -> Result<Self> {
224-
let in_memory_store_config =
224+
let StoreConfig { state_store, event_cache_store, media_store, .. } =
225225
StoreConfig::new(cross_process_store_locks_holder_name.to_owned())
226-
.state_store(MemoryStore::new())
227-
.crypto_store(self.crypto_store.clone());
226+
.event_cache_store({
227+
if cross_process_store_locks_holder_name == self.event_cache_store.lock_holder()
228+
{
229+
return Err(Error::DuplicatedCrossProcessLockHolder(
230+
cross_process_store_locks_holder_name.to_owned(),
231+
));
232+
}
233+
234+
// SAFETY: the store will be used behind another lock holder, so it's safe to
235+
// use a clone of it.
236+
unsafe { self.event_cache_store.clone_store() }
237+
})
238+
.media_store({
239+
if cross_process_store_locks_holder_name == self.media_store.lock_holder() {
240+
return Err(Error::DuplicatedCrossProcessLockHolder(
241+
cross_process_store_locks_holder_name.to_owned(),
242+
));
243+
}
244+
245+
// SAFETY: the store will be used behind another lock holder, so it's safe to
246+
// use a clone of it.
247+
unsafe { self.media_store.clone_store() }
248+
});
249+
250+
// Clone the Crypto store.
251+
// We copy the crypto store as well as the `OlmMachine` for two reasons:
252+
// 1. The `self.crypto_store` is the same as the one used inside the
253+
// `OlmMachine`.
254+
// 2. We need to ensure that the parent and child use the same data and caches
255+
// inside the `OlmMachine` so the various ratchets and places where new
256+
// randomness gets introduced don't diverge, i.e. one-time keys that get
257+
// generated by the Olm Account or Olm sessions when they encrypt or decrypt
258+
// messages.
259+
let crypto_store = self.crypto_store.clone();
260+
let olm_machine = self.olm_machine.clone();
228261

229262
let copy = Self {
230-
state_store: BaseStateStore::new(in_memory_store_config.state_store),
231-
// The event and media stores both have cross process locking support.
232-
event_cache_store: self.event_cache_store.clone(),
233-
media_store: self.media_store.clone(),
234-
// We copy the crypto store as well as the `OlmMachine` for two reasons:
235-
// 1. The `self.crypto_store` is the same as the one used inside the `OlmMachine`.
236-
// 2. We need to ensure that the parent and child use the same data and caches inside
237-
// the `OlmMachine` so the various ratchets and places where new randomness gets
238-
// introduced don't diverge, i.e. one-time keys that get generated by the Olm Account
239-
// or Olm sessions when they encrypt or decrypt messages.
240-
crypto_store: self.crypto_store.clone(),
241-
olm_machine: self.olm_machine.clone(),
263+
state_store: BaseStateStore::new(state_store),
264+
event_cache_store,
265+
media_store,
266+
crypto_store,
267+
olm_machine,
242268
ignore_user_list_changes: Default::default(),
243269
room_info_notable_update_sender: self.room_info_notable_update_sender.clone(),
244270
room_key_recipient_strategy: self.room_key_recipient_strategy.clone(),

crates/matrix-sdk-base/src/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ pub enum Error {
5858
#[error(transparent)]
5959
CryptoStore(#[from] CryptoStoreError),
6060

61+
/// The given cross-process lock holder name is already used by a store.
62+
#[error("The given cross-process lock holder name is already used by a store: `{0}`")]
63+
DuplicatedCrossProcessLockHolder(String),
64+
6165
/// An error occurred during a E2EE operation.
6266
#[cfg(feature = "e2e-encryption")]
6367
#[error(transparent)]

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,22 @@ impl EventCacheStoreLock {
9191

9292
Ok(lock_state)
9393
}
94+
95+
/// Clone the inner store, by-passing the lock.
96+
///
97+
/// # Safety
98+
///
99+
/// This method is useful when you want to build a new client with another
100+
/// lock holder name for example. But the lock is fully by-passed in this
101+
/// method. Be extremely careful!
102+
pub(crate) unsafe fn clone_store(&self) -> Arc<DynEventCacheStore> {
103+
self.store.clone()
104+
}
105+
106+
/// Get the lock holder name.
107+
pub(crate) fn lock_holder(&self) -> &str {
108+
self.cross_process_lock.lock_holder()
109+
}
94110
}
95111

96112
/// The equivalent of [`CrossProcessLockState`] but for the [`EventCacheStore`].

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,22 @@ impl MediaStoreLock {
150150

151151
Ok(MediaStoreLockGuard { cross_process_lock_guard, store: self.store.deref() })
152152
}
153+
154+
/// Clone the inner store, by-passing the lock.
155+
///
156+
/// # Safety
157+
///
158+
/// This method is useful when you want to build a new client with another
159+
/// lock holder name for example. But the lock is fully by-passed in this
160+
/// method. Be extremely careful!
161+
pub(crate) unsafe fn clone_store(&self) -> Arc<DynMediaStore> {
162+
self.store.clone()
163+
}
164+
165+
/// Get the lock holder name.
166+
pub(crate) fn lock_holder(&self) -> &str {
167+
self.cross_process_lock.lock_holder()
168+
}
153169
}
154170

155171
/// An RAII implementation of a “scoped lock” of an [`MediaStoreLock`].

0 commit comments

Comments
 (0)