Skip to content

Commit

Permalink
Update BlockingFlowableIterable.onNext() to set error before cancel (#…
Browse files Browse the repository at this point in the history
…7789)

To avoid race with hasNext(), which checks for cancel first before checking for error. For example, in the following case, hasNext() may return false to the caller, making the caller assume the iterable finished successfully.
1. onNext() called cancel
2. hasNext() found the iterable is cancelled
3. hasNext() found that error is null thus returned false to the caller, without throwing the error
4. onNext() set error
  • Loading branch information
kaioni17 authored Nov 21, 2024
1 parent e46ea36 commit 338f2a1
Showing 1 changed file with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,12 @@ public void onSubscribe(Subscription s) {
@Override
public void onNext(T t) {
if (!queue.offer(t)) {
// Error must be set first before calling cancel to avoid race
// with hasNext(), which checks for cancel first before checking
// for error.
error = new QueueOverflowException();
SubscriptionHelper.cancel(this);

onError(new QueueOverflowException());
onComplete();
} else {
signalConsumer();
}
Expand Down

0 comments on commit 338f2a1

Please sign in to comment.