Repository navigation
fix(core): safely handle server platform destruction on aborted SSR requests - #72139
Open
alan-agius4 wants to merge 11 commits into
Open
alan-agius4 wants to merge 11 commits into
alan-agius4 wants to merge 11 commits into
Conversation
…nding tasks When a server platform is destroyed while bootstrap or pending async work is in flight (for example, when an SSR request is aborted), several core paths could throw `NG0205` or rethrow errors via `setTimeout`, crashing the Node.js process: - `ApplicationInitStatus`: unsubscribe observable initializers on `DestroyRef.onDestroy`, settle `donePromise` without marking `done = true`, stop remaining initializers if destroyed, and ignore late post-destroy rejections. - `bootstrap()`, `internalCreateApplication()`, and `PlatformRef.bootstrapModule()`: return the destroyed `ApplicationRef` / `NgModuleRef` without looking up `ENABLE_ROOT_COMPONENT_BOOTSTRAP` or bootstrapping root components when destroyed before or during initialization. - `PendingTasks.run()`: skip reporting rejections to `ErrorHandler` once `DestroyRef.destroyed` is true. - `INTERNAL_APPLICATION_ERROR_HANDLER`: when destroyed on the server without a cached `ErrorHandler`, log via `console.error` instead of throwing asynchronously from `setTimeout`.
Register a `DestroyRef.onDestroy` teardown in `FetchBackend.handle()` that calls `observer.unsubscribe()` to abort in-flight `fetch` requests and clear timeouts without emitting `complete()` or `error()`. This avoids RxJS `EmptyError` rejections in `firstValueFrom` / `lastValueFrom` and unhandled rejections in unawaited async lifecycle hooks when an SSR platform is destroyed mid-request. Also skip initiating `fetch` if `DestroyRef` is already destroyed, cancel unread or partially read response body streams after destruction, and remove the stray `observer.complete()` on stream cancellation.
Track the `navigationTransitions.setupNavigations(this)` subscription on `eventsSubscription` (after `subscribeToNavigationEvents()`) so `Router.dispose()` unsubscribes active navigation transitions and cancels in-flight guards, resolvers, and lazy loaders without dispatching events to a disposed router. Also skip starting `initialNavigation()` in `withEnabledBlockingInitialNavigation()` if the injector was destroyed while awaiting `LOCATION_INITIALIZED`.
…r destruction Return an empty string from `renderInternal()`, `renderApplication()`, and `renderModule()` when the platform or application is destroyed before or during async `BEFORE_APP_SERIALIZED` hooks, skip reporting post-destroy hook rejections to `ErrorHandler`, and ensure `asyncDestroyPlatform()` checks `platformRef.destroyed` and always resolves.
alan-agius4
requested review from
JeanMeche
and removed request for
crisbeto
November 20, 2026 07:46
… and pending tasks
JeanMeche
reviewed
Nov 20, 2026
JeanMeche
reviewed
Nov 20, 2026
JeanMeche
reviewed
Nov 20, 2026
JeanMeche
approved these changes
Nov 20, 2026
JeanMeche
requested review from
thePunderWoman
and removed request for
kirjs
November 21, 2026 12:31
Document that destroying the injector providing HttpClient (with the default fetch backend) aborts in-flight requests without emitting complete or error, so firstValueFrom/lastValueFrom promises stay unsettled and finally blocks do not run. Recommend DestroyRef.onDestroy() or takeUntilDestroyed() when cleaning up external resources such as locks or connections.
Contributor
Author
|
Caretaker note: g3 failure is pre-existing. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Checklist
PR Type
What is the current behavior?
When an SSR server immediately destroys
PlatformRefon request abort (e.g. via@angular/ssr), several framework paths can crash the Node.js process withuncaughtExceptionorunhandledRejection, or leave in-flight requests open:bootstrap()looks upENABLE_ROOT_COMPONENT_BOOTSTRAPafterApplicationInitStatus.donePromisewithout checkingenvInjector.destroyed, throwingNG0205.ApplicationInitStatusdoes not unsubscribe observable initializers or settledonePromisewhen destroyed.INTERNAL_APPLICATION_ERROR_HANDLERthrows viasetTimeout(() => { throw e; })wheninjector.destroyedand no prior error has cacheduserErrorHandler, terminating Node.js SSR servers.PendingTasks.run()reports post-destroy promise rejections toErrorHandler.FetchBackenddoes not abort in-flightfetchrequests onDestroyRef.onDestroy, and emittingcomplete()on stream cancellation triggersEmptyErrorinfirstValueFrom/lastValueFrom.Router.dispose()does not unsubscribe activesetupNavigationstransitions, leaving guards and resolvers running after platform teardown.renderInternal()reports rejections from asyncBEFORE_APP_SERIALIZEDhooks and callsrenderToString()even if the platform was destroyed mid-hook.Related issue: angular/angular-cli#34754
What is the new behavior?
@angular/core:ApplicationInitStatusunsubscribes observable initializers onDestroyRef.onDestroy, settlesdonePromisewithout settingdone = true, stops remaining initializers if destroyed, and ignores late post-destroy rejections.bootstrap(),internalCreateApplication(), andPlatformRef.bootstrapModule()settle cleanly with the destroyedApplicationRef/NgModuleRefwithout bootstrapping root components when destroyed before or during initialization.PendingTasks.run()checksDestroyRef.destroyedbefore reporting errors.INTERNAL_APPLICATION_ERROR_HANDLERlogs viaconsole.error('ERROR', e)on the server when destroyed without a cachedErrorHandlerinstead of throwing fromsetTimeout.@angular/common/http:FetchBackendunsubscribes onDestroyRef.onDestroyto abort in-flightfetchcalls and clear timeouts without emittingcomplete()orerror(), cancels body streams on destroy, and skips initiating requests when already destroyed.@angular/router:Router.dispose()unsubscribes activesetupNavigationstransitions (after detaching the navigation event listener) andwithEnabledBlockingInitialNavigation()skips starting navigation if the injector was destroyed while awaitingLOCATION_INITIALIZED.@angular/platform-server:renderInternal(),renderApplication(), andrenderModule()return''early when destroyed before or during asyncBEFORE_APP_SERIALIZEDhooks, suppress post-destroyErrorHandlerreporting, and ensureasyncDestroyPlatform()always resolves safely.Does this PR introduce a breaking change?
Other information
Companion
@angular/ssrPR: angular/angular-cli#34768Closes angular/angular-cli#34754