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.

shareReplay not cleaning up subscriptions.

See original GitHub issue

RxJS version: 5 or 6

Code to reproduce:

const source = interval(1000).pipe(
  tap(() => console.log('tick'),
  shareReplay(1),
);

const sub = source.subscribe(x => console.log(x));

sub.unsubscribe();

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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:27
  • Comments:26 (7 by maintainers)

github_iconTop GitHub Comments

11reactions
intellixcommented, May 22, 2018

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 publishReplay(1).refCount() above with a timer emitted a weird result when subscribed to with 0 refs (it initially gave the last result and then started fresh)

9reactions
d3lmcommented, May 15, 2018

@ksaldana1 You can use publishReplay(1).refCount() as an alternative for the time being.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Be careful when using shareReplay
The shareReplay operator does not clean up streams when they have not yet completed. In our case, the interval$ keeps emitting values after ......
Read more >
fixing a racecondition on rxjs subscription with shareReplay
The reason shareReplay isn't working for you is because you are applying it to the derived observable, rather than the source.
Read more >
ShareReplay Doesn't Clear Its Replay Buffer
Here we'll demonstrate that the ShareReplay buffer is not cleared when the number of subscribers reaches zero:
Read more >
using shareReplay as cache good or bad practice? : r/Angular2
As such, if you know an observable will complete then you do not need to worry about cleaning up any subscriptions.
Read more >
RxJS: Avoiding memory leaks - Developer's Notes
First, we do not have any issue here because HttpClient from RxJS just ... The shareReplay operator does not clean up observables when...
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 Hashnode Post

No results found