-
Notifications
You must be signed in to change notification settings - Fork 3k
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
shareReplay not cleaning up subscriptions. #3336
Comments
Explored this bug a bit, it seems that Teardown logic for shareReplay is never triggered at all, because current implementation of Observable.prototype.subscribe doesn't do anything about function returned from
I guess best bet here is to refactor shareReplay to a class with own _next, _error & _unsubscribe methods Other fix which is more hacky is to modify |
Also as far as I see behavior described in this bug contradicts with unit test
because we can prevent the source observable from restarting only by maintaining the subscription to it. In other words shareReplay returns a hot observable and currently upon reaching 0 subscriptions it is still hot. |
Added a pr fixing not working teardown logic by injecting code into sink's unsubscribe |
Any updates on this? |
I ran into this issue today in our React-Native mobile application. We are using
where extractPath is
@benlesh Do you have a suggested workaround for the time being? Is there something more appropriate than the |
@ksaldana1 You can use |
I remember this was reported and discussed some time ago in this issue #3127 |
This workaround was given to me in gitter as I cried that my teardown was never run: const timer$ = Rx.Observable.timer(0, 500)
.multicast(x => new Rx.ReplaySubject(1))
.refCount(); The |
I'm having the same issue as @intellix with Does this issue (recycling the |
@jbedard What you are seeing is the intended behaviour. If you want a new subject to be created and subscribed to the source when the reference count hits zero, you need to pass a factory to |
That means the "weird result" described above is correct? I guess I've been using the wrong operators all this time 🤔 However this bug is essentially making |
@jbedard Yes, the "weird result" was the expected behaviour. And, no, they aren't the same.
|
I see. Thanks for the info 👍 |
A good explanation on Note that on the official RxJS v 4 page: Which contracticts with the unit test of the V5 and V6 |
@benlesh "this should be an easy fix" 😄 |
There are two workarounds posted here, but I'm not familiar enough with the multicast operators to know the difference. Is there a workaround that provides exactly the desired behaviour of |
@benlesh It's clear from this issue that when the ref count drops to zero, it should unsubscribe from the source. However, there are other aspects that are unclear. Precisely how is it supposed to behave? In particular:
I'd like to fix this, as I'm seeing people have to resort to unusual solutions - like placing |
Okay... so here's the problem, overall. I've seen a split on what the behavior should be. Given what this operator is used for, which is "hey, this thing is underway, and I want to share it's results with everyone, including those that are late to the game"... The problem is, the semantics are the question here. If the whole point of But if you're using it for other scenarios (such as endless streaming data), I can see why you'd want to clean up on unsubscription to ref count 0. It might be best to put this behavior behind a flag which can be passed as an argument to |
Either way, I think it should be consistent with regards to the behavior when ref count hits zero. That is to say:
|
I think you should deprecate the current
|
I think it would be better not to deprecate |
In my opinion is |
I totally agree, therefore I think the default behavior of |
maybe something like this? |
Just a personal opinion here, but at glance (judging by the operator name) I was expecting both teardown and recycle be part of
Would it be a good argument to say that a stream produced by an operator should not introduce any discrepancies in its consumption for cases where the source errors or successfully completes. Either cache in both cases or not cache in both cases? |
Use `publishReplay` and `refCount` instead of `shareReplay` to avoid memory leaks. See ReactiveX/rxjs#3336
RxJS version: 5 or 6
Code to reproduce:
Expected behavior:
It should never log "tick"
Actual behavior:
It TOTALLY logs "tick".
Additional information:
shareReplay
should definitely not recycle the underlying ReplaySubject when refCount drops to 0 due to unsubscription. However, it should end the subscription, which it's currently not doing, because I'm a dolt.It should be an easy fix.
The text was updated successfully, but these errors were encountered: