Skip to content

Commit

Permalink
Merge pull request #5903 from Bnyro/master
Browse files Browse the repository at this point in the history
fix: crash in DownloadService if trying to read dead download item
  • Loading branch information
Bnyro authored Apr 14, 2024
2 parents 2205043 + dd59b89 commit c9d6589
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface DownloadDao {
suspend fun findById(videoId: String): DownloadWithItems

@Query("SELECT * FROM downloaditem WHERE id = :id")
suspend fun findDownloadItemById(id: Int): DownloadItem
suspend fun findDownloadItemById(id: Int): DownloadItem?

@Query("DELETE FROM downloaditem WHERE id = :id")
suspend fun deleteDownloadItemById(id: Int)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ import kotlin.io.path.div
import kotlin.io.path.fileSize
import kotlin.math.min
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
Expand Down Expand Up @@ -341,7 +340,8 @@ class DownloadService : LifecycleService() {
}

lifecycleScope.launch(coroutineContext) {
downloadFile(Database.downloadDao().findDownloadItemById(id))
val file = Database.downloadDao().findDownloadItemById(id) ?: return@launch
downloadFile(file)
}
}

Expand All @@ -361,16 +361,14 @@ class DownloadService : LifecycleService() {
/**
* Stop downloading job for given [id]. If no downloads are active, stop the service.
*/
private fun stop(id: Int) = CoroutineScope(Dispatchers.IO).launch {
private fun stop(id: Int) = lifecycleScope.launch(coroutineContext) {
downloadQueue[id] = false
_downloadFlow.emit(id to DownloadStatus.Stopped)

lifecycleScope.launch {
val item = Database.downloadDao().findDownloadItemById(id)
notificationManager.cancel(item.getNotificationId())
Database.downloadDao().deleteDownloadItemById(id)
stopServiceIfDone()
}
val item = Database.downloadDao().findDownloadItemById(id) ?: return@launch
notificationManager.cancel(item.getNotificationId())
Database.downloadDao().deleteDownloadItemById(id)
stopServiceIfDone()
}

/**
Expand Down

0 comments on commit c9d6589

Please sign in to comment.