Skip to content

Commit 3c9f92f

Browse files
committedDec 17, 2018
Add more robust cancellation support to MultiModelLoader.
There's a race described in #3343 where a child DataFetcher can call the MultiModelLoader after it has been cancelled, which can result in us unnecessarily starting a new load. Although we should probably also change the fetcher to avoid calling its callback after it's cancelled, we can make this behavior less of a problem in general by changing MultiModelLoader instead of relying on every individual DataFetcher implementation. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=216546841
1 parent c147f46 commit 3c9f92f

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
 

‎library/src/main/java/com/bumptech/glide/load/model/MultiModelLoader.java

+14
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ static class MultiFetcher<Data> implements DataFetcher<Data>, DataCallback<Data>
8181
@Nullable
8282
private List<Throwable> exceptions;
8383

84+
private volatile boolean isCancelled;
85+
8486
MultiFetcher(
8587
@NonNull List<DataFetcher<Data>> fetchers,
8688
@NonNull Pool<List<Throwable>> throwableListPool) {
@@ -97,6 +99,14 @@ public void loadData(
9799
this.callback = callback;
98100
exceptions = throwableListPool.acquire();
99101
fetchers.get(currentIndex).loadData(priority, this);
102+
103+
// If a race occurred where we cancelled the fetcher in cancel() and then called loadData here
104+
// immediately after, make sure that we cancel the newly started fetcher. We don't bother
105+
// checking cancelled before loadData because it's not required for correctness and would
106+
// require an unlikely race to be useful.
107+
if (isCancelled) {
108+
cancel();
109+
}
100110
}
101111

102112
@Override
@@ -112,6 +122,7 @@ public void cleanup() {
112122

113123
@Override
114124
public void cancel() {
125+
isCancelled = true;
115126
for (DataFetcher<Data> fetcher : fetchers) {
116127
fetcher.cancel();
117128
}
@@ -145,6 +156,9 @@ public void onLoadFailed(@NonNull Exception e) {
145156
}
146157

147158
private void startNextOrFail() {
159+
if (isCancelled) {
160+
return;
161+
}
148162
if (currentIndex < fetchers.size() - 1) {
149163
currentIndex++;
150164
loadData(priority, callback);

0 commit comments

Comments
 (0)
Please sign in to comment.