Skip to content

Commit

Permalink
3.x: Fix ObservableSwitchMap NPE due to cancel race
Browse files Browse the repository at this point in the history
  • Loading branch information
akarnokd committed Aug 29, 2023
1 parent a57ad01 commit eeef8d6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,10 @@ public void onSubscribe(Disposable d) {

@Override
public void onNext(R t) {
if (index == parent.unique) {
SimpleQueue<R> q = queue;
if (index == parent.unique && q != null) {
if (t != null) {
queue.offer(t);
q.offer(t);
}
parent.drain();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1377,4 +1377,19 @@ Flowable<Integer> createFlowable(AtomicInteger inner) {
inner.incrementAndGet();
});
}

@Test
public void innerOnSubscribeOuterCancelRace() {
TestSubscriber<Integer> ts = new TestSubscriber<Integer>();

Flowable.just(1)
.hide()
.switchMap(v -> Flowable.just(1)
.doOnSubscribe(d -> ts.cancel())
.scan(1, (a, b) -> a)
)
.subscribe(ts);

ts.assertEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1438,4 +1438,19 @@ Observable<Integer> createObservable(AtomicInteger inner) {
inner.incrementAndGet();
});
}

@Test
public void innerOnSubscribeOuterCancelRace() {
TestObserver<Integer> to = new TestObserver<Integer>();

Observable.just(1)
.hide()
.switchMap(v -> Observable.just(1)
.doOnSubscribe(d -> to.dispose())
.scan(1, (a, b) -> a)
)
.subscribe(to);

to.assertEmpty();
}
}

0 comments on commit eeef8d6

Please sign in to comment.