Remote: Fix a race when reporting action progresses.
Fixes
I will create checkpick request for 4.2.0 once merged into main branch.
Closes
@@ -62,12 +62,14 @@ import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Nullable;
+import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;
/** Tracks state for the UI. */
@@ -228,6 +230,7 @@ final class UiStateTracker {
}
}
+ @GuardedBy("this")
private final LinkedHashMap<String, ProgressState> runningProgresses = new LinkedHashMap<>();
/** Starts tracking the state of an action. */
@@ -333,6 +336,13 @@ final class UiStateTracker {
state.latestEvent = event;
}
+ synchronized Optional<ProgressState> firstProgress() {
+ if (runningProgresses.isEmpty()) {
+ return Optional.empty();
+ }
+ return Optional.of(runningProgresses.entrySet().iterator().next().getValue());
+ }
+
/** Generates a human-readable description of this action's state. */
synchronized String describe() {
if (runningStrategiesBitmap != 0) {
@@ -705,12 +715,12 @@ final class UiStateTracker {
}
private String describeActionProgress(ActionState action, int desiredWidth) {
- if (action.runningProgresses.isEmpty()) {
+ Optional<ActionState.ProgressState> stateOpt = action.firstProgress();
+ if (!stateOpt.isPresent()) {
return "";
}
- ActionState.ProgressState state =
- action.runningProgresses.entrySet().iterator().next().getValue();
+ ActionState.ProgressState state = stateOpt.get();
ActionProgressEvent event = state.latestEvent;
String message = event.progress();
if (message.isEmpty()) {
