Still problems with unsubscribed observables calling subscriber
See original GitHub issueSo, this is a follow-up to https://github.com/Netflix/RxJava/issues/1407, which I thought had been fixed at first, since the crashes were gone for one particular case, but now I see crashes in our app in different places that are exactly like this. This is with RxJava 0.20-RC3.
Not wanting to jump to conclusions, but as far as I understand the fix that went in we now make sure that as soon as the “downstream” subscriber (not sure if I’m getting the terminology right here) unsubscribes from the sequence, then we unsubscribe immediately. Is that correct?
I found another case where it’s still not working, and it might be related to the use of the cache
operator. Again, as in the other ticket subscription, this is a fragment subscribing to an observable in onViewCreated
, then unsubscribing in onDestroyView
, but still receiving calls to onNext, crashing it, since it will be detached from the window at that point in time.
The only difference I could find to the case where it is now working is the use of the cache
operator. Looking at it, it specifically says in the docs that the subscription returned from it will not unsubscribe from the source observable. Might that be the problem, i.e. will the fix that went in for #1407 not have any effect on this, as it attempted to fix it based on subscriber/subscription interaction?
Issue Analytics
- State:
- Created 9 years ago
- Comments:9 (9 by maintainers)
Thanks for clarifying. That’s indeed a big issue on Android then. The problem is that unsubscribing from an observable as part of a (synchronous) life-cycle callback in Android must ensure that no more messages will arrive in the subscriber, since Android will do its clean up synchronously / immediately:
In the above code snippet, line 2 will always finish before line 1, since
unsubscribe
merely posts a request for unsubscription on the message looper, it does not actually unsubscribe right away. That is a problem, since Android will perform its cleanup before any subscribers get released, and this most likely results in crashes, as these subscribers might attempt to access now defunct views.We can work around this with `OperatorConditionalBinding’ which will ignore “out of band” messages that arrive after Android has released all views. I’m just not a fan of a defensive programming style, so I’m wondering if there’s something that we can do to release subscribers immediately?
Hey @dpsm sorry for not staying on top of this, a bit swamped at the moment. We’re still setting up RxAndroid and I’d like to get the build and project setup hurdles out of our feet first.
Meanwhile I’d like to start by moving all RxAndroid related issues over to that project. Could you repost your proposal here? https://github.com/ReactiveX/RxAndroid/issues/3
Then we can close this out and move the discussion over. Thanks!