Skip to content

Commit a5c983b

Browse files
authored
Merge pull request #1899 from faraz152/fix/telegram-cache-timsort-contract
fix: snapshot lastModified before sort in TelegramCacheManager
2 parents 2546d3f + c328d8d commit a5c983b

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

app/src/main/java/com/theveloper/pixelplay/data/telegram/TelegramCacheManager.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,13 @@ class TelegramCacheManager @Inject constructor(
189189
return@launch // Within limits
190190
}
191191

192-
// Sort by last modified (oldest first) for LRU eviction
193-
embeddedArtFiles.sortBy { it.lastModified() }
192+
// Sort by last modified (oldest first) for LRU eviction.
193+
// Snapshot timestamps before sorting — reading lastModified() inside the
194+
// comparator on every pairwise call lets another coroutine's setLastModified()
195+
// change the value mid-sort, violating TimSort's comparator contract and
196+
// throwing "Comparison method violates its general contract!".
197+
val snapshots = embeddedArtFiles.associateWith { it.lastModified() }
198+
embeddedArtFiles.sortWith(compareBy { snapshots[it] })
194199

195200
var deletedCount = 0
196201
for (file in embeddedArtFiles) {

0 commit comments

Comments
 (0)