Skip to content

Commit 6b838af

Browse files
committed
Fix an issue with infinite timeout setting.
JAVA-5856
1 parent 5eef0fc commit 6b838af

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

driver-core/src/main/com/mongodb/internal/connection/TlsChannelStreamFactoryFactory.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,9 @@ public void openAsync(final OperationContext operationContext, final AsyncComple
237237
socketChannel, () -> initializeTslChannel(handler, socketChannel));
238238

239239
int connectTimeoutMs = getSettings().getConnectTimeout(TimeUnit.MILLISECONDS);
240-
241-
group.getTimeoutExecutor().schedule(() -> {
242-
boolean markedTimedOut = socketRegistration.markConnectionEstablishmentTimedOut();
243-
if (markedTimedOut) {
244-
closeAndTimeout(handler, socketChannel);
245-
}
246-
}, connectTimeoutMs, TimeUnit.MILLISECONDS);
247-
240+
if (connectTimeoutMs > 0) {
241+
scheduleTimeoutInterruption(handler, socketRegistration, socketChannel, connectTimeoutMs);
242+
}
248243
selectorMonitor.register(socketRegistration);
249244
} catch (IOException e) {
250245
handler.failed(new MongoSocketOpenException("Exception opening socket", getServerAddress(), e));
@@ -253,6 +248,18 @@ public void openAsync(final OperationContext operationContext, final AsyncComple
253248
}
254249
}
255250

251+
private void scheduleTimeoutInterruption(final AsyncCompletionHandler<Void> handler,
252+
final SelectorMonitor.SocketRegistration socketRegistration,
253+
final SocketChannel socketChannel,
254+
final int connectTimeoutMs) {
255+
group.getTimeoutExecutor().schedule(() -> {
256+
boolean markedTimedOut = socketRegistration.markConnectionEstablishmentTimedOut();
257+
if (markedTimedOut) {
258+
closeAndTimeout(handler, socketChannel);
259+
}
260+
}, connectTimeoutMs, TimeUnit.MILLISECONDS);
261+
}
262+
256263
private void closeAndTimeout(final AsyncCompletionHandler<Void> handler, final SocketChannel socketChannel) {
257264
// We check if this stream was closed before timeout exception.
258265
boolean streamClosed = isClosed();

0 commit comments

Comments
 (0)