Skip to content

Commit f346d2f

Browse files
committed
Ensure that socket is closed on handshake failure
JAVA-3680
1 parent 8b63647 commit f346d2f

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

driver-core/src/main/com/mongodb/connection/netty/NettyStream.java

+21-13
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public ServerAddress getAddress() {
270270
}
271271

272272
@Override
273-
public void close() {
273+
public synchronized void close() {
274274
isClosed = true;
275275
if (channel != null) {
276276
channel.close();
@@ -396,20 +396,28 @@ private class OpenChannelFutureListener implements ChannelFutureListener {
396396

397397
@Override
398398
public void operationComplete(final ChannelFuture future) {
399-
if (future.isSuccess()) {
400-
channel = channelFuture.channel();
401-
channel.closeFuture().addListener(new ChannelFutureListener() {
402-
@Override
403-
public void operationComplete(final ChannelFuture future) {
404-
handleReadResponse(null, new IOException("The connection to the server was closed"));
399+
synchronized (NettyStream.this) {
400+
if (future.isSuccess()) {
401+
if (isClosed) {
402+
channelFuture.channel().close();
403+
} else {
404+
channel = channelFuture.channel();
405+
channel.closeFuture().addListener(new ChannelFutureListener() {
406+
@Override
407+
public void operationComplete(final ChannelFuture future) {
408+
handleReadResponse(null, new IOException("The connection to the server was closed"));
409+
}
410+
});
405411
}
406-
});
407-
handler.completed(null);
408-
} else {
409-
if (socketAddressQueue.isEmpty()) {
410-
handler.failed(new MongoSocketOpenException("Exception opening socket", getAddress(), future.cause()));
412+
handler.completed(null);
411413
} else {
412-
initializeChannel(handler, socketAddressQueue);
414+
if (isClosed) {
415+
handler.completed(null);
416+
} else if (socketAddressQueue.isEmpty()) {
417+
handler.failed(new MongoSocketOpenException("Exception opening socket", getAddress(), future.cause()));
418+
} else {
419+
initializeChannel(handler, socketAddressQueue);
420+
}
413421
}
414422
}
415423
}

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

+17-7
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,17 @@ public BufferProvider getBufferProvider() {
6969
return bufferProvider;
7070
}
7171

72-
public ExtendedAsynchronousByteChannel getChannel() {
72+
public synchronized ExtendedAsynchronousByteChannel getChannel() {
7373
return channel;
7474
}
7575

76-
protected void setChannel(final ExtendedAsynchronousByteChannel channel) {
76+
protected synchronized void setChannel(final ExtendedAsynchronousByteChannel channel) {
7777
isTrue("current channel is null", this.channel == null);
78-
this.channel = channel;
78+
if (isClosed) {
79+
closeChannel(channel);
80+
} else {
81+
this.channel = channel;
82+
}
7983
}
8084

8185
@Override
@@ -133,16 +137,22 @@ public ServerAddress getAddress() {
133137
}
134138

135139
@Override
136-
public void close() {
140+
public synchronized void close() {
141+
isClosed = true;
142+
try {
143+
closeChannel(channel);
144+
} finally {
145+
channel = null;
146+
}
147+
}
148+
149+
private void closeChannel(final ExtendedAsynchronousByteChannel channel) {
137150
try {
138151
if (channel != null) {
139152
channel.close();
140153
}
141154
} catch (IOException e) {
142155
// ignore
143-
} finally {
144-
channel = null;
145-
isClosed = true;
146156
}
147157
}
148158

0 commit comments

Comments
 (0)