Skip to content

Commit 0cfaf89

Browse files
authored
2.x: fix switchMap bad cancellation (#4513)
1 parent 939f172 commit 0cfaf89

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

Diff for: src/main/java/io/reactivex/internal/operators/flowable/FlowableSwitchMap.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ public void request(long n) {
175175
public void cancel() {
176176
if (!cancelled) {
177177
cancelled = true;
178+
s.cancel();
178179

179180
disposeInner();
180181
}
@@ -186,7 +187,7 @@ void disposeInner() {
186187
if (a != CANCELLED) {
187188
a = active.getAndSet((SwitchMapInnerSubscriber<T, R>)CANCELLED);
188189
if (a != CANCELLED && a != null) {
189-
s.cancel();
190+
a.cancel();
190191
}
191192
}
192193
}

Diff for: src/main/java/io/reactivex/internal/operators/observable/ObservableSwitchMap.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public void onComplete() {
160160
public void dispose() {
161161
if (!cancelled) {
162162
cancelled = true;
163-
163+
s.dispose();
164164
disposeInner();
165165
}
166166
}
@@ -176,7 +176,7 @@ void disposeInner() {
176176
if (a != CANCELLED) {
177177
a = active.getAndSet((SwitchMapInnerSubscriber<T, R>)CANCELLED);
178178
if (a != CANCELLED && a != null) {
179-
s.dispose();
179+
a.cancel();
180180
}
181181
}
182182
}

Diff for: src/test/java/io/reactivex/internal/operators/flowable/FlowableSwitchTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -797,4 +797,18 @@ public Publisher<Integer> apply(Object v) throws Exception {
797797

798798
}
799799

800+
@Test
801+
public void switchMapInnerCancelled() {
802+
PublishProcessor<Integer> pp = PublishProcessor.create();
803+
804+
TestSubscriber<Integer> ts = Flowable.just(1)
805+
.switchMap(Functions.justFunction(pp))
806+
.test();
807+
808+
assertTrue(pp.hasSubscribers());
809+
810+
ts.cancel();
811+
812+
assertFalse(pp.hasSubscribers());
813+
}
800814
}

Diff for: src/test/java/io/reactivex/internal/operators/observable/ObservableSwitchTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -607,4 +607,20 @@ public ObservableSource<Integer> apply(Object v) throws Exception {
607607

608608
}
609609

610+
611+
@Test
612+
public void switchMapInnerCancelled() {
613+
PublishSubject<Integer> pp = PublishSubject.create();
614+
615+
TestObserver<Integer> ts = Observable.just(1)
616+
.switchMap(Functions.justFunction(pp))
617+
.test();
618+
619+
assertTrue(pp.hasObservers());
620+
621+
ts.cancel();
622+
623+
assertFalse(pp.hasObservers());
624+
}
625+
610626
}

0 commit comments

Comments
 (0)