diff --git a/crates/matrix-sdk-crypto/src/store/locks.rs b/crates/matrix-sdk-crypto/src/store/locks.rs index b7fabaa01..03c0208ca 100644 --- a/crates/matrix-sdk-crypto/src/store/locks.rs +++ b/crates/matrix-sdk-crypto/src/store/locks.rs @@ -297,6 +297,12 @@ impl CryptoStoreLock { sleep(Duration::from_millis(wait.into())).await; } } + + /// Returns the value in the database that represents the holder's + /// identifier. + pub fn lock_holder(&self) -> &str { + &self.lock_holder + } } /// Error related to the locking API of the crypto store. diff --git a/crates/matrix-sdk/src/encryption/mod.rs b/crates/matrix-sdk/src/encryption/mod.rs index f891da56e..90d08bcab 100644 --- a/crates/matrix-sdk/src/encryption/mod.rs +++ b/crates/matrix-sdk/src/encryption/mod.rs @@ -858,6 +858,15 @@ impl Encryption { /// /// The provided `lock_value` must be a unique identifier for this process. pub async fn enable_cross_process_store_lock(&self, lock_value: String) -> Result<(), Error> { + // If the lock has already been created, don't recreate it from scratch. + if let Some(prev_lock) = self.client.inner.cross_process_crypto_store_lock.get() { + let prev_holder = prev_lock.lock_holder(); + if prev_holder == lock_value { + return Ok(()); + } + warn!("recreating cross-process store lock with a different holder value: prev was {prev_holder}, new is {lock_value}"); + } + let olm_machine = self.client.base_client().olm_machine().await; let olm_machine = olm_machine.as_ref().ok_or(Error::AuthenticationRequired)?;