-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
1.x Take operator can emit more values than its limit #3346
Comments
Hi and thanks for discovering this. The behavior is not intentional. We are generally not prepared for synchronous reentry such as this. Solving it is a bit tricky because a delicate behavior needs to be ensured right at the limit. Here is how I'd fix this:
@Override
public void onNext(T i) {
if (!isUnsubscribed()) {
boolean stop = ++count >= limit;
if (stop) {
if (this.stop) {
return;
}
this.stop = true;
}
child.onNext(i);
if (stop && !completed) {
completed = true;
try {
child.onCompleted();
} finally {
unsubscribe();
}
}
}
} This will prevent a recursive onNext call to be delivered after the limit. |
Thanks for the response! I think alternatively you could do: if (!isUnsubscribed() && (count++ < limit)) {
boolean stop = count >= limit;
child.onNext(i);
if (stop && !completed) {
completed = true;
try {
child.onCompleted();
} finally {
unsubscribe();
}
}
} Which only changes the outer conditional and the computation of |
Looks even better, go ahead with the PR. |
@mgp Are you going to post a PR against 1.x? |
Discovered by @mgp in ReactiveX#3346 and using his supplied fix. I've already applied it to NbpObservable's take this Monday so all that's left was the unit test.
Closing via #3384. |
This can happen if a
doOnNext
action downstream can cause the observable upstream to emit another value. Here's a test that demonstrates this behavior:I first noticed this in a project that uses RxJava 1.1.10. The
onNext
implementation ofOperatorTake
has changed a bit in master. Before I go ahead and submit a PR that fixes this behavior, I just wanted to ensure that it wasn't deliberate? Thanks!The text was updated successfully, but these errors were encountered: