Skip to content

Commit 3198cba

Browse files
Fix cleanResponses must handle cleaning up cancelled future (#1218)
* Fix `cleanResponses` must handle cleaning up cancelled future If a future would be cancelled outside of `cleanResponses`, calling `future.get()` would throw a `CancellationException`. This exception was not caught, breaking future cleanup. Signed-off-by: Maurice van Veen <[email protected]> * Catch Throwable Signed-off-by: Maurice van Veen <[email protected]> --------- Signed-off-by: Maurice van Veen <[email protected]>
1 parent b2ceb1a commit 3198cba

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/main/java/io/nats/client/impl/NatsConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,7 @@ else if (future.isDone()) {
11711171
wasInterrupted = true;
11721172
break;
11731173
}
1174-
catch (ExecutionException ignore) {}
1174+
catch (Throwable ignore) {}
11751175
}
11761176

11771177
if (remove) {

src/test/java/io/nats/client/impl/RequestTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,4 +739,24 @@ public void testNatsImplAndEmptyStatsCoverage() {
739739
assertEquals(0, s.getDroppedCount());
740740
assertEquals(0, s.getReconnects());
741741
}
742+
743+
@Test
744+
public void testCancelledFutureMustNotErrorOnCleanResponses() throws Exception {
745+
try (NatsTestServer ts = new NatsTestServer(false)) {
746+
Options options = Options.builder()
747+
.server(ts.getURI())
748+
.noNoResponders()
749+
.requestCleanupInterval(Duration.ofSeconds(10))
750+
.build();
751+
NatsConnection nc = (NatsConnection) Nats.connect(options);
752+
753+
NatsRequestCompletableFuture future = (NatsRequestCompletableFuture) nc.request("request", null);
754+
future.cancelClosing();
755+
756+
// Future is already cancelled, collecting it shouldn't result in an exception being thrown.
757+
assertDoesNotThrow(() -> {
758+
nc.cleanResponses(false);
759+
});
760+
}
761+
}
742762
}

0 commit comments

Comments
 (0)