Skip to content

Commit 2c44c6a

Browse files
While handleCommunicationIssue, closeSocket should use reconnect impl otherwise tryingToConnect will guard (#1206)
* While `handleCommunicationIssue`, `closeSocket` should use reconnect impl otherwise `tryingToConnect` will guard Signed-off-by: Maurice van Veen <[email protected]> * Fix NPE on dataPortFuture with low connection timeout Signed-off-by: Maurice van Veen <[email protected]> * During forceReconnect, ensure reader/writer is stopped before continuing --------- Signed-off-by: Maurice van Veen <[email protected]> Co-authored-by: scottf <[email protected]>
1 parent ddb4b78 commit 2c44c6a

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,12 @@ void forceReconnectImpl(ForceReconnectOptions options) throws InterruptedExcepti
326326

327327
// stop i/o
328328
try {
329-
this.reader.stop(false).get(10, TimeUnit.SECONDS);
329+
this.reader.stop(false).get(100, TimeUnit.MILLISECONDS);
330330
} catch (Exception ex) {
331331
processException(ex);
332332
}
333333
try {
334-
this.writer.stop().get(10, TimeUnit.SECONDS);
334+
this.writer.stop().get(100, TimeUnit.MILLISECONDS);
335335
} catch (Exception ex) {
336336
processException(ex);
337337
}
@@ -765,7 +765,7 @@ void closeSocket(boolean tryReconnectIfConnected, boolean forceClose) throws Int
765765
if (isClosing()) { // isClosing() means we are in the close method or were asked to be
766766
close();
767767
} else if (wasConnected && tryReconnectIfConnected) {
768-
reconnect();
768+
reconnectImpl(); // call the impl here otherwise the tryingToConnect guard will block the behavior
769769
}
770770
} finally {
771771
closeSocketLock.unlock();
@@ -880,7 +880,11 @@ void closeSocketImpl(boolean forceClose) {
880880
//
881881
}
882882

883-
this.dataPortFuture.cancel(true);
883+
// Close and reset the current data port and future
884+
if (dataPortFuture != null) {
885+
dataPortFuture.cancel(true);
886+
dataPortFuture = null;
887+
}
884888

885889
// Close the current socket and cancel anyone waiting for it
886890
try {

src/test/java/io/nats/client/ConnectTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,4 +617,17 @@ private static void validateRunInJsCluster(Connection nc1, Connection nc2, Conne
617617
assertTrue(urls3.contains(port2));
618618
assertTrue(urls3.contains(port3));
619619
}
620+
621+
// https://github.com/nats-io/nats.java/issues/1201
622+
@Test
623+
void testLowConnectionTimeoutResultsInIOException() {
624+
Options options = Options.builder()
625+
.connectionTimeout(Duration.ZERO)
626+
.build();
627+
628+
assertThrows(IOException.class, () -> {
629+
Connection nc = Nats.connect(options);
630+
nc.close();
631+
});
632+
}
620633
}

0 commit comments

Comments
 (0)