Repository navigation
zip(o1, o2) doesn't unsubscribe from o1 #3960
Description
Activity
Zip uses a prefetch of 128 elements by default. Since both sources are synchronous, the second case takes all 4 from the first source, including completion, then moves onto
o2which first emits 1, then its completion completes the whole sequence. Thus, you see both doOnCompleted executed.The first case,
o1signals 1 and completion, then onceo2signals its 1, it is knowno2won't ever signal more values, thuso2gets unsubscribed and never allowed to run to completion.Both are the expected behavior. If you need resource cleanup associated with a sequence, you can't rely on doOnCompleted as sequences may get unsubscribed before that.
I see, but isn't this implementation details?
It is the explanation for what you experienced. The
zipjavadoc has this clause:The resulting
Observable<R>returned fromzipwill invokeObserver#onNext
as many times as the number ofonNextinvocations of the source Observable that emits the fewest
items.Since
zip's behavior seems to be confusing, maybe we could add a clause thatzipcompletes eagerly if one of the sources emits fewer elements than the others and unsubscribes the rest.I agree that docs should be updated. Although the new clause you suggested needs to be more accurate:
- Examples in this issue contradict it
- If source observables emit equal number of elements (and not fewer), some (which? this one really bothers me) are still going to be unsubscribed.
See the proposed documentation changes in #3981.
Thanks, now I know what to expect from
zip. The current implementation still feels unpolished to me, though.- added a commit that references this issue
on Jun 2, 2016
A simple example, looks ok
Output:
Now swap o1 and o2
Output: