-
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 with refCount stops working #6760
Comments
Removing I had run in to something similar in my own project. So I ended up writing a simple operator that works like
Replace |
Confirmed I am also seeing behavior where Using I feel like I am missing something basic here, but something definitely changed in the functionality between 6.x and 7.current. For me there seems to be a commonality of using |
I think this might be related: https://github.com/ReactiveX/rxjs/pull/5634/files#diff-44fa2a928f593fdd8b981dac7737a65f6b0ddc844300af18a539c59b683b5467R130 Looks like shareReplay now calls Am I missing something? |
I'm experiencing this as well. It happens when you subscribe multiple times, then unsubscribe and resubscribe again (is this called a reentrant subscription?). The code in the original post is a good example since multiple things subscribe to Specifically, it happens if the code emits and unsubscribes immediately. If it emits after the first subscription, things seem to work as expected. It looks like it's a race condition in the setup code and the reset at refcount 0 logic. Excuse my atrocious console logging, but this is what I see when a subscription is made and the source emits after a delay: Whereas this is what happens if the source emits immediately: As you can see, if a value emits immediately and both subscriptions unsubscribe (as you might get with This started happening in 7 because of this new code, which prevents excess subscriptions to the source observable. There was also some rearranging of the code which may have prevented it in 6, I'm not sure. rxjs/src/internal/operators/share.ts Lines 214 to 220 in 47fa8d5
Flipping the order of the code so the source subscription check runs before the refCount decrement subscription seems to fix the issue for me, but there are some comments explicitly wanting to do it in the other order, so I don't know if that solution will work. |
@henry-alakazhang Do you have a code reproduction you can share that demonstrates the issue? It would probably help the devs make sure it gets tested and fixed! (I haven't been able to reproduce this outside of my app - sporadically.) |
I haven't tried to reproduce it outside my app either, but the reproduction in the issue itself should demonstrate the issue fine.
I can simplify the reproduction a little more: // Apply `replay` to an observable which emits immediately once
const shared$ = new BehaviorSubject('6.4.0').pipe(
shareReplay({ refCount: true, bufferSize: 1 })
);
// Subscribe to it multiple times simultaneously, then unsubscribe simultaneously
combineLatest([shared$, shared$]).pipe(
take(1)
).toPromise();
// It doesn't emit again.
const lateSub = shared$.subscribe((value) => console.log('late:', value)); This example specifically is minimal to the point of silliness (combineLatest on the same observable) but if you imagine doing a Note that also: // If we reset it (via refCount = 0), the state gets fixed.
lateSub.unsubscribe();
shared$.subscribe((value) => console.log('later:', value)); // emits on both 7.5.1 and 6.4.0 Stackblitz 6.4.0: https://stackblitz.com/edit/q7u9jg-xxev1z?file=index.ts |
Hey guys, Do you have any update on this? |
@benlesh it seems like part of the issue (or a tangential issue) is, with the refactor of For example, tl;dr |
@benlesh bumping this ^ |
Describe the bug
When subscribe to shared observable multiple times internal connection to the source observable gets broken. It stops emit values on subscribe.
Expected behavior
Expected output:
Actual output
Reproduction code
Reproduction URL
7.5.1
6.4.0
Version
7.5.1
Environment
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: