Skip to content

Commit 42133a6

Browse files
committed
fix(ffi): use EventCache::clear_all_rooms() to clear the events caches
Clearing only the store backend, while not emptying the in-memory linked chunks, will lead to out-of-sync state across the in-memory caches and the database. As a result, it's safer to call the existing `EventCacheInner::clear_all_rooms`, which will clear all the rooms manually.
1 parent d30dc71 commit 42133a6

File tree

3 files changed

+18
-6
lines changed
  • bindings/matrix-sdk-ffi/src
  • crates
    • matrix-sdk-base/src/event_cache/store
    • matrix-sdk/src/event_cache

3 files changed

+18
-6
lines changed

bindings/matrix-sdk-ffi/src/client.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,13 +1208,14 @@ impl Client {
12081208
/// retention policy.
12091209
pub async fn clear_caches(&self) -> Result<(), ClientError> {
12101210
let closure = async || -> Result<_, EventCacheError> {
1211-
let store = self.inner.event_cache_store().lock().await?;
1212-
1213-
// Clear all the room chunks.
1214-
store.clear_all_rooms_chunks().await?;
1215-
12161211
// Clean up the media cache according to the current media retention policy.
1217-
store.clean_up_media_cache().await?;
1212+
self.inner.event_cache_store().lock().await?.clean_up_media_cache().await?;
1213+
1214+
// Clear all the room chunks. It's important to *not* call
1215+
// `EventCacheStore::clear_all_rooms_chunks` here, because there might be live
1216+
// observers of the linked chunks, and that would cause some very bad state
1217+
// mismatch.
1218+
self.inner.event_cache().clear_all_rooms().await?;
12181219

12191220
Ok(())
12201221
};

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ pub trait EventCacheStore: AsyncTraitDeps {
101101
/// using the above [`Self::handle_linked_chunk_updates`] methods. It
102102
/// must *also* delete all the events' content, if they were stored in a
103103
/// separate table.
104+
///
105+
/// ⚠ This is meant only for super specific use cases, where there shouldn't
106+
/// be any live in-memory linked chunks. In general, prefer using
107+
/// `EventCache::clear_all_rooms()` from the common SDK crate.
104108
async fn clear_all_rooms_chunks(&self) -> Result<(), Self::Error>;
105109

106110
/// Given a set of event IDs, return the duplicated events along with their

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,13 @@ impl EventCache {
361361
Ok((room, drop_handles))
362362
}
363363

364+
/// Cleanly clear all the rooms' event caches.
365+
///
366+
/// This will notify any live observers that the room has been cleared.
367+
pub async fn clear_all_rooms(&self) -> Result<()> {
368+
self.inner.clear_all_rooms().await
369+
}
370+
364371
/// Add an initial set of events to the event cache, reloaded from a cache.
365372
///
366373
/// TODO: temporary for API compat, as the event cache should take care of

0 commit comments

Comments
 (0)