Skip to content

Commit

Permalink
fix ProgressiveDownloader infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
a.mochalov committed Aug 30, 2022
1 parent 6288182 commit dca6378
Showing 1 changed file with 18 additions and 13 deletions.
Expand Up @@ -88,19 +88,7 @@ public ProgressiveDownloader(
public void download(@Nullable ProgressListener progressListener)
throws IOException, InterruptedException {
this.progressListener = progressListener;
downloadRunnable =
new RunnableFutureTask<Void, IOException>() {
@Override
protected Void doWork() throws IOException {
cacheWriter.cache();
return null;
}

@Override
protected void cancelWork() {
cacheWriter.cancel();
}
};
downloadRunnable = createDownloadTask();

if (priorityTaskManager != null) {
priorityTaskManager.add(C.PRIORITY_DOWNLOAD);
Expand All @@ -119,6 +107,8 @@ protected void cancelWork() {
Throwable cause = Assertions.checkNotNull(e.getCause());
if (cause instanceof PriorityTooLowException) {
// The next loop iteration will block until the task is able to proceed.
// recreate downloadRunnable in order to prevent error state caching
downloadRunnable = createDownloadTask();
} else if (cause instanceof IOException) {
throw (IOException) cause;
} else {
Expand Down Expand Up @@ -161,4 +151,19 @@ private void onProgress(long contentLength, long bytesCached, long newBytesCache
: ((bytesCached * 100f) / contentLength);
progressListener.onProgress(contentLength, bytesCached, percentDownloaded);
}

private RunnableFutureTask<Void, IOException> createDownloadTask() {
return new RunnableFutureTask<Void, IOException>() {
@Override
protected Void doWork() throws IOException {
cacheWriter.cache();
return null;
}

@Override
protected void cancelWork() {
cacheWriter.cancel();
}
};
}
}

0 comments on commit dca6378

Please sign in to comment.