Skip to content

fix(@angular/ssr): log errors thrown while destroying the platform - #34777

Merged
alan-agius4 merged 1 commit into
angular:mainfrom
SkyZeroZx:fix/ssr-destroy-platform-uncaught-error
Nov 24, 2026
Merged

alan-agius4 merged 1 commit into
angular:mainfrom
SkyZeroZx:fix/ssr-destroy-platform-uncaught-error

Conversation

@SkyZeroZx

@SkyZeroZx SkyZeroZx commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

If a destroy hook throws while the platform is being destroyed, the error escapes the setTimeout in asyncDestroyPlatform() and ends the process on servers that use AngularAppEngine. #34768 added a try/finally there, but that still lets the error through.

It doesn't matter whether the component is routed or not. A component that creates a subscription once an HTTP call answers ends the process if the visitor leaves before the answer:

export class Profile implements OnInit, OnDestroy {
  private readonly http = inject(HttpClient);
  private readonly updates = new Subject<string>();
  private subscription!: Subscription;
  protected readonly name = signal('');

  ngOnInit() {
    this.http.get<{ name: string }>('http://localhost:4400/api/profile').subscribe(({ name }) => {
      this.name.set(name);
      this.subscription = this.updates.subscribe((next) => this.name.set(next));
    });
  }

  ngOnDestroy() {
    // undefined if the visitor left before the answer
    this.subscription.unsubscribe();
  }
}

A routed component behind an HTTP resolver only needs a plain subscribe. In a zoneless app, if the request is aborted right as the resolver answers, the component is destroyed before its first change detection and ngOnDestroy runs without ngOnInit:

export const routes: Routes = [
  {
    path: '',
    component: Dashboard,
    resolve: { user: () => inject(HttpClient).get('http://localhost:4400/api/user') },
  },
];

export class Dashboard implements OnInit, OnDestroy {
  private readonly notifications = inject(Notifications);
  private subscription!: Subscription;
  protected readonly unread = signal(0);

  ngOnInit() {
    this.subscription = this.notifications.unread$.subscribe((n) => this.unread.set(n));
  }

  ngOnDestroy() {
    // can run before ngOnInit
    this.subscription.unsubscribe();
  }
}

The error is now caught and logged. angular/angular#72139 also catches it in the @angular/platform-server copy. Here it is logged instead of rejected, since nothing awaits the destroy.

Related to #34754

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request wraps the platform destruction logic in a try-catch block within asyncDestroyPlatform to catch and log any errors thrown by destroy hooks, preventing them from escaping the macrotask and causing uncaught exceptions. Additionally, a unit test has been added to verify this behavior. There are no review comments, and the changes look solid, so I have no feedback to provide.

An error thrown by a destroy hook escaped the timer that destroys the
platform and could terminate the server process. It is now logged.

Fixes angular#34754
@SkyZeroZx
SkyZeroZx force-pushed the fix/ssr-destroy-platform-uncaught-error branch from e9479a4 to f32d7cb Compare November 23, 2026 18:48
@alan-agius4 alan-agius4 added target: patch This PR is targeted for the next patch release action: merge The PR is ready for merge by the caretaker labels Nov 24, 2026
@alan-agius4
alan-agius4 merged commit d33a557 into angular:main Nov 24, 2026
41 checks passed
@alan-agius4

Copy link
Copy Markdown
Collaborator

This PR was merged into the repository. The changes were merged into the following branches:

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

Labels

action: merge The PR is ready for merge by the caretaker area: @angular/ssr target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants