Skip to content

Commit bcec086

Browse files
committed
fix: do not use subclasses of CompletableFuture to catch cancel
We can catch it via normal callbacks, this lets us use the proper wrapped CF from SR-CP.
1 parent 54a3812 commit bcec086

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.smallrye.mutiny.operators.uni;
22

3+
import java.util.concurrent.CancellationException;
34
import java.util.concurrent.CompletableFuture;
45
import java.util.concurrent.atomic.AtomicReference;
56

@@ -13,21 +14,22 @@ public class UniSubscribeToCompletionStage {
1314
public static <T> CompletableFuture<T> subscribe(Uni<T> uni, Context context) {
1415
final AtomicReference<Cancellable> cancellable = new AtomicReference<>();
1516

16-
CompletableFuture<T> future = new CompletableFuture<T>() {
17-
@Override
18-
public boolean cancel(boolean mayInterruptIfRunning) {
19-
boolean cancelled = super.cancel(mayInterruptIfRunning);
20-
if (cancelled) {
17+
CompletableFuture<T> future = Infrastructure.wrapCompletableFuture(new CompletableFuture<T>());
18+
future.whenComplete((val, x) -> {
19+
if (x instanceof CancellationException) {
20+
// forward the cancellation to the uni
21+
if (future.isCancelled()) {
2122
Cancellable c = cancellable.get();
2223
if (c != null) {
2324
c.cancel();
2425
}
2526
}
26-
return cancelled;
2727
}
28-
};
29-
28+
});
3029
cancellable.set(uni.subscribe().with(context, future::complete, future::completeExceptionally));
31-
return Infrastructure.wrapCompletableFuture(future);
30+
// We return future here and not whatever is returned from future.whenComplete, because that
31+
// new stage will wrap any exceptions into a CompletionException which we do not want, and
32+
// is exposed by UniOrTest (at least)
33+
return future;
3234
}
3335
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989

9090
<microprofile-reactive-streams.version>3.0.1</microprofile-reactive-streams.version>
9191
<microprofile-context-propagation.version>1.3</microprofile-context-propagation.version>
92-
<smallrye-context-propagation.version>2.2.1</smallrye-context-propagation.version>
92+
<smallrye-context-propagation.version>2.3.0</smallrye-context-propagation.version>
9393
<smallrye-config.version>3.14.1</smallrye-config.version>
9494
<smallrye-common-annotation.version>2.13.9</smallrye-common-annotation.version>
9595
<jakarta.enterprise.cdi-api.version>4.1.0</jakarta.enterprise.cdi-api.version>

0 commit comments

Comments
 (0)