mirror of
https://github.com/matrix-org/matrix-rust-sdk.git
synced 2026-05-15 11:36:07 -04:00
This implements a new time lease based lock for the `CryptoStore`, that doesn't require explicit unlocking, so that's more robust in the context of #1928, where any process may die because the device is running out of battery, or unexpected flows cause a lock to not be released properly in one or the other process. ``` //! This is a per-process lock that may be used only for very specific use //! cases, where multiple processes might concurrently write to the same //! database at the same time; this would invalidate crypto store caches, so //! that should be done mindfully. Such a lock can be acquired multiple times by //! the same process, and it remains active as long as there's at least one user //! in a given process. //! //! The lock is implemented using time-based leases to values inserted in a //! crypto store. The store maintains the lock identifier (key), who's the //! current holder (value), and an expiration timestamp on the side; see also //! `CryptoStore::try_take_leased_lock` for more details. //! //! The lock is initially acquired for a certain period of time (namely, the //! duration of a lease, aka `LEASE_DURATION_MS`), and then a "heartbeat" task //! renews the lease to extend its duration, every so often (namely, every //! `EXTEND_LEASE_EVERY_MS`). Since the tokio scheduler might be busy, the //! extension request should happen way more frequently than the duration of a //! lease, in case a deadline is missed. The current values have been chosen to //! reflect that, with a ratio of 1:10 as of 2023-06-23. //! //! Releasing the lock happens naturally, by not renewing a lease. It happens //! automatically after the duration of the last lease, at most. ``` --- * feat: implement a time lease based lock for the crypto store * feat: switch the crypto-store lock a time-leased based one * chore: fix CI, don't use unixepoch in sqlite and do time math in rust * chore: dummy implementation in indexeddb, don't run lease locks tests there * feat: in NSE, wait the duration of a lease if first attempt to unlock failed * feat: immediately release the lock when there are no more holders * chore: clippy * chore: add comment about atomic sanity * chore: increase sleeps in timeline queue tests? * feat: lower lease and renew durations * feat: keep track of the extend-lease task * fix: increment num_holders when acquiring the lock for the first time * chore: reduce indent + abort prev renew task on non-wasm + add logs