Skip to content

Commit 59b94e0

Browse files
committed
Make ChangeStreamBatchCursor#close idempotent
JAVA-3442
1 parent 3ac00c4 commit 59b94e0

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

driver-core/src/main/com/mongodb/operation/ChangeStreamBatchCursor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ final class ChangeStreamBatchCursor<T> implements AggregateResponseBatchCursor<T
4040

4141
private AggregateResponseBatchCursor<RawBsonDocument> wrapped;
4242
private BsonDocument resumeToken;
43+
private volatile boolean closed;
4344

4445
ChangeStreamBatchCursor(final ChangeStreamOperation<T> changeStreamOperation,
4546
final AggregateResponseBatchCursor<RawBsonDocument> wrapped,
@@ -91,8 +92,11 @@ public List<T> apply(final AggregateResponseBatchCursor<RawBsonDocument> queryBa
9192

9293
@Override
9394
public void close() {
94-
wrapped.close();
95-
binding.release();
95+
if (!closed) {
96+
closed = true;
97+
wrapped.close();
98+
binding.release();
99+
}
96100
}
97101

98102
@Override

driver-core/src/test/unit/com/mongodb/operation/ChangeStreamBatchCursorSpecification.groovy

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ class ChangeStreamBatchCursorSpecification extends Specification {
5757

5858
then:
5959
1 * wrapped.close()
60+
61+
when:
62+
cursor.close()
63+
64+
then:
65+
0 * wrapped.close()
6066
}
6167

6268
}

0 commit comments

Comments
 (0)