Skip to content

Commit

Permalink
fix: do not interrupt threads on cancellation in UniDelayOnItem
Browse files Browse the repository at this point in the history
This causes spurious interrupt signals in failure retry with exponential backoff
that get propagated downstream.

Also https://twitter.com/relizarov/status/1184460504238100480

Fixes #1436
  • Loading branch information
jponge committed Nov 22, 2023
1 parent 0217652 commit 905e55b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void cancel() {
if (!isCancelled()) {
super.cancel();
if (scheduledFuture != null) {
scheduledFuture.cancel(true);
scheduledFuture.cancel(false);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.time.Duration;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -558,4 +559,21 @@ public void rejectNullExecutors() {
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("`executor` must not be `null`");
}

@Test
void avoidSpuriousInterruptsWithBackoff() {
AtomicBoolean flip = new AtomicBoolean();
AtomicBoolean interrupted = new AtomicBoolean();
String result = Uni.createFrom().item(() -> {
if (flip.getAndSet(true)) {
return "yolo";
}
throw new RuntimeException("boom");
})
.onFailure().retry().withBackOff(Duration.ofMillis(100), Duration.ofSeconds(5)).atMost(1)
.onItem().invoke(() -> interrupted.set(Thread.currentThread().isInterrupted()))
.await().atMost(Duration.ofSeconds(1));
assertThat(result).isEqualTo("yolo");
assertThat(interrupted).isFalse();
}
}

0 comments on commit 905e55b

Please sign in to comment.