- Notifications
You must be signed in to change notification settings - Fork3k
Description
Bug Report
Current Behavior
When instantiating a Subscription and passing a callback, that callback is saved asthis._unsubscribe of theSubscription object:
rxjs/src/internal/Subscription.ts
Line 43 ind608cf3
| (<any>this)._unsubscribe=unsubscribe; |
When unsubscribing that Subscription, the function is called, but not nulled out afterwards:
rxjs/src/internal/Subscription.ts
Line 79 ind608cf3
| _unsubscribe.call(this); |
This means that if it did some manual resource cleanup like closing a MessagePort, that MessagePort still can't be garbage-collected, because the callback, and therefor the Subscription, still holds a reference to it.
This is different from Subscriptions added viaadd(), which are correctly de-referenced:
rxjs/src/internal/Subscription.ts
Line 66 ind608cf3
| this._subscriptions=null; |
Reproduction
- REPL or Repo link:
(you can usehttps://stackblitz.com/ to create one to attach here)
const{ port1}=newMessageChannel()constsubscription=newSubscription(()=>{port1.close()})subscription.unsubscribe()console.log(subscription._unsubscribe)
Expected behavior
After calling the callback,this._unsubscribe should be nulled out so the callback and its references can be garbage-collected. Since a Subscription can only be unsubscribed once, there is no need to keep the reference.
Environment
- Runtime: All
- RxJS version: 6.5.5
Possible Solution
Setthis._unsubscribe tonull whenunsubscribe() is called.