Ensure connection is closed in monitor before thread completes
If the monitor was in the process of opening a new connection when
it is closed, the monitor's close method won't close the socket
because it hasn't been set yet.  This ensures that the run method
closes it.
@@ -138,6 +138,7 @@ class DefaultServerMonitor implements ServerMonitor {
@Override
public void run() {
ServerDescription currentServerDescription = getConnectingServerDescription(null);
+ try {
while (!isClosed) {
ServerDescription previousServerDescription = currentServerDescription;
currentServerDescription = lookupServerDescription(currentServerDescription);
@@ -168,6 +169,11 @@ class DefaultServerMonitor implements ServerMonitor {
}
waitForNext();
}
+ } finally {
+ if (connection != null) {
+ connection.close();
+ }
+ }
}
private ServerDescription getConnectingServerDescription(final Throwable exception) {
