Skip to content

Commit

Permalink
fix: internalStream should handle duplicate onCompletes. (#1523)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsannas committed Jan 11, 2024
1 parent 4dd720a commit b3067d7
Showing 1 changed file with 8 additions and 11 deletions.
Expand Up @@ -1477,8 +1477,6 @@ public void stream(@Nonnull final ApiStreamObserver<DocumentSnapshot> responseOb

internalStream(
new QuerySnapshotObserver() {
boolean hasCompleted = false;

@Override
public void onNext(QueryDocumentSnapshot documentSnapshot) {
responseObserver.onNext(documentSnapshot);
Expand All @@ -1491,8 +1489,6 @@ public void onError(Throwable throwable) {

@Override
public void onCompleted() {
if (hasCompleted) return;
hasCompleted = true;
responseObserver.onCompleted();
}
},
Expand Down Expand Up @@ -1660,6 +1656,10 @@ private void internalStream(
boolean firstResponse;
int numDocuments;

// The stream's `onComplete()` could be called more than once,
// this flag makes sure only the first one is actually processed.
boolean hasCompleted = false;

@Override
public void onStart(StreamController streamController) {}

Expand Down Expand Up @@ -1695,7 +1695,7 @@ public void onResponse(RunQueryResponse response) {
"Firestore.Query: Completed",
ImmutableMap.of(
"numDocuments", AttributeValue.longAttributeValue(numDocuments)));
documentObserver.onCompleted(readTime);
onComplete();
}
}

Expand Down Expand Up @@ -1723,6 +1723,9 @@ public void onError(Throwable throwable) {

@Override
public void onComplete() {
if (hasCompleted) return;
hasCompleted = true;

Tracing.getTracer()
.getCurrentSpan()
.addAnnotation(
Expand Down Expand Up @@ -1788,9 +1791,6 @@ ApiFuture<QuerySnapshot> get(@Nullable ByteString transactionId) {
internalStream(
new QuerySnapshotObserver() {
final List<QueryDocumentSnapshot> documentSnapshots = new ArrayList<>();
// The stream's onCompleted could be called more than once,
// this flag makes sure only the first one is actually processed.
boolean hasCompleted = false;

@Override
public void onNext(QueryDocumentSnapshot documentSnapshot) {
Expand All @@ -1804,9 +1804,6 @@ public void onError(Throwable throwable) {

@Override
public void onCompleted() {
if (hasCompleted) return;
hasCompleted = true;

// The results for limitToLast queries need to be flipped since we reversed the
// ordering constraints before sending the query to the backend.
List<QueryDocumentSnapshot> resultView =
Expand Down

0 comments on commit b3067d7

Please sign in to comment.