From b2791d5aadc648814559041ce71a7d08e46377f0 Mon Sep 17 00:00:00 2001 From: Jorge Martin Espinosa Date: Wed, 2 Apr 2025 12:58:40 +0200 Subject: [PATCH] Make sure the live timeline is destroyed before clearing a room's cache (#4515) * Make sure the live timeline is destroyed before clearing a room's cache According to the SDK team, having a live timeline could lead to DB issues. * Try to destroy all room and list item references when the `RustRoomFactory` is destroyed It may also have some effect on DB corruption issues we've seen --- .../android/features/roomlist/impl/RoomListPresenter.kt | 2 ++ .../android/libraries/matrix/impl/room/RustRoomFactory.kt | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/features/roomlist/impl/src/main/kotlin/io/element/android/features/roomlist/impl/RoomListPresenter.kt b/features/roomlist/impl/src/main/kotlin/io/element/android/features/roomlist/impl/RoomListPresenter.kt index a9be28e6ae..19e46607ef 100644 --- a/features/roomlist/impl/src/main/kotlin/io/element/android/features/roomlist/impl/RoomListPresenter.kt +++ b/features/roomlist/impl/src/main/kotlin/io/element/android/features/roomlist/impl/RoomListPresenter.kt @@ -311,6 +311,8 @@ class RoomListPresenter @Inject constructor( private fun CoroutineScope.clearCacheOfRoom(roomId: RoomId) = launch { client.getRoom(roomId)?.use { room -> + // Ideally we wouldn't have a live timeline at this point, but right now we instantiate one when retrieving the room + room.liveTimeline.close() room.clearEventCacheStorage() } } diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustRoomFactory.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustRoomFactory.kt index 2bcd15c142..a65112fca9 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustRoomFactory.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustRoomFactory.kt @@ -81,6 +81,10 @@ class RustRoomFactory( withContext(NonCancellable + dispatcher) { mutex.withLock { Timber.d("Destroying room factory") + cache.snapshot().values.forEach { (listItem, innerRoom) -> + innerRoom.destroy() + listItem.destroy() + } cache.evictAll() isDestroyed = true }