Fix download completion crash from stale array index across await

completeMultiFileDownload captured an index via firstIndex, then awaited a
detached file-size calculation. Another concurrently-completing download could
mutate activeDownloads during that suspension, leaving the index stale and
crashing in Array.remove(at:) with an out-of-bounds index. Remove the download
by identity instead.

Fixes a SIGTRAP seen in TestFlight build 261 (5 reports).
This commit is contained in:
Arkadiusz Fal
2026-05-11 22:50:16 +02:00
parent f74659e903
commit ec5ac944c4

View File

@@ -705,8 +705,11 @@ extension DownloadManager {
download.totalBytes = totalBytes
download.downloadedBytes = totalBytes
// Move to completed
activeDownloads.remove(at: index)
// Move to completed. Remove by identity rather than the captured `index`:
// the `await` above is a suspension point during which another completing
// download may have mutated `activeDownloads`, leaving `index` stale and
// making `remove(at:)` crash with an out-of-bounds index.
activeDownloads.removeAll { $0.id == downloadID }
completedDownloads.insert(download, at: 0)
// Update cached Sets