Skip to content

Commit f015023

Browse files
committed
Avoid calling batch cursor when the cursor is closed
JAVA-3710
1 parent 717ac88 commit f015023

File tree

5 files changed

+14
-7
lines changed

5 files changed

+14
-7
lines changed

driver-async/src/main/com/mongodb/async/client/AbstractSubscription.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ public void request(final long n) {
6969
if (n < 1) {
7070
throw new IllegalArgumentException("Number requested must be > 0: " + n);
7171
}
72+
if (isTerminated()) {
73+
return;
74+
}
7275

7376
boolean requestData = false;
7477
synchronized (this) {
@@ -132,7 +135,7 @@ void onError(final Throwable t) {
132135
throw MongoException.fromThrowableNonNull(t1);
133136
}
134137
} else {
135-
throw MongoException.fromThrowableNonNull(t);
138+
throw new MongoException("Subscription has already been terminated", t);
136139
}
137140
}
138141

driver-async/src/main/com/mongodb/async/client/MongoIterableSubscription.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void postTerminate() {
7878
void requestMoreData() {
7979
boolean mustRead = false;
8080
synchronized (this) {
81-
if (!isReading && !isTerminated() && batchCursor != null) {
81+
if (!isReading && !isTerminated() && batchCursor != null && !batchCursor.isClosed()) {
8282
isReading = true;
8383
mustRead = true;
8484
}

driver-async/src/test/unit/com/mongodb/async/client/FlatteningSingleResultCallbackSubscriptionSpecification.groovy

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ class FlatteningSingleResultCallbackSubscriptionSpecification extends Specificat
279279

280280
then:
281281
def ex = thrown(MongoException)
282-
ex.message == 'exception calling onComplete'
282+
ex.message == 'Subscription has already been terminated'
283+
ex.cause.cause.message == 'exception calling onComplete'
283284
observer.assertTerminalEvent()
284285
observer.assertNoErrors()
285286
}
@@ -312,7 +313,8 @@ class FlatteningSingleResultCallbackSubscriptionSpecification extends Specificat
312313

313314
then:
314315
def ex = thrown(MongoException)
315-
ex.message == 'exception calling onError'
316+
ex.message == 'Subscription has already been terminated'
317+
ex.cause.cause.message == 'exception calling onError'
316318
observer.assertTerminalEvent()
317319
observer.assertErrored()
318320
}

driver-async/src/test/unit/com/mongodb/async/client/MongoIterableSubscriptionSpecification.groovy

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ class MongoIterableSubscriptionSpecification extends Specification {
464464

465465
then:
466466
def ex = thrown(MongoException)
467-
ex.message == 'exception calling onComplete'
467+
ex.message == 'Subscription has already been terminated'
468468
observer.assertTerminalEvent()
469469
observer.assertNoErrors()
470470
}
@@ -497,7 +497,8 @@ class MongoIterableSubscriptionSpecification extends Specification {
497497

498498
then:
499499
def ex = thrown(MongoException)
500-
ex.message == 'exception calling onError'
500+
ex.message == 'Subscription has already been terminated'
501+
ex.cause.cause.message == 'exception calling onError'
501502
observer.assertTerminalEvent()
502503
observer.assertErrored()
503504
}

driver-async/src/test/unit/com/mongodb/async/client/SingleResultCallbackSubscriptionSpecification.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ class SingleResultCallbackSubscriptionSpecification extends Specification {
318318
def ex = thrown(MongoException)
319319
observer.assertNoErrors()
320320
observer.assertTerminalEvent()
321-
ex.message == 'exception calling onComplete'
321+
ex.message == 'Subscription has already been terminated'
322+
ex.cause.cause.message == 'exception calling onComplete'
322323
}
323324

324325
def 'should throw the exception if calling onError raises one'() {

0 commit comments

Comments
 (0)