Skip to content

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

Merged
alan-agius4 merged 2 commits into
angular:mainfrom
alan-agius4:fix-ssr-abort-guards
Nov 23, 2026
Merged

alan-agius4 merged 2 commits 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
Comment thread packages/angular/ssr/src/utils/ng.ts Resolved
@alan-agius4 alan-agius4 added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Nov 23, 2026
@alan-agius4
alan-agius4 merged commit 59e3fd2 into angular:main Nov 23, 2026
39 checks passed
@alan-agius4
alan-agius4 deleted the fix-ssr-abort-guards branch November 23, 2026 10:38
@alan-agius4

Copy link
Copy Markdown
Collaborator Author

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

@SkyZeroZx

Copy link
Copy Markdown
Contributor

@alan-agius4 The process still ends with this changes and angular/angular#72139 merged. Repro on the main snapshot builds of both repos (@angular/ssr at de5620f18, the framework at f3ea683dd):

https://github.com/SkyZeroZx/angular-ssr-abort-crash-lab/tree/destroy-hook-after-fixes

It's the first example from the description plus an error handler, routed at / and /users/:id. The API answers in 100 ms and returns 404 for unknown users.

npm ci
npx ng build
node tools/destroy-hook.mjs
1. GET / -> 200 Alice; then a GET / closed after 50 ms
   server EXITED, code 1
      TypeError: Cannot read properties of undefined (reading 'unsubscribe')
2. GET /users/ghost -> 200 User not found, nothing aborted
   server EXITED, code 1
      TypeError: Cannot read properties of undefined (reading 'unsubscribe')

The second case doesn't need an abort. The call fails, the app handles it, the subscription is never created, and the destroy at the end of the render throws.

On destroy-hook-with-pr-34777 the same app runs with this PR applied: node tools/apply-pr-34777.mjs after npm ci adds the two catch lines to @angular/ssr, the same change a bazel build of this PR makes. Both requests log the error and the server keeps running:

1. GET / -> 200 Alice; then a GET / closed after 50 ms
   server alive
      An error occurred while destroying the platform. TypeError: Cannot read properties of undefined (reading 'unsubscribe')
2. GET /users/ghost -> 200 User not found, nothing aborted
   server alive
      An error occurred while destroying the platform. TypeError: Cannot read properties of undefined (reading 'unsubscribe')

The Express server from ng new --ssr stays up on both branches, since AngularNodeAppEngine installs process handlers.

I opened #34777 to fix it

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.

3 participants