Skip to content

Commit 3c98211

Browse files
committed
Ensure that socket is closed on handshake failure
JAVA-3680
1 parent cfc9037 commit 3c98211

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

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ public ServerAddress getAddress() {
269269
}
270270

271271
@Override
272-
public void close() {
272+
public synchronized void close() {
273273
isClosed = true;
274274
if (channel != null) {
275275
channel.close();
@@ -395,20 +395,28 @@ private class OpenChannelFutureListener implements ChannelFutureListener {
395395

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

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

Lines changed: 17 additions & 7 deletions
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)