Skip to content

rxjs subscription leak in DowngradeComponentAdapter.subscribeToOutput #48032

@vbraun

Description

@vbraun

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

No

Description

Destroying a downgraded component with @Output leaks a rxjs subscription

Steps to reproduce:

  • You need an Angular component with an @Output
  • Downgrade to use it in an AngularJS component
  • Create & destroy the downgraded component, e.g. by routing to a page that contains it and then routing away

In https://github.com/angular/angular/blob/main/packages/upgrade/src/common/src/downgrade_component_adapter.ts:

  private subscribeToOutput(output: PropertyBinding, expr: string, isAssignment: boolean = false) {
    const getter = this.$parse(expr);
    const setter = getter.assign;
    if (isAssignment && !setter) {
      throw new Error(`Expression '${expr}' is not assignable!`);
    }
    const emitter = this.component[output.prop] as EventEmitter<any>;
    if (emitter) {
      emitter.subscribe({                            <------------- this subscription is never unsubscribed from 
        next: isAssignment ? (v: any) => setter!(this.scope, v) :
                             (v: any) => getter(this.scope, {'$event': v})
      });
    } else {
      throw new Error(`Missing emitter '${output.prop}' on component '${
          getTypeName(this.componentFactory.componentType)}'!`);
    }
  }

The subscription leak might be garbage collectable depending on how the @Output observable is defined, our leak check tooling cannot test that programatically. In any case a user could have defined the @Output to be a long-lived observable, in which this is a genuine leak.

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 14.2.9
Node: 16.15.0
Package Manager: npm 8.5.5 
OS: linux x64

Angular: 14.2.10
... animations, common, compiler, compiler-cli, core, elements
... forms, language-service, localize, platform-browser
... platform-browser-dynamic, router, upgrade

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1402.9
@angular-devkit/build-angular   14.2.9
@angular-devkit/core            14.2.9
@angular-devkit/schematics      13.3.5
@angular/cdk                    14.2.7
@angular/cli                    14.2.9
@angular/material               14.2.7
@schematics/angular             14.2.9
ng-packagr                      14.1.0
rxjs                            6.6.7
typescript                      4.7.4

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: upgradeIssues related to AngularJS → Angular upgrade APIs

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions