question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Still problems with unsubscribed observables calling subscriber

See original GitHub issue

So, 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:closed
  • Created 9 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
mttkaycommented, Aug 23, 2014

When going over async boundaries, such as observeOn there can be race conditions that allow onNext/onError events to emit to the Subscriber after it has unsubscribed. It is “best effort” across threads/networks to shut down and some items may still emit before it shuts down.

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:

// An Activity is a screen context object in the presentation layer of an Android app
class MyActivity extends Activity {

  // this callback signals that the current screen context, and all its views, will be released.
  // this is then where an RxAndroid app would release its UI subscribers
  void onDestroy() {
     subscription.unsubscribe(); // this is asynchronous
     super.onDestroy(); // this is synchronous
  }

}

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?

0reactions
mttkaycommented, Sep 12, 2014

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

6 Ways to Unsubscribe from Observables in Angular
1. Use the unsubscribe method ... A Subscription essentially just has an unsubscribe() function to release resources or cancel Observable ...
Read more >
RxJS: Don't Unsubscribe - Ben Lesh - Medium
Developers will invariably make 3 HTTP requests with an Observable, and then keep 3 subscription objects that they're going to call when some...
Read more >
Angular/RxJS When should I unsubscribe from `Subscription`
In most cases we will not need to explicitly call the unsubscribe method unless we want to cancel early or our Observable has...
Read more >
When should I unsubscribe my Subscriptions in Angular?
First things first: Never subscribe! If there was ever a best practice to follow with observables in Angular, this would be it. If...
Read more >
Best Practices for Managing RxJS Subscriptions - This Dot Labs
This is great; however, when working with RxJS, you will likely have more than one subscription. Calling unsubscribe for each of them could...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found