Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#562) Fix a request not getting canceled when encountering a 404 response from the server #575

Merged
merged 1 commit into from
Feb 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.adamantcheese.chan.core.cache.downloader

import com.github.adamantcheese.chan.core.cache.CacheHandler
import com.github.adamantcheese.chan.utils.BackgroundUtils
import com.github.adamantcheese.chan.utils.exhaustive
import com.github.k1rakishou.fsaf.FileManager
import com.github.k1rakishou.fsaf.file.RawFile
import io.reactivex.BackpressureStrategy
Expand Down Expand Up @@ -110,6 +111,7 @@ internal class ChunkPersister(
}, BackpressureStrategy.BUFFER)
}

@Synchronized
private fun handleErrors(
url: String,
totalChunksCount: Int,
Expand All @@ -119,24 +121,16 @@ internal class ChunkPersister(
emitter: FlowableEmitter<ChunkDownloadEvent>
) {
val state = activeDownloads.getState(url)
val isStoppedOrCanceled = state == DownloadState.Canceled
|| state == DownloadState.Stopped
val isStoppedOrCanceled = state == DownloadState.Canceled || state == DownloadState.Stopped

// If totalChunksCount == 1 then there is nothing else to stop so we can just emit
// one error
if (isStoppedOrCanceled || totalChunksCount > 1 && error !is IOException) {
when (state) {
DownloadState.Canceled -> {
activeDownloads.get(url)?.cancelableDownload?.cancel()
}
DownloadState.Stopped -> {
activeDownloads.get(url)?.cancelableDownload?.stop()
}
else -> {
throw RuntimeException("Expected: Canceled or Stopped, but " +
"actual state is Running")
}
}
DownloadState.Running,
DownloadState.Canceled -> activeDownloads.get(url)?.cancelableDownload?.cancel()
DownloadState.Stopped -> activeDownloads.get(url)?.cancelableDownload?.stop()
}.exhaustive

log(TAG, "pipeChunk($chunkIndex) ($url) cancel for chunk ${chunk.start}..${chunk.end}")
if (isStoppedOrCanceled) {
Expand All @@ -150,8 +144,7 @@ internal class ChunkPersister(
}
} else {
emitter.tryOnError(error)
log(TAG, "pipeChunk($chunkIndex) ($url) fail " +
"for chunk ${chunk.start}..${chunk.end}")
log(TAG, "pipeChunk($chunkIndex) ($url) fail for chunk ${chunk.start}..${chunk.end}")
}
}

Expand Down