Skip to content

1.x: fix premature cleanup in AsyncOnSubscribe when the last Observable is still running #5430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/main/java/rx/observables/AsyncOnSubscribe.java
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,11 @@ boolean tryEmit(long n) {
onNextCalled = false;
expectedDelivery = n;
nextIteration(n);

if (hasTerminated || isUnsubscribed()) {

//hasTerminated will be true when onCompleted was already emitted from the request callback
//even if the the observer has not seen onCompleted from the requested observable,
//so we should not clean up while there are active subscriptions
if (hasTerminated && !subscriptions.hasSubscriptions() || isUnsubscribed()) {
cleanup();
return true;
}
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/rx/observables/AsyncOnSubscribeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -483,4 +483,38 @@ public Integer call(Integer state, Long requested, Observer<Observable<? extends

subscriber.assertNotCompleted();
}

@Test
public void testMergeDelayedWithScalar() {
final TestScheduler scheduler = new TestScheduler();
Observable<Integer> os = Observable.create(AsyncOnSubscribe.<Integer, Integer> createStateful(
new Func0<Integer>() {

@Override
public Integer call() {
return 0;
}

},
new Func3<Integer, Long, Observer<Observable<? extends Integer>>, Integer>() {

@Override
public Integer call(Integer state, Long requested, Observer<Observable<? extends Integer>> emitter) {
if (state == 0) {
emitter.onNext(Observable.range(0,100).delay(1, TimeUnit.SECONDS, scheduler));
} else {
emitter.onCompleted();
}
return state + 1;
}

}));

TestSubscriber<Object> ts = new TestSubscriber<Object>();
os.mergeWith(Observable.just(0)).subscribe(ts);
scheduler.advanceTimeBy(1, TimeUnit.HOURS);
ts.assertCompleted();
ts.assertValueCount(101);
}

}