Skip to content
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

ChildSubscriptions unsubscribed but not removed. #2355

Closed
johncvrs opened this issue Feb 10, 2017 · 5 comments · Fixed by #2360
Closed

ChildSubscriptions unsubscribed but not removed. #2355

johncvrs opened this issue Feb 10, 2017 · 5 comments · Fixed by #2360

Comments

@johncvrs
Copy link

johncvrs commented Feb 10, 2017

In the switch() operator, you'll find this code used to add
subscriptions to the Subscriptions._subscriptions[] array:

this.add(this.innerSubscription = subscribeToResult(this, value));

And you'll find this code to remove them:

const innerSubscription = this.innerSubscription;
if (innerSubscription) {
  innerSubscription.unsubscribe();
  this.remove(innerSubscription);
}

But note behavior of Subscription.add(s) is not "add s to the array and return s";
behavior is "create a new ChildSubscription from s, add the ChildSubscription to the array,
and return the ChildSubscription."
The consequence? The remove() does not match the add(). The remove() becomes a silent no-op.
The unsubscribe() was successful (good - we get correct program behavior)
but the remove() fails (bad - cannot GC these silent ChildSubscriptions).

RxJS version:
v5.1.0

Code to reproduce:

var parentTicker = Rx.Observable.interval(1000);
var childTickers = parentTicker.map((x) => {
  return Rx.Observable.interval(100).take(2);
});

var switcher = childTickers.switch();
var sub = switcher.subscribe(function(x){
        console.log('tick #' + x + ': subscriptions = ' +
          sub._subscriptions[0]._innerSub._subscriptions.length);
      });

http://jsbin.com/zotobo/edit?js,console

Additional information:

PRs with specs and fixes for two operators (switch(), throttle())
will follow this message. Take a look and tell me if you agree
that this is a legit problem and not failed understanding on my part.

( Because if you do agree, this problem also exists in many
operators. Operators audit(), switchMap(), switchMapTo(), windowToggle(),
windowWhen() can be fixed as above, but fixing MergeMap() e.g., is going to
require stepping back and deciding if remove() responsibility should
even live in the operators, or if it should live in the Subscription class
or ChildSubscription class rolled into the unsubscribe methods. )

@johncvrs
Copy link
Author

johncvrs commented Feb 10, 2017

Oh, should have included the fix in this message, not just the PR. For the switch and throttle operators the fix is simply:

Before

this.add(this.innerSubscription = subscribeToResult(this, value));

After

this.innerSubscription = this.add(subscribeToResult(this, value));

And with this in place, this line

this.remove(innerSubscription);

is not even needed (ChildSubscriptions remove themselves on unsubscribe).

For other operators, fixing is not so simple.

@kwonoj
Copy link
Member

kwonoj commented Feb 10, 2017

coincidencely I noticed numbers of childsubscription grows up with switchmap yesterday, seems this issue is culprit (as noted in issue itself), but haven't digged myself into yet.

johncvrs added a commit to johncvrs/rxjs that referenced this issue Feb 10, 2017
…eX#2355)

Hold reference to ChildSubscription, not Subscription, so it can be properly removed later.
johncvrs added a commit to johncvrs/rxjs that referenced this issue Feb 10, 2017
…iveX#2355)

Hold reference to ChildSubscription, not Subscription, so it can be properly removed later.
johncvrs added a commit to johncvrs/rxjs that referenced this issue Feb 10, 2017
…eX#2355)

Hold reference to ChildSubscription, not Subscription, so it can be properly removed later.
@trxcllnt
Copy link
Member

Yes this seems serious, and no doubt affects many operators. @Blesh perhaps there's a surgical fix we can make to Subscription that would solve this everywhere?

@trxcllnt
Copy link
Member

trxcllnt commented Feb 12, 2017

@johncvrs after auditing the operator impls, and issue #2244 that led to introducing ChildSubscription, I think we can make a surgical fix to Subscription that will solve this problem comprehensively. Going to try to get that PR done asap, thanks for reporting and submitting your PR.

trxcllnt added a commit to trxcllnt/rxjs that referenced this issue Feb 12, 2017
…event operators from leaking ChildSubscriptions.

The addition of ChildSubscription to fix ReactiveX#2244 accidentally introduced a different memory leak. Most operators that add and remove inner Subscriptions store the inner Subscriber instance, not the value returned by Subscription#add. When they try to remove the inner Subscription manually, nothing is removed, because the ChildSubscription wrapper instance is the one added to the subscriptions list.

Fixes ReactiveX#2355
trxcllnt added a commit to trxcllnt/rxjs that referenced this issue Feb 13, 2017
…event operators from leaking ChildSubscriptions.

The addition of ChildSubscription to fix ReactiveX#2244 accidentally introduced a different memory leak. Most operators that add and remove inner Subscriptions store the inner Subscriber instance, not the value returned by Subscription#add. When they try to remove the inner Subscription manually, nothing is removed, because the ChildSubscription wrapper instance is the one added to the subscriptions list.

Fixes ReactiveX#2355
benlesh pushed a commit that referenced this issue Feb 13, 2017
…event operators from leaking ChildSubscriptions. (#2360)

The addition of ChildSubscription to fix #2244 accidentally introduced a different memory leak. Most operators that add and remove inner Subscriptions store the inner Subscriber instance, not the value returned by Subscription#add. When they try to remove the inner Subscription manually, nothing is removed, because the ChildSubscription wrapper instance is the one added to the subscriptions list.

Fixes #2355
trxcllnt added a commit to trxcllnt/rxjs that referenced this issue Feb 15, 2017
…event operators from leaking ChildSubscriptions. (ReactiveX#2360)

The addition of ChildSubscription to fix ReactiveX#2244 accidentally introduced a different memory leak. Most operators that add and remove inner Subscriptions store the inner Subscriber instance, not the value returned by Subscription#add. When they try to remove the inner Subscription manually, nothing is removed, because the ChildSubscription wrapper instance is the one added to the subscriptions list.

Fixes ReactiveX#2355
benlesh pushed a commit that referenced this issue Mar 27, 2017
* fix(timeout): Cancels scheduled timeout, if no longer needed

fixes #2134

* fix(timeoutWith): Cancels scheduled timeout, if no longer needed

* build(npm-scripts): update debug_mocha npm script for node 6

* fix(VirtualAction): Block rescheduled VirtualActions from executing their scheduled work.

VirtualActions are immutable so they can be inspected by the TestScheduler. In order to mirror

rescheduled stateful Actions, rescheduled VirtualActions shouldn't execute if they've been

rescheduled before execution.

* fix(timeout): Update timeout and timeoutWith to recycle their scheduled timeout actions.

The timeout and timeoutWith operators should dispose their scheduled timeout actions on

unsubscription. Also, given the new scheduling architecture, they can recycle their scheduled

actions so just one action is allocated per subscription.

* test(timeout): Add types to timeout and timeoutWith specs

* Fix merge conflicts

* Fix timeoutWith to work with new Subscriber leak fix.

* fix(timeout-spec): fix merge conflicts

* fix(Subscription): fold ChildSubscription logic into Subscriber to prevent operators from leaking ChildSubscriptions. (#2360)

The addition of ChildSubscription to fix #2244 accidentally introduced a different memory leak. Most operators that add and remove inner Subscriptions store the inner Subscriber instance, not the value returned by Subscription#add. When they try to remove the inner Subscription manually, nothing is removed, because the ChildSubscription wrapper instance is the one added to the subscriptions list.

Fixes #2355

* chore(publish): 5.1.1

* Ignore coverage

It's 5.5mb that people installing this don't need :)

* feat(AjaxObservable) : support 'PATCH' request type

Add support of the 'PATCH' request type based on the already existing 'PUT' request.

* fix(subscribeToResult): accept array-like as result

Accept array-like as a result to subscribe, so that
Observable.from and operators using subscribeToResult
have identical behaviour.

* chore(ajax.patch): Adds test for ajax.patch

* fix(forkJoin): add type signature for single observable with selector

Add type signature for using forkJoin with single observable as first parameter
and selector function as second parameter, so that typings would not prevent
usage which is permitted and properly handled by operator.

Closes #2347

* feat(webSocket): Add binaryType to config object

Add binaryType to config object, so that it is possible
to set that parameter on underlying socket before any
data emits happen.

Closes #2353

* fix(merge): return Observable when called with single lowerCaseO

Return Observable when merge is called with single lower case observable,
so that merge would always return Observable instance.

* fix(bindNodeCallback): emit undefined when callback has no success arguments

Emit undefined insteady of empty array by resulting Observable,
when callback function is called without success parameters.

Closes #2254

* chore(danger): update dangerfile to validate commit message

* chore(*): correctly scope disabled `max-line-length` tslint rule

The max line length is set to 150 in 'tslint.json'. In specific regions, it is
desirable to allow longer lines, so these regions should be wrapped in comments
like the following:

```js
// Max line length enforced here.

/* tslint:disable:max-line-length */
// Max line length NOT enforced here.
/* tslint:enable:max-line-length */   <-- CORRECT

// Max line length enforced here.
```

In many cases, the re-enabling comment incorrectly included `disable` instead of
`enable` (as shown below), which essentially keeps the `max-line-length`
**disabled** for the rest of the file:

```js
// Max line length enforced here.

/* tslint:disable:max-line-length */
// Max line length NOT enforced here.
/* tslint:disable:max-line-length */   <-- INCORRECT

// Max line length NOT enforced here.
```

This commit fixes these comments, so the `max-line-length` rule is properly
enforced in regions that don't need longer lines.

* fix(bindCallback): emit undefined when callback is without arguments

In resulting Observable emit undefined when callback is called without
parameters, instead of emitting empty array.

* fix(mergeAll): introduce variant support <T, R> for mergeMap

- closes #2372

* feat(windowTime): maxWindowSize parameter in windowTime operator

Adds new parameter in windowTime operator to control how much values given
window can emit.

Closes #1301

* docs(ObservableInput): add ObservableInput and SubscribableOrPromise descriptions

Add ObservableInput and SubscribableOrPromise interface descriptions,
as well as link these interfaces in type descriptions of operators,
so that users always know what kind of parameters they can pass to
used methods.

* fix(timeoutWith): update timeoutWith to work with new Subscriber leak fix changes
@lock
Copy link

lock bot commented Jun 6, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
3 participants