Repository navigation
fix(@angular/ssr): destroy platform when request is aborted during rendering - #34568
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces support for aborting the SSR rendering process using an AbortSignal across AngularNodeAppEngine, AngularAppEngine, and AngularServerApp. When a request is aborted, the underlying Angular platform is destroyed to halt any in-progress rendering, and a DOMException with an 'AbortError' is thrown. The feedback suggests introducing an isDestroyed guard flag in renderAngular to prevent redundant platform destruction when destroy() is called multiple times, and simplifying the catch block by reusing this updated destroy() function.
This comment was marked as resolved.
This comment was marked as resolved.
…ndering Previously, aborting `request.signal` during server-side rendering caused `AngularServerApp.handle()` to reject via `promiseWithAbort`, while `renderAngular()` continued running in the background until `whenStable()` completed (or leaked the platform if a pending task never resolved). This commit forwards `request.signal` into `renderAngular()` so that aborting an in-flight request destroys the underlying `PlatformRef`, cancels any pending render timer, and exits cleanly. Additionally, `promiseWithAbort()` now attaches rejection handlers to the underlying promise even when the signal is already aborted to avoid unhandled promise rejections. Closes angular#34567
d079bbc to
322f09f
Compare
| } finally { | ||
| if (hasNavigationError || redirectTo) { | ||
| void asyncDestroyPlatform(platformRef); | ||
| void destroy(); |
There was a problem hiding this comment.
Question: Is there a risk this calls destroy twice across the catch and finally clauses?
There was a problem hiding this comment.
We can actually remove the catch block altogether since finally already handles calling destroy() when an error occurs (as hasNavigationError remains true). Done.
| }; | ||
| } catch (error) { | ||
| await asyncDestroyPlatform(platformRef); | ||
| await destroy(); |
There was a problem hiding this comment.
Consider: Seems preexisting, but this is the only destroy call which gets await-ed rather than dropped with void. Is that intentional?
For that matter, would it be safer to await each one? Do we really want to drop that promise chain on the floor?
There was a problem hiding this comment.
Removed the catch block since finally handles it. For the void destroy() calls, this is intentional so we don't block rendering or returning the response while waiting for the platform to be destroyed.
| inject, | ||
| } from '@angular/core'; | ||
| import { ActivatedRoute, CanActivateFn, Router } from '@angular/router'; | ||
| import { setTimeout } from 'node:timers/promises'; |
There was a problem hiding this comment.
TIL about node:timers/promises. 😅
Previously, aborting
request.signalduring server-side rendering causedAngularServerApp.handle()to reject viapromiseWithAbort, whilerenderAngular()continued running in the background untilwhenStable()completed (or leaked the platform if a pending task never resolved).This commit forwards
request.signalintorenderAngular()so that aborting an in-flight request destroys the underlyingPlatformRef, cancels any pending render timer, and exits cleanly. Additionally,promiseWithAbort()now attaches rejection handlers to the underlying promise even when the signal is already aborted to avoid unhandled promise rejections.Closes #34567