@@ -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 ( ) ,
0 commit comments