Skip to content

Commit 7c83b55

Browse files
authored
check various task states before returning (#1739)
It's possible that the task was not successful, was canceled or timed out which should be checked before returning. Existing callers of this method catch Exception so new exception types being thrown are safe.
1 parent b348663 commit 7c83b55

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

  • firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/internal/common

firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/internal/common/Utils.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Comparator;
2828
import java.util.List;
2929
import java.util.concurrent.Callable;
30+
import java.util.concurrent.CancellationException;
3031
import java.util.concurrent.CountDownLatch;
3132
import java.util.concurrent.Executor;
3233
import java.util.concurrent.ExecutorService;
@@ -171,12 +172,9 @@ public static <T> T awaitEvenIfOnMainThread(Task<T> task)
171172

172173
task.continueWith(
173174
TASK_CONTINUATION_EXECUTOR_SERVICE,
174-
new Continuation<T, Object>() {
175-
@Override
176-
public Object then(@NonNull Task<T> task) throws Exception {
177-
latch.countDown();
178-
return null;
179-
}
175+
unusedTask -> {
176+
latch.countDown();
177+
return null;
180178
});
181179

182180
if (Looper.getMainLooper() == Looper.myLooper()) {
@@ -185,8 +183,12 @@ public Object then(@NonNull Task<T> task) throws Exception {
185183
latch.await();
186184
}
187185

188-
if (task.isComplete()) {
186+
if (task.isSuccessful()) {
189187
return task.getResult();
188+
} else if (task.isCanceled()) {
189+
throw new CancellationException("Task is already canceled");
190+
} else if (task.isComplete()) {
191+
throw new IllegalStateException(task.getException());
190192
} else {
191193
throw new TimeoutException();
192194
}

0 commit comments

Comments
 (0)