downloads: harden queue reset reconciliation

Refresh per-source rows under the reconciliation mutex before reset decisions and chunk queue-visibility checks to respect SQLite bind limits.

Cover a blocked pre-completion emission and source deletion at the bundled SQLite variable limit.
This commit is contained in:
austinried
2026-11-14 12:05:29 +00:00
parent cc94dfc458
commit e4d97f7aba
3 changed files with 113 additions and 3 deletions
@@ -56,6 +56,9 @@ interface DownloadDao {
@Query("SELECT * FROM song_downloads WHERE sourceId = :sourceId")
fun downloads(sourceId: Long): Flow<List<SongDownload>>
@Query("SELECT * FROM song_downloads WHERE sourceId = :sourceId")
suspend fun downloadsOnce(sourceId: Long): List<SongDownload>
@Query(
"SELECT d.* FROM song_downloads d " +
"JOIN songs ON songs.sourceId = d.sourceId AND songs.id = d.songId " +
@@ -85,10 +85,12 @@ class DownloadRepository(
File(downloadsDir, NO_MEDIA).createNewFile()
scope.launch {
var resetPoint = queueRepository.captureResetPoint()
downloadStates().collect { (sourceId, rows) ->
statesFlow.value = rows
downloadStates().collect { (sourceId, emittedRows) ->
statesFlow.value = emittedRows
if (sourceId != null) {
mutex.withLock {
val rows = db.downloadDao().downloadsOnce(sourceId).associateBy { it.songId }
statesFlow.value = rows
val completed = completedDownloads(sourceId)
val completedRows = completedDownloadKeys(rows)
completedBaselineMutex.withLock {
@@ -147,7 +149,11 @@ class DownloadRepository(
songIds: Collection<String>,
) {
if (songIds.isEmpty()) return
if (db.downloadDao().queueVisibleSongIds(sourceId, songIds).isNotEmpty()) {
val queueVisible =
songIds.asSequence().chunked(DELETE_CHUNK).any { ids ->
db.downloadDao().queueVisibleSongIds(sourceId, ids).isNotEmpty()
}
if (queueVisible) {
captureForReset(sourceId)
} else {
completedBaselineMutex.withLock {