Commit f8b85739 by olly Committed by Oliver Woodman

Fix race condition that could cause downloader not to be canceled

PiperOrigin-RevId: 224048465
parent 5bbe3ae7
...@@ -565,7 +565,7 @@ public final class DownloadManager { ...@@ -565,7 +565,7 @@ public final class DownloadManager {
*/ */
@TargetState private volatile int targetState; @TargetState private volatile int targetState;
@MonotonicNonNull private volatile Downloader downloader; @MonotonicNonNull private Downloader downloader;
@MonotonicNonNull private Thread thread; @MonotonicNonNull private Thread thread;
@MonotonicNonNull private Throwable error; @MonotonicNonNull private Throwable error;
...@@ -624,6 +624,7 @@ public final class DownloadManager { ...@@ -624,6 +624,7 @@ public final class DownloadManager {
state = STATE_STARTED; state = STATE_STARTED;
targetState = STATE_COMPLETED; targetState = STATE_COMPLETED;
downloadManager.onTaskStateChange(this); downloadManager.onTaskStateChange(this);
downloader = downloaderFactory.createDownloader(action);
thread = new Thread(this); thread = new Thread(this);
thread.start(); thread.start();
} }
...@@ -648,11 +649,7 @@ public final class DownloadManager { ...@@ -648,11 +649,7 @@ public final class DownloadManager {
private void stopDownloadThread(@TargetState int targetState) { private void stopDownloadThread(@TargetState int targetState) {
this.targetState = targetState; this.targetState = targetState;
// TODO: The possibility of downloader being null here may prevent the download thread from Assertions.checkNotNull(downloader).cancel();
// stopping in a timely way. Fix this.
if (downloader != null) {
downloader.cancel();
}
Assertions.checkNotNull(thread).interrupt(); Assertions.checkNotNull(thread).interrupt();
} }
...@@ -675,7 +672,6 @@ public final class DownloadManager { ...@@ -675,7 +672,6 @@ public final class DownloadManager {
logd("Task is started", this); logd("Task is started", this);
Throwable error = null; Throwable error = null;
try { try {
downloader = downloaderFactory.createDownloader(action);
if (action.isRemoveAction) { if (action.isRemoveAction) {
downloader.remove(); downloader.remove();
} else { } else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment