rxjs icon indicating copy to clipboard operation
rxjs copied to clipboard

Subscription to Observable is not removed from observers array after unsubscribe()

Open ababashka opened this issue 8 years ago • 38 comments

Bug Report

Current Behavior Currently subscription isn't removed from observers array after unsubscribe, both with takeUntil() and unsubscribe().

Reproduction

function called() { console.log('CALLED'); }

const subject = new Subject();
const destroyed = new Subject();

const subscription = subject
        .pipe(takeUntil(destroyed))
        .subscribe(() => called());

console.log(subject.observers.length); // 1

subject.next(); // CALLED
subject.next(); // CALLED

// Unsubscribe from subject
destroyed.next(true);
destroyed.complete();

console.log(subject.observers.length); // Still 1, expected: 0

subscription.unsubscribe();
subject.next(); // no console.log('CALLED')

console.log(subject.observers.length); // Still 1, expected: 0

Expected behavior In 2 last console.log(subject.observers.length) it's expected to have 0 : observer should be removed from array of observers after unsubscribe()

Environment

  • RxJS version:
  • 6.3.3

Additional notes It isn't reproducible with rxjs version 6.2.2

ababashka avatar Oct 05 '18 13:10 ababashka

same issue here after upgrading from 6.3.2 to 6.3.3

mdepasquale21 avatar Oct 05 '18 15:10 mdepasquale21

For me, this issue's repro outputs the expected counts:

$ ts-node index.ts
1
CALLED
CALLED
0
0

The code is unchanged:

import { Subject } from "rxjs";
import { takeUntil } from "rxjs/operators";

function called() { console.log('CALLED'); }

const subject = new Subject();
const destroyed = new Subject();

const subscription = subject
  .pipe(takeUntil(destroyed))
  .subscribe(() => called());

console.log(subject.observers.length); // 1

subject.next(); // CALLED
subject.next(); // CALLED

// Unsubscribe from subject
destroyed.next(true);
destroyed.complete();

console.log(subject.observers.length); // Still 1, expected: 0

subscription.unsubscribe();
subject.next(); // no console.log('CALLED')

console.log(subject.observers.length); // Still 1, expected: 0

And I'm testing it with 6.3.3:

$ npm list rxjs
[email protected] C:\Users\Nicholas\git\temp\so-rxjs-v6-scratchpad
`-- [email protected]

cartant avatar Oct 05 '18 22:10 cartant

@cartant You are right, i've started new (angular) project and couldn't reproduce this issue with RxJS 6.3.3. But still i can reproduce it here: https://stackblitz.com/edit/rxjs-me3evv?file=index.ts

I've copied content of node_modules/rxjs/bundles/rxjs.umd.js bundle and put it into file rxjsbundle.js in my example on stackblitz.

Also, some additional note: i found this issue in my angular project's test and when i start this karma test with ng - it's reproducible. I've updated only rxjs in my package.json (from 6.2.2 to 6.3.3 as i mentioned previously) - and that failed.

Thanks for your time and would be nice if you could clarify this.

ababashka avatar Oct 06 '18 12:10 ababashka

So are you saying that it's only reproducible when using the bundle that's in 6.3.3?

cartant avatar Oct 06 '18 12:10 cartant

Actually i've just tried to copy bundle's content to my example from versions 6.3.2, 6.3.1 and even from 6.2.2 - it's still reproducible with stackblitz and that's pretty strange. But the test got failed only with RxJS version 6.3.3 and is working with 6.3.2

ababashka avatar Oct 06 '18 14:10 ababashka

This works fine for me, too:

<html>
<body>
<script src="./node_modules/rxjs/bundles/rxjs.umd.js"></script>
<script>

const { Subject } = rxjs;
const { takeUntil } = rxjs.operators;

function called() { console.log('CALLED'); }

const subject = new Subject();
const destroyed = new Subject();

const subscription = subject
    .pipe(takeUntil(destroyed))
    .subscribe(() => called());

console.log(subject.observers.length); // 1

subject.next(); // CALLED
subject.next(); // CALLED

// Unsubscribe from subject
destroyed.next(true);
destroyed.complete();

console.log(subject.observers.length); // Still 1

subscription.unsubscribe();
subject.next(); // no console.log('CALLED')

console.log(subject.observers.length); // Still 1

</script>
</body>
</html>

capture

cartant avatar Oct 06 '18 20:10 cartant

In your StackBlitz, you are using two separate implementations of RxJS: the bundle; and the package.json dependency. You are importing Subject from the former and takeUntil from the latter.

I suspect that the problem is directly related to the two implementations and that the changes made in 6.3.3 that have effected this behaviour are in https://github.com/reactivex/rxjs/commit/50ee0a7 and/or https://github.com/reactivex/rxjs/commit/0972c56.

The problem is reproducible with this code:

const { Subject } = require("rxjs/bundles/rxjs.umd");
import { takeUntil } from "rxjs/operators";

function called() { console.log('CALLED'); }

const subject = new Subject();
const destroyed = new Subject();

const subscription = subject
  .pipe(takeUntil(destroyed))
  .subscribe(() => called());

console.log(subject.observers.length); // 1

subject.next(); // CALLED
subject.next(); // CALLED

// Unsubscribe from subject
destroyed.next(true);
destroyed.complete();

console.log(subject.observers.length); // Still 1, expected: 0

subscription.unsubscribe();
subject.next(); // no console.log('CALLED')

console.log(subject.observers.length); // Still 1, expected: 0

And the problem does not occur if 6.3.2 is used.

cartant avatar Oct 06 '18 20:10 cartant

In my case I have a working angular app (cli version 6.2.4, with rxjs 6.3.2) , in which I have a component behaving more or less like this:

import { Component, OnDestroy, OnInit } from '@angular/core';
import { Subscription } from 'rxjs';
import { tap } from 'rxjs/internal/operators';

export class MyComponent implements OnInit, OnDestroy {
    myObservable: Observable<T>;
    mySubscription: Subscription;

    ngOnInit() {
        console.log('subscription');
        this.mySubscription = this.myObservable.pipe(
            tap(() => {
                console.log('do stuff with observable');
            })
        ).subscribe(
            () => {
                console.log('do other stuff!');
            }
        );
    }

    ngOnDestroy() {
        console.log('unsubscribe');
        this.mySubscription.unsubscribe();
     }
}

After the update to rxjs 6.3.3, in the console I see: > subscription > do stuff with observable > do other stuff! > unsubscribe > do stuff with observable

mdepasquale21 avatar Oct 09 '18 08:10 mdepasquale21

@mdepasquale21 For this problem to manifest, I think you'd need to have multiple copies of RxJS installed in node_modules - either different versions or the same version in different locations.

Run npm list rxjs in your project's root to see what's installed.

cartant avatar Oct 09 '18 08:10 cartant

@cartant you were right, shame on me! Some other dependencies in my app (@angular-devkit), which depend in turn internally on rxjs, downloaded version 6.2.2 when npm installed, even if in their package.json they have rxjs: ''~6.3.0" (https://github.com/angular/angular-cli/blob/master/package.json). I tried also to delete node_modules and re-install everything, but looks like nothing changed. rxjs in my package.json was updated to 6.3.3, but version in @angular/cli was 6.2.2 so it was not the same everywhere, as you suggested. For some reason until version 6.3.2 this mixture did not break stuff! I've tried a few solutions and it seems to me that it is very difficult to "force" the download of rxjs 6.3.3 for all dependencies, so actually I'm reverting to rxjs 6.2.2 to make everything work again. But I remain interested in future developments of this post! Thanks!

mdepasquale21 avatar Oct 09 '18 15:10 mdepasquale21

Pretty sure this defect resulted in a huge memory leak across a bunch of our angular sites.

Reverting to rxjs 6.2.2 resolved the issue for us.

lowe0292 avatar Oct 10 '18 18:10 lowe0292

hi all - i am still a bit confused as to why there would be different version of rxjs installed for an angular application, especially when rxjs is listed as a peerDependency. I did a build w/o optimzation to check to see if there were multiple versions of rxjs installed and couldn't find more than 1. Also, when i run npm list rxjs, the version returned is 6.2.2; however when i look inside ./node_modules, I can clearly see that 6.3.3 is installed. I guess I would expect npm list rxjs to return 6.3.3? Admittedly, this is more of a how does npm work question rather than a rxjs question, but hoping someone here could enlighten me since it's related :). Thanks all!

danielgeri avatar Oct 16 '18 20:10 danielgeri

Same isssue here since i have update from 6.3.2 to 6.3.3 with angular-cli

Spawnrad avatar Oct 30 '18 02:10 Spawnrad

@danielgeri I'm not sure either, the only thing I can suggest is that you try to delete node_modules directory and run npm install to reinstall everything from scratch. In my case when I tried this solution it didn't work. I had rxjs listed as a peer dependency but other libraries in my package.json which depend on rxjs for some reason were using a different version. For me it was completely unexpected, I had to revert to rxjs 6.2.2. Anyway after updating to Angular 7 everything works fine and I could finally use rxjs 6.3.3.

mdepasquale21 avatar Oct 31 '18 09:10 mdepasquale21

I tried everything with deleting node_modules directory.

Angular 6 and rxjs 6.3.3 => not working Angular 6 and rxjs 6.3.2 => working

Angular 7 and rxjs 6.3.3 => not working Angular 7 and rxjs 6.3.2 => working

Spawnrad avatar Oct 31 '18 23:10 Spawnrad

FWIW, I've had the same issue when upgrading from 6.3.2 to 6.3.3, and the bug was in fact caused by an incorrect import: I imported from 'rxjs/internal/operators' instead of 'rxjs/operators'.

jnizet avatar Nov 19 '18 10:11 jnizet

@jnizet Do you have a minimal reproduction? I have an idea of how using the internal import location could be problematic, but I'd like to reproduce the problem. Mostly because I'm curious.

cartant avatar Nov 19 '18 11:11 cartant

I don't have a public one. I'll try creating one if I have some time, but it won't be now.

jnizet avatar Nov 19 '18 12:11 jnizet

May be related with the discussion, I have an update with respect to my previous answer and in light of @jnizet 's answer: with angular 7 and rxjs 6.3.3 I thought everything was working fine, but instead I had a huge, random bug on initialization of the app! Sometimes the app just did not start, loading forever while sometimes started normally with no errors. So this 'init' bug appeared randomly without apparent reason whatsoever and it was difficult to reproduce and therefore to solve. I noticed I had several imports from 'rxjs/internal/operators' and changed them to import from 'rxjs/operators' as @jnizet suggested. This solved the problem finally (or at least it looks like, the bug has never appeared again since then).

mdepasquale21 avatar Nov 20 '18 09:11 mdepasquale21

I have the same issue. I downgraded to rxjs 6.3.2 and now is working.

tiagowanke avatar Jan 11 '19 10:01 tiagowanke