@@ -717,9 +717,11 @@ private static <T> CompletableFuture<T> f_mostSuccessTupleWithEhOf0(
717
717
718
718
private static <T > CompletableFuture <T > f_mostSuccessTupleOf0 (
719
719
Executor executorWhenTimeout , long timeout , TimeUnit unit , CompletionStage <?>[] stages ) {
720
- // MUST be *Non-Minimal* CF instances in order to read results(`getSuccessNow`),
721
- // otherwise UnsupportedOperationException
722
- final CompletableFuture <Object >[] cfArray = toNonMinCfArray0 (stages );
720
+ // 1. MUST be non-minimal-stage CF instances in order to read results(`getSuccessNow`), otherwise UnsupportedOpException.
721
+ // 2. SHOULD copy input cfs(by calling `exceptionally` method) to avoid memory leaks,
722
+ // otherwise all input cfs would be retained until output cf completes.
723
+ CompletableFuture <?>[] cfArray = mapArray (stages , CompletableFuture []::new ,
724
+ s -> toNonMinCf0 (s ).exceptionally (v -> null ));
723
725
return cffuCompleteOnTimeout (CompletableFuture .allOf (cfArray ), null , timeout , unit , executorWhenTimeout )
724
726
.handle ((unused , ex ) -> f_tupleOf0 (f_mGetSuccessNow0 (null , cfArray )));
725
727
}
@@ -944,9 +946,11 @@ private static <T> CompletableFuture<List<T>> mostSuccessResultsOf0(
944
946
.handle ((unused , ex ) -> arrayList (getSuccessNow (f , valueIfNotSuccess )));
945
947
}
946
948
947
- // MUST be non-minimal-stage CF instances in order to read results(`getSuccessNow`),
948
- // otherwise UnsupportedOperationException
949
- final CompletableFuture <T >[] cfArray = toNonMinCfArray0 (cfs );
949
+ // 1. MUST be non-minimal-stage CF instances in order to read results(`getSuccessNow`), otherwise UnsupportedOpException.
950
+ // 2. SHOULD copy input cfs(by calling `exceptionally` method) to avoid memory leaks,
951
+ // otherwise all input cfs would be retained until output cf completes.
952
+ CompletableFuture <T >[] cfArray = mapArray (cfs , CompletableFuture []::new ,
953
+ s -> LLCF .<T >toNonMinCf0 (s ).exceptionally (v -> valueIfNotSuccess ));
950
954
return cffuCompleteOnTimeout (CompletableFuture .allOf (cfArray ), null , timeout , unit , executorWhenTimeout )
951
955
.handle ((unused , ex ) -> arrayList (f_mGetSuccessNow0 (valueIfNotSuccess , cfArray )));
952
956
}
0 commit comments