Skip to content

Commit

Permalink
3.x: Fix ObservableSwitchMap NPE due to cancel race (#7597)
Browse files Browse the repository at this point in the history
  • Loading branch information
akarnokd committed Aug 29, 2023
1 parent 13a8d83 commit 542374c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
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
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();
}
}
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 542374c

Please sign in to comment.