Skip to content

fix(@angular/ssr): destroy the platform of an aborted request once it… - #34755

Closed
SkyZeroZx wants to merge 1 commit into
angular:mainfrom
SkyZeroZx:fix/ssr-abort-in-flight-render
Closed

SkyZeroZx wants to merge 1 commit into
angular:mainfrom
SkyZeroZx:fix/ssr-abort-in-flight-render

Conversation

@SkyZeroZx

Copy link
Copy Markdown
Contributor

… is stable

Aborting a request destroyed the platform while an app initializer or an HttpClient request was still pending, which could end the server process. The platform is now destroyed once the application is stable, or after 10 seconds.

Fixes #34754

… is stable

Aborting a request destroyed the platform while an app initializer or an
`HttpClient` request was still pending, which could end the server
process. The platform is now destroyed once the application is stable,
or after 10 seconds.

Fixes angular#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 improves the handling of aborted renders in Angular SSR by delaying platform destruction until the application becomes stable or a 10-second timeout elapses, preventing pending work from failing abruptly. The review feedback recommends replacing AbortSignal.timeout with standard setTimeout and clearTimeout to prevent the Node.js event loop from hanging in serverless environments, and provides corresponding adjustments for the test suite.

Comment on lines +113 to +114
let abortTimeoutSignal: AbortSignal | undefined;
let isStable = false;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Instead of using AbortSignal.timeout, which creates a timer that cannot be cleared manually and can keep the Node.js event loop active (potentially causing serverless functions to hang or delay termination), we can use a standard setTimeout and clearTimeout approach. This is simpler, more compatible, and avoids event loop retention issues.

Suggested change
let abortTimeoutSignal: AbortSignal | undefined;
let isStable = false;
let abortTimeoutId: ReturnType<typeof setTimeout> | undefined;
let isStable = false;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

AbortSignal.timeout() doesn't keep the event loop alive, in Node its timer is unref'd. I tested it with the built server: aborting a request whose render never becomes stable and then closing the server, the process exits right away (0.00 s), while with a setTimeout it waits for the timer (9.83 s).

It's also what the route extraction and render workers already use for their 30 s deadlines, and the listener is removed when the platform is destroyed, so the timer firing later does nothing. Keeping it as is.

Comment thread packages/angular/ssr/src/utils/ng.ts Resolved
Comment thread packages/angular/ssr/test/app_spec.ts Resolved
Comment thread packages/angular/ssr/test/app_spec.ts Resolved
@alan-agius4

Copy link
Copy Markdown
Collaborator

Hi @SkyZeroZx, thank you for investigating #34754 and putting together the reproduction and this PR!

After looking closely at the failure modes, we want to fix the underlying teardown bugs directly in the framework rather than deferring platform destruction with a timeout in @angular/ssr:

  1. Deferring destruction until stability keeps in-flight HttpClient requests, route guards/resolvers, and timers alive after the client disconnects, which regresses the cancellation behavior introduced for @angular/ssr: an aborted request rejects handle() but the render keeps running #34567.
  2. Any request or upstream call that takes longer than the 10-second fallback still hits the same post-destroy crash paths (NG0205 in bootstrap(), setTimeout(() => { throw e; }) in INTERNAL_APPLICATION_ERROR_HANDLER, EmptyError from FetchBackend, etc.).

We've opened two PRs to address the root causes directly while keeping immediate platform destruction on abort:

Because of this, we're going to close this PR in favor of the framework fixes. Thanks again for the detailed issue report and contribution!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Angular SSR can crash AngularAppEngine servers with a single aborted request (22.2.6 regression)

2 participants