Skip to content

Commit b0c8ec2

Browse files
jstewart148jyemin
authored andcommitted
Authentication failures against a mongos causes connection leaks after running MongoClient.close
JAVA-3631
1 parent 022d731 commit b0c8ec2

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

driver-core/src/main/com/mongodb/internal/session/ServerSessionPool.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,25 +141,29 @@ private void endClosedSessions(final List<BsonDocument> identifiers) {
141141
return;
142142
}
143143

144-
Connection connection = cluster.selectServer(new ServerSelector() {
145-
@Override
146-
public List<ServerDescription> select(final ClusterDescription clusterDescription) {
147-
for (ServerDescription cur : clusterDescription.getServerDescriptions()) {
148-
if (cur.getAddress().equals(primaryPreferred.get(0).getAddress())) {
149-
return Collections.singletonList(cur);
144+
Connection connection = null;
145+
try {
146+
connection = cluster.selectServer(new ServerSelector() {
147+
@Override
148+
public List<ServerDescription> select(final ClusterDescription clusterDescription) {
149+
for (ServerDescription cur : clusterDescription.getServerDescriptions()) {
150+
if (cur.getAddress().equals(primaryPreferred.get(0).getAddress())) {
151+
return Collections.singletonList(cur);
152+
}
150153
}
154+
return Collections.emptyList();
151155
}
152-
return Collections.emptyList();
153-
}
154-
}).getConnection();
155-
try {
156+
}).getConnection();
157+
156158
connection.command("admin",
157159
new BsonDocument("endSessions", new BsonArray(identifiers)), new NoOpFieldNameValidator(),
158160
ReadPreference.primaryPreferred(), new BsonDocumentCodec(), NoOpSessionContext.INSTANCE);
159161
} catch (MongoException e) {
160162
// ignore exceptions
161163
} finally {
162-
connection.release();
164+
if (connection != null) {
165+
connection.release();
166+
}
163167
}
164168
}
165169

0 commit comments

Comments
 (0)