Skip to content

Commit 5ca354f

Browse files
committed
Ensure initial socket setup takes into account timeoutMS
1 parent 20602e0 commit 5ca354f

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private SSLSocket initializeSslSocketOverSocksProxy(final OperationContext opera
119119
final int serverPort = address.getPort();
120120

121121
SocksSocket socksProxy = new SocksSocket(settings.getProxySettings());
122-
configureSocket(socksProxy, settings);
122+
configureSocket(socksProxy, operationContext, settings);
123123
InetSocketAddress inetSocketAddress = toSocketAddress(serverHost, serverPort);
124124
socksProxy.connect(inetSocketAddress, operationContext.getTimeoutContext().getConnectTimeoutMs());
125125

@@ -141,7 +141,7 @@ private static InetSocketAddress toSocketAddress(final String serverHost, final
141141

142142
private Socket initializeSocketOverSocksProxy(final OperationContext operationContext) throws IOException {
143143
Socket createdSocket = socketFactory.createSocket();
144-
configureSocket(createdSocket, settings);
144+
configureSocket(createdSocket, operationContext, settings);
145145
/*
146146
Wrap the configured socket with SocksSocket to add extra functionality.
147147
Reason for separate steps: We can't directly extend Java 11 methods within 'SocksSocket'

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.net.SocketOption;
2929

3030
import static com.mongodb.internal.connection.SslHelper.configureSslSocket;
31-
import static java.util.concurrent.TimeUnit.MILLISECONDS;
3231

3332
@SuppressWarnings({"unchecked", "rawtypes"})
3433
final class SocketStreamHelper {
@@ -72,15 +71,18 @@ final class SocketStreamHelper {
7271
static void initialize(final OperationContext operationContext, final Socket socket,
7372
final InetSocketAddress inetSocketAddress, final SocketSettings settings,
7473
final SslSettings sslSettings) throws IOException {
75-
configureSocket(socket, settings);
74+
configureSocket(socket, operationContext, settings);
7675
configureSslSocket(socket, sslSettings, inetSocketAddress);
7776
socket.connect(inetSocketAddress, operationContext.getTimeoutContext().getConnectTimeoutMs());
7877
}
7978

80-
static void configureSocket(final Socket socket, final SocketSettings settings) throws SocketException {
79+
static void configureSocket(final Socket socket, final OperationContext operationContext, final SocketSettings settings) throws SocketException {
8180
socket.setTcpNoDelay(true);
82-
socket.setSoTimeout(settings.getReadTimeout(MILLISECONDS));
8381
socket.setKeepAlive(true);
82+
int readTimeoutMS = (int) operationContext.getTimeoutContext().getReadTimeoutMS();
83+
if (readTimeoutMS > 0) {
84+
socket.setSoTimeout(readTimeoutMS);
85+
}
8486

8587
// Adding keep alive options for users of Java 11+. These options will be ignored for older Java versions.
8688
setExtendedSocketOptions(socket);

0 commit comments

Comments
 (0)