Skip to content

@boundary keeps subscriptions alive after a sibling constructor throws #72210

In progress

Description

@SkyZeroZx

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

core

Is this a regression?

No

Description

The boundary shows the fallback when a constructor throws inside @defer. But a component created before the throwing sibling keeps its subscription alive, even with takeUntilDestroyed(). Each $reset() adds another one.

LiveOrders subscribes to OrdersService.updates. OrderTotal then throws because USDD is an invalid currency code.

import { Component, Service, inject, signal } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { Subject } from 'rxjs';

@Service()
export class OrdersService {
  readonly orderCount = 3;
  readonly total = 100;
  readonly updates = new Subject<void>();
  readonly count = signal(0);
}

@Component({ selector: 'live-orders', template: '<p>Orders: {{ orders.orderCount }}</p>' })
class LiveOrders {
  readonly orders = inject(OrdersService);

  constructor() {
    this.orders.updates
      .pipe(takeUntilDestroyed())
      .subscribe(() => this.orders.count.update((value) => value + 1));
  }
}

@Component({
  selector: 'order-total',
  template: '<p>Total: {{ formatter.format(orders.total) }}</p>',
})
class OrderTotal {
  readonly orders = inject(OrdersService);

  readonly formatter = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USDD',
  });
}

@Component({
  imports: [LiveOrders, OrderTotal],
  selector: 'app-root',
  template: `
    @boundary {
      @defer (on immediate) {
        <live-orders />
        <order-total />
      }
    } @error {
      <p>Could not load orders.</p>
      <button (click)="$reset()">Retry</button>
    }
    <button (click)="orders.updates.next()">Emit</button>
    <p id="notifications">Notifications: {{ orders.count() }}</p>
  `,
})
export class App {
  readonly orders = inject(OrdersService);
}
  1. Wait for Retry.
  2. Click Retry once and wait for the fallback again.
  3. Click Emit once.

I expected Notifications: 0. I get Notifications: 2: both failed attempts still react to the same emission.

Anything else?

This also happens without @defer. Moving the error to ngOnInit cleans up both subscriptions.

In the constructor case, DestroyRef.onDestroy, ngOnDestroy, and subscription finalization never run for LiveOrders. Destroying the fixture afterward doesn't clean them up either.

Activity

  1. added
    area: core
    on Nov 25, 2026
  2. added this to the needsTriage milestone on Nov 25, 2026
  3. JeanMeche commented on Nov 26, 2026

    @JeanMeche
    Member

    When a threw my agent at it I got #72222, looking at the code it reminded me of #70406.

    The question here is should @boundary cover errors thrown at construction time. As you mention when throwing in ngOnInit the component is cleaned up correctly. This is expected as hooks are part of rendering.

  4. SkyZeroZx commented on Nov 26, 2026

    @SkyZeroZx
    ContributorAuthor

    As I understand it, or at least for the case in our own codebase in Angular, I think @boundary should/must cover errors in the constructor since that was the intended behavior. However, we can add it as a limitation in the documentation, but I don't think that can be the case because it can be a common case and break the expected behavior for the users/agents.

    Also, it's a bit odd this behavior , if we do a subscription to something , we can get duplicated and cause unexpected bugs

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: core

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions