Skip to content

Commit

Permalink
fix: merge the JDK bug fix "CompletableFuture.orTimeout leaks if th…
Browse files Browse the repository at this point in the history
…e future completes exceptionally", more info see

- the JDK bug issue: https://bugs.openjdk.org/browse/JDK-8303742
- PR review: openjdk/jdk#13059
- JDK bugfix commit: openjdk/jdk@ded6a81
  • Loading branch information
linzee1 authored and oldratlee committed Aug 17, 2024
1 parent 609047e commit 4695801
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3725,6 +3725,11 @@ public static <C extends CompletableFuture<?>> C cffuOrTimeout(
* Unless all subsequent actions of dependent CompletableFutures is ensured executing async
* (aka. the dependent CompletableFutures is created by async methods), using this method and {@link CompletableFuture#orTimeout(long, TimeUnit)}
* is one less thread switch of task execution when triggered by timeout.
* <p>
* Note: Before Java 21(Java 20-), {@link CompletableFuture#orTimeout(long, TimeUnit)}
* can leak if the future completes exceptionally, more info see
* <a href="https://bugs.openjdk.org/browse/JDK-8303742">JDK-8303742</a>
* and <a href="https://github.com/openjdk/jdk/commit/ded6a8131970ac2f7ae59716769e6f6bae3b809a">JDK bugfix commit</a>.
*
* @param timeout how long to wait before completing exceptionally with a TimeoutException, in units of {@code unit}
* @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter
Expand All @@ -3741,7 +3746,7 @@ public static <C extends CompletableFuture<?>> C orTimeout(C cfThis, long timeou
} else {
// below code is copied from CompletableFuture#orTimeout with small adoption
if (!cfThis.isDone()) {
ScheduledFuture<?> f = Delayer.delayToTimoutCf(cfThis, timeout, unit);
ScheduledFuture<?> f = Delayer.delayToTimeoutCf(cfThis, timeout, unit);
cfThis.whenComplete(new FutureCanceller(f));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static ScheduledFuture<?> delay(Runnable command, long delay, TimeUnit unit) {
* @return a Future can be used to cancel the delayed task(timeout CF)
* @see FutureCanceller
*/
static ScheduledFuture<?> delayToTimoutCf(CompletableFuture<?> cf, long delay, TimeUnit unit) {
static ScheduledFuture<?> delayToTimeoutCf(CompletableFuture<?> cf, long delay, TimeUnit unit) {
return delay(new CfTimeout(cf), delay, unit);
}

Expand Down Expand Up @@ -188,7 +188,7 @@ public void run() {
* code is copied from {@link CompletableFuture.Canceller} with small adoption.
*
* @see Delayer#delay(Runnable, long, TimeUnit)
* @see Delayer#delayToTimoutCf(CompletableFuture, long, TimeUnit)
* @see Delayer#delayToTimeoutCf(CompletableFuture, long, TimeUnit)
* @see Delayer#delayToCompleteCf(CompletableFuture, Object, long, TimeUnit)
*/
@SuppressWarnings("JavadocReference")
Expand All @@ -202,7 +202,7 @@ final class FutureCanceller implements BiConsumer<Object, Throwable> {

@Override
public void accept(Object ignore, @Nullable Throwable ex) {
if (ex == null && f != null && !f.isDone())
if (f != null && !f.isDone())
f.cancel(false);
}
}
Expand Down

0 comments on commit 4695801

Please sign in to comment.