Skip to content

Commit

Permalink
Fix concurrency issue when closing sessions in ServerSessionPool
Browse files Browse the repository at this point in the history
  • Loading branch information
jstewart148 authored and jyemin committed Nov 5, 2019
1 parent 6e06585 commit 33b86f7
Showing 1 changed file with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ public void close() {
try {
closing = true;
serverSessionPool.close();
endClosedSessions();

List<BsonDocument> identifiers;
synchronized (this) {
identifiers = new ArrayList<BsonDocument>(closedSessionIdentifiers);
closedSessionIdentifiers.clear();
}
endClosedSessions(identifiers);
} finally {
closed = true;
}
Expand All @@ -111,14 +117,21 @@ private void closeSession(final ServerSessionImpl serverSession) {
return;
}

closedSessionIdentifiers.add(serverSession.getIdentifier());
if (closedSessionIdentifiers.size() == END_SESSIONS_BATCH_SIZE) {
endClosedSessions();
List<BsonDocument> identifiers = null;
synchronized (this) {
closedSessionIdentifiers.add(serverSession.getIdentifier());
if (closedSessionIdentifiers.size() == END_SESSIONS_BATCH_SIZE) {
identifiers = new ArrayList<BsonDocument>(closedSessionIdentifiers);
closedSessionIdentifiers.clear();
}
}
if (identifiers != null) {
endClosedSessions(identifiers);
}
}

private void endClosedSessions() {
if (closedSessionIdentifiers.isEmpty()) {
private void endClosedSessions(final List<BsonDocument> identifiers) {
if (identifiers.isEmpty()) {
return;
}

Expand All @@ -141,12 +154,11 @@ public List<ServerDescription> select(final ClusterDescription clusterDescriptio
}).getConnection();
try {
connection.command("admin",
new BsonDocument("endSessions", new BsonArray(closedSessionIdentifiers)), new NoOpFieldNameValidator(),
new BsonDocument("endSessions", new BsonArray(identifiers)), new NoOpFieldNameValidator(),
ReadPreference.primaryPreferred(), new BsonDocumentCodec(), NoOpSessionContext.INSTANCE);
} catch (MongoException e) {
// ignore exceptions
} finally {
closedSessionIdentifiers.clear();
connection.release();
}
}
Expand Down

0 comments on commit 33b86f7

Please sign in to comment.