Skip to content

fix(@angular/ssr): guard aborted requests across rendering and serialization - #34768

Open
alan-agius4 wants to merge 1 commit into
angular:mainfrom
alan-agius4:fix-ssr-abort-guards
Open

alan-agius4 wants to merge 1 commit into
angular:mainfrom
alan-agius4:fix-ssr-abort-guards

Conversation

@alan-agius4

Copy link
Copy Markdown
Collaborator

PR Checklist

PR Type

  • Bugfix

What is the current behavior?

When a request is aborted during SSR rendering:

  • AngularServerApp.handleRendering() can still perform asset reads, run html:transform:pre hooks, invoke renderAngular(), or run Beasties inlineCriticalCss() after request.signal has already aborted.
  • renderAngular() does not check moduleRef.injector.destroyed, applicationRef.destroyed, platformRef.destroyed (post-whenStable), or destroyPromise consistently across bootstrap, stability, and content(), only clears renderTimeoutId on the first destroy() call, detaches abort listeners two microtasks after content() settles, and can leave asyncDestroyPlatform() unresolved if platformRef.destroy() throws.

Companion framework fix: angular/angular#72139
Related issue: #34754

What is the new behavior?

  • Short-circuit AngularServerApp.handleRendering() on request.signal.aborted before asset reads, html:transform:pre hooks, renderAngular(), redirect response creation, and critical CSS inlining, and uniformly await result.destroy() when aborting inside the response ReadableStream.
  • Keep immediate platform destruction on abort in renderAngular() (relying on the framework teardown fixes in fix(core): safely handle server platform destruction on aborted SSR requests angular#72139 instead of deferring destruction or adding fallback timeouts).
  • Guard moduleRef.injector.destroyed, applicationRef.destroyed, platformRef.destroyed, and destroyPromise consistently across renderAngular() and content(), clear renderTimeoutId on every destroy() call, detach abort listeners before settling content(), and wrap platformRef.destroy() in try/finally inside asyncDestroyPlatform().

Does this PR introduce a breaking change?

  • No

…ization

Short-circuit `AngularServerApp.handleRendering()` on `request.signal.aborted` before asset reads, `html:transform:pre` hooks, `renderAngular()`, and critical CSS inlining so aborted requests avoid unnecessary work.

In `renderAngular()`, check `moduleRef.injector.destroyed`, `applicationRef.destroyed`, `platformRef.destroyed`, and `destroyPromise` consistently across bootstrap, `whenStable()`, and `content()`, clear `renderTimeoutId` on every `destroy()` call, detach abort listeners before settling `content()`, and ensure `asyncDestroyPlatform()` always resolves.

@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 introduces early abort checks using request.signal.aborted throughout the Angular SSR and CSR rendering lifecycles to prevent unnecessary processing when a client disconnects. It also improves platform destruction safety and adds comprehensive unit tests to verify abort behavior. The feedback suggests extending the abort checks to the CSR path after HTML transformations for consistency with the SSR path.

Comment thread packages/angular/ssr/src/app.ts Resolved
@alan-agius4 alan-agius4 added action: review The PR is still awaiting reviews from at least one requested reviewer target: patch This PR is targeted for the next patch release labels Nov 20, 2026
@alan-agius4
alan-agius4 requested a review from dgp1130 November 20, 2026 13:07
) {
void destroy();

return;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Question: Do we need to call reject in these cases? Seems like we're not doing that now, but feels weird to leave a dangling promise.

);

expect(result.hasNavigationError).toBeFalse();
if (!('content' in result)) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggestion: Prefer expect over if (...) { fail(...) }.

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

Labels

action: review The PR is still awaiting reviews from at least one requested reviewer 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