From c328d8d85313111ef48572d977fd30086deccc8e Mon Sep 17 00:00:00 2001 From: faraz152 <38698072+faraz152@users.noreply.github.com> Date: Mon, 4 May 2026 17:48:29 +0500 Subject: [PATCH] fix: snapshot lastModified before sort in TelegramCacheManager MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reading file.lastModified() inside the comparator lambda on every pairwise comparison lets a concurrent setLastModified() call change the value mid-sort, breaking TimSort's transitivity requirement and crashing with 'Comparison method violates its general contract!'. Snapshot all timestamps into a map before sorting so the comparator sees a stable, immutable view — the same fix applied to AlbumArtCacheManager in an earlier commit. --- .../pixelplay/data/telegram/TelegramCacheManager.kt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/theveloper/pixelplay/data/telegram/TelegramCacheManager.kt b/app/src/main/java/com/theveloper/pixelplay/data/telegram/TelegramCacheManager.kt index c351b05ab..7623e9f82 100644 --- a/app/src/main/java/com/theveloper/pixelplay/data/telegram/TelegramCacheManager.kt +++ b/app/src/main/java/com/theveloper/pixelplay/data/telegram/TelegramCacheManager.kt @@ -189,8 +189,13 @@ class TelegramCacheManager @Inject constructor( return@launch // Within limits } - // Sort by last modified (oldest first) for LRU eviction - embeddedArtFiles.sortBy { it.lastModified() } + // Sort by last modified (oldest first) for LRU eviction. + // Snapshot timestamps before sorting — reading lastModified() inside the + // comparator on every pairwise call lets another coroutine's setLastModified() + // change the value mid-sort, violating TimSort's comparator contract and + // throwing "Comparison method violates its general contract!". + val snapshots = embeddedArtFiles.associateWith { it.lastModified() } + embeddedArtFiles.sortWith(compareBy { snapshots[it] }) var deletedCount = 0 for (file in embeddedArtFiles) {