-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Unsubscribed observer receives event #3850
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
Comments
This is due to the best-effort way of unsubscribing: PublishSubject, to remain low overhead as possible, doesn't check |
I'm closing this issue due to inactivity. If you have further input on the issue, don't hesitate to reopen this issue or post a new one. |
@akarnokd isn't from point of view of observer pattern, which is rx based on, this situation is incorrect? I mean "subscribed" and "unsubscribed" states from abstraction's point of view differs only by fact if observer continues to receive events. "Subscribed" == "receives events", "unsubscribed" == "not receives events". Is this something to be fixed, or user code needs additional checks to ensure that unsubscribed subscriber will not get events? |
Yes, you should check |
@akarnokd as a consequence this makes "unsubscribe" method non trustworthy. Every time this method is used we need additinally check if subscriber was unsubscrubed when receiving events. Imagine next code: public final class SomeMechanics {
private final Subscription mEventSubscription;
public SomeMechanics(final Observable<Event> eventStream) {
mEventSubscription = eventStream.subscribe(/* ... */);
}
public void stop() {
mEventSubscription.unsubscribe();
}
} This code must check if mEventSubscription is unsubscribed because it can't guarantee the conditions "stop" called from. public final class SomeMechanics {
private final Subscription mEventSubscription;
public SomeMechanics(final Observable<Event> eventStream) {
mEventSubscription
= eventStream.subscribe(onNext -> {
if (mEventSubscription.isUnsubscribed()) return;
// do stuff
} );
}
public void stop() {
mEventSubscription.unsubscribe();
}
} Moreover, that behavior is in contradictory in the description of "unsubscribe" method from documentation: http://reactivex.io/RxJava/javadoc/rx/Subscription.html#unsubscribe() |
See #4225 for an update to |
@akarnokd thanks! |
This code:
Unsubscribes second subscription in the first callback. At the time second callback called it is not subscribed (unsubscribe call completed for it).
It's expected that second callback will not be called, but it is.
Output of that program is
The text was updated successfully, but these errors were encountered: