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.

SafeSubscriber does not unsubscribe itself

See original GitHub issue

When subscribing to Observable with Partial Observer, rxjs creates SafeSubscriber, but on unsubscribe it does not unsubscribe itself. Moreover if a closure is passed to next/error callback of partial observer, this leads to memory leaks, as consumers cannot be freed.

See the code (pure JS).

RxJS version: 5.4.1

Code to reproduce:

via gist: https://gist.github.com/czterocyty/d4ad5dea55316c9a734c64f378975f04

Expected behavior:

I think SafeSubscriber#unsubscribe should be called too upon unsubscription.

Test should pass:


  it('upon subscription, the destination SafeSubscriber should be unsubscribed too', () => {
    const observer = {
      next: function () { /*noop*/ },
      error: function (e) { throw e; }
    };

    const sub = new Subscriber(observer);

    const destination = sub['destination'];
    const destUnsubscribe = destination['unsubscribe'] = sinon.spy();
    const destUnsubscribeProtected = destination['_unsubscribe'] = sinon.spy();

    sub.unsubscribe();

    expect(sub.closed).to.eq(true);
    expect(destination.closed).to.eq(true);

    expect(destUnsubscribe).have.been.called;
    expect(destUnsubscribeProtected).have.been.called;
  });

Actual behavior:

It is not when passing partial observer into Subscriber.

Additional information:

After playing around with buttons, check JS heap and not-freed Foo instance due to handing closures in SafeSubscriber. See https://drive.google.com/open?id=0B4JH51FD7JBZcUJTTVRjRXJ2a00. There is Subscriber which is closed (good), but destination as SafeSubscriber is still not closed.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:8
  • Comments:18 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
trxcllntcommented, Feb 13, 2020
1reaction
cartantcommented, Feb 13, 2020

(For my own reference) this issue is somewhat related to this one: https://github.com/ReactiveX/rxjs/pull/5243

Read more comments on GitHub >

github_iconTop Results From Across the Web

SafeSubscriber does not unsubscribe itself #2675 - GitHub
When subscribing to Observable with Partial Observer, rxjs creates SafeSubscriber, but on unsubscribe it does not unsubscribe itself.
Read more >
Angular/RxJS When should I unsubscribe from `Subscription`
Calling complete() by itself doesn't appear to clean up the subscriptions. However calling next() and then complete() does, I believe takeUntil() only stops ......
Read more >
Best Practices for Managing RxJS Subscriptions - This Dot Labs
Unsubscribing Manually. One method we can use, is to unsubscribe manually from active subscriptions when we no longer require them. RxJS ...
Read more >
Remember to unsubscribe() from streams in your Angular ...
I decided to write this article because my eyes start to bleed whenever I see code which a developer did not clean up...
Read more >
6 Effective Ways to Unsubscribe in RxJS - Byte This!
Managing subscriptions and unsubscribing from them appropriately is one of the most important considerations when using RxJS.
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