Skip to content

Subscription callback is not nulled out when Subscription is unsubscribed #5464

Closed
@felixfbecker

Description

Bug Report

Current Behavior
When instantiating a Subscription and passing a callback, that callback is saved as this._unsubscribe of the Subscription object:

(<any> this)._unsubscribe = unsubscribe;

When unsubscribing that Subscription, the function is called, but not nulled out afterwards:

_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 via add(), which are correctly de-referenced:

this._subscriptions = null;

Reproduction

const { port1 } = new MessageChannel()
const subscription = new Subscription(() => {
  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
Set this._unsubscribe to null when unsubscribe() is called.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    bugConfirmed bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions