-
Notifications
You must be signed in to change notification settings - Fork 7.6k
ScheduledObserver doesn't Guaranty Ordering #233
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 used as a pass-thru for For example, if I need to ensure ordering then I probably don't sent it to a thread-pool, but send it to a single thread (such as the UI thread). If I choose to send something to a thread-pool for execution on multiple threads why should I expect the events to all be processed and returned in order when I am specifically injecting concurrency. I can't find anything in the Rx Design Guidelines that dictates that order should be retained and it's not at all guaranteed by all operators (for example |
Quote from the 4.2. Assume observer instances are called in a serialized fashion
If ordering is not guarantied we might observe |
But I don't read that to mean ordered. We must ensure the contract of onNext|onCompleted|onError but I don't see how we need to or should ensure that execution of onNext events arrive in the same order once they've been thrown off on a thread-pool and multiple threads. Do you think otherwise? |
You can take a look at an Observer wrapper we used in production in our internal version before open-sourcing that allows concurrent onNext executions but ensures the onCompleted/onError contract: https://gist.github.com/benjchristensen/5400653 We were more lenient in our internal version about allowing concurrent execution of onNext since we forced everything to be functional and without state (which I still actually prefer and think onNext should be allowed to be concurrent) but when we open sourced I removed this since it is against the contract of official Rx and do not allow concurrent onNext execution. However, it shows the principle of how onNext calls could interleave (be out of order) but still be serialized while ensuring onCompleted/onError occur only at the end after all onNext calls are completed. |
Thinking about this more ... the ordering still isn't the part that concerns me (though maybe it should) but the real issue here is that each onNext/onCompleted/onError could be triggered on a separate thread. We need to not only ensure only one of those is running at a time but then we ensure visibility/memory consistency. This means we are basically forced into wrapping this in Without that I can't see |
I thought if |
Using only It is a good question whether memory consistency/visibility should be responsibility of the library or of the observer. |
It would guarantee ordering if the synchronization happens BEFORE putting the event on to the Scheduler since they would enter and leave the Scheduler in the same order they hit the SynchronizedObserver. It should only be permitted to schedule one event at a time. It can't schedule more than one at a time because then you can no longer prevent them from running concurrently. |
ScheduledObserver
should be reimplemented the way that it guaranties ordering of the events.Right now this test prints values in different ordering.
The text was updated successfully, but these errors were encountered: