rxjs: Subscription to Observable is not removed from observers array after unsubscribe()
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
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 5
- Comments: 38
Commits related to this issue
- feat: Add rxjs-no-index rule. See https://github.com/ReactiveX/rxjs/issues/4230 — committed to cartant/rxjs-tslint-rules by cartant 5 years ago
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’.
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.
As I’ve said above:
Seriously, no one is going to be able to answer any questions about this without a minimal reproduction of the problem.
@PaulAGH I agree. This almost drove me mad 😃
I also started encountering this after upgrading from Angular 6 to 7. On Chrome everything would behave, but on Firefox I would observe the following as the app would initialize.
I tried different versions of rxjs, examining closely npm list, etc - all to no avail.
Finally what did the trick was to change import {Subject} from ‘rxjs/index’; //This did not cause issues before the upgrade to import {Subject} from ‘rxjs’;
Crazy stuff!
It seems like import statements from ‘rxjs/index’ and ‘rxjs/internal/*’ (and perhaps others) should cause compilation errors.
With the absence of minimal reproduction - and with reports that the problem is due to importing an internal file - IMO, this issue should just be closed.
I have the same issue. I downgraded to rxjs 6.3.2 and now is working.
I don’t have a public one. I’ll try creating one if I have some time, but it won’t be now.
In your StackBlitz, you are using two separate implementations of RxJS: the bundle; and the
package.json
dependency. You are importingSubject
from the former andtakeUntil
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:
And the problem does not occur if 6.3.2 is used.