Skip to content
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

Observable.using() disposes resource before upstream #6347

Closed
AterIgnis opened this issue Dec 19, 2018 · 5 comments
Closed

Observable.using() disposes resource before upstream #6347

AterIgnis opened this issue Dec 19, 2018 · 5 comments

Comments

@AterIgnis
Copy link

I've expected that Observable.using flow is:

  • create resource
  • creates resource using observable, subscribe
  • finish/cancel resource-using observable
  • dispose resource

But for me actual is sometimes

  • create resource
  • creates resource using observable, subscribe
  • dispose resource
  • finish/cancel resource-using observable

As I understand, using 'eager' makes second flow consistent,but i need first behavior, which is not observed on dispose() call - resource is disposed first always, then observable. What is correct way to make Observable which is not eager even on dispose?

@akarnokd
Copy link
Member

Indeed eagerness is not affecting the dispose order, and I'm not certain if it should or not. /cc @artem-zinnatullin @davidmoten @vanniktech

The shortest workaround would be for you to take the operator by source and have a local version with the desired order of disposal.

@vanniktech
Copy link
Collaborator

I'm not sure either. Haven't really used using.

@AterIgnis
Copy link
Author

My understanding was that resource is allocated firtst and free'd last, and i used it in Android to wrap some of APIs, like 'call register/start', 'handle results as observable', 'call unregister/stop'. But as on dispose Using first disposes resource (in my case - calls 'unregister/stop') it ended up in some kind of 'race conditon': observable chain is partially disposed, 'stop' is called, but as stop can be called by Android itself - it emits onError, which is not handled already, thus app crashes with undeliverable exception.
Second cause of issue was because i used SwitchMap on this Using, which first disposes upstream and then inner subscription, but that was fixed by moving switchMap inside of Using observable (second argument).

Currently i've went around this by copying code from Using and making it always dispose resource last, as was suggested.

@akarnokd
Copy link
Member

This would be a change for 2.x with unexpected consequences so instead, this should be changed in 3.x.
So eager disposes the resource first, non-eager disposes resource last.

@akarnokd
Copy link
Member

Closing via #6534.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants