Repository navigation
fix(@angular/ssr): destroy platform when request is aborted during rendering #34568
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alan-agius4
merged 3 commits into
angular:main
from
alan-agius4:fix/ssr-abort-destroy-platform
Oct 28, 2026
+210
−30
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
322f09f
fix(@angular/ssr): destroy platform when request is aborted during re…
alan-agius4 a984205
fixup! fix(@angular/ssr): destroy platform when request is aborted du…
alan-agius4 212ad75
fixup! fix(@angular/ssr): destroy platform when request is aborted du…
alan-agius4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,15 @@ import '@angular/compiler'; | |
| /* eslint-enable import/no-unassigned-import */ | ||
|
|
||
| import { APP_BASE_HREF } from '@angular/common'; | ||
| import { Component, PlatformRef, REQUEST, RESPONSE_INIT, inject } from '@angular/core'; | ||
| import { | ||
| Component, | ||
| DestroyRef, | ||
| PendingTasks, | ||
| PlatformRef, | ||
| REQUEST, | ||
| RESPONSE_INIT, | ||
| inject, | ||
| } from '@angular/core'; | ||
| import { ActivatedRoute, CanActivateFn, Router } from '@angular/router'; | ||
| import { setTimeout } from 'node:timers/promises'; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL about |
||
| import { AngularServerApp } from '../src/app'; | ||
|
|
@@ -21,16 +29,21 @@ import { setAngularAppTestingManifest } from './testing-utils'; | |
|
|
||
| describe('AngularServerApp', () => { | ||
| let app: AngularServerApp; | ||
| let slowRouteHook: (() => void) | undefined; | ||
|
|
||
| @Component({ | ||
| selector: 'app-home', | ||
| template: `Home works`, | ||
| }) | ||
| class HomeComponent { | ||
| constructor() { | ||
| if (inject(ActivatedRoute).snapshot.data['destroyApp']) { | ||
| const data = inject(ActivatedRoute).snapshot.data; | ||
| if (data['destroyApp']) { | ||
| inject(PlatformRef).destroy(); | ||
| } | ||
| if (data['slow']) { | ||
| slowRouteHook?.(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -78,6 +91,7 @@ describe('AngularServerApp', () => { | |
| { path: 'page-with-headers', component: HomeComponent }, | ||
| { path: 'page-with-status', component: HomeComponent }, | ||
| { path: 'page-destroy-app', component: HomeComponent, data: { destroyApp: true } }, | ||
| { path: 'page-slow', component: HomeComponent, data: { slow: true } }, | ||
| { path: 'redirect', redirectTo: 'home' }, | ||
| { path: 'redirect-via-navigate', component: RedirectComponent }, | ||
| { | ||
|
|
@@ -313,14 +327,106 @@ describe('AngularServerApp', () => { | |
| controller.abort(); | ||
| }); | ||
|
|
||
| await expectAsync(app.handle(request)).toBeRejectedWithError( | ||
| DOMException, | ||
| /Request for: http:\/\/localhost\/home was aborted/, | ||
| ); | ||
| }); | ||
|
|
||
| it('should destroy the platform and stop rendering when an in-flight request is aborted', async () => { | ||
| let resolveTaskReady!: () => void; | ||
| const taskReady = new Promise<void>((resolve) => { | ||
| resolveTaskReady = resolve; | ||
| }); | ||
| let appDestroyed = false; | ||
| let destroySpy: jasmine.Spy | undefined; | ||
|
|
||
| slowRouteHook = () => { | ||
| destroySpy = spyOn(inject(PlatformRef), 'destroy').and.callThrough(); | ||
| const pendingTasks = inject(PendingTasks); | ||
| const removeTask = pendingTasks.add(); | ||
| inject(DestroyRef).onDestroy(() => { | ||
| appDestroyed = true; | ||
| removeTask(); | ||
| }); | ||
| resolveTaskReady(); | ||
| }; | ||
|
|
||
| try { | ||
| await app.handle(request); | ||
| throw new Error('Should not be called.'); | ||
| } catch (e) { | ||
| expect(e).toBeInstanceOf(DOMException); | ||
| const controller = new AbortController(); | ||
| const request = new Request('http://localhost/page-slow', { signal: controller.signal }); | ||
|
|
||
| const handlePromise = app.handle(request); | ||
|
|
||
| // Wait until the component is instantiated and holding a pending task | ||
| await taskReady; | ||
| expect(appDestroyed).toBeFalse(); | ||
|
|
||
| // Abort the request while `applicationRef.whenStable()` is still pending | ||
| controller.abort(); | ||
|
|
||
| await expectAsync(handlePromise).toBeRejectedWithError( | ||
| DOMException, | ||
| /Request for: http:\/\/localhost\/page-slow was aborted/, | ||
| ); | ||
|
|
||
| // Wait for the macrotask queue to clear since platform destruction is scheduled via setTimeout(0) | ||
| await setTimeout(0); | ||
| expect(destroySpy).toHaveBeenCalledTimes(1); | ||
| expect(appDestroyed).toBeTrue(); | ||
| } finally { | ||
| slowRouteHook = undefined; | ||
| } | ||
| }); | ||
|
|
||
| it('should not bootstrap the platform if the request signal is already aborted', async () => { | ||
| // Warm up the router so route extraction does not create a PlatformRef during this test | ||
| const warmupResponse = await app.handle(new Request('http://localhost/home')); | ||
| await warmupResponse?.text(); | ||
| await setTimeout(0); | ||
|
|
||
| const destroySpy = spyOn(PlatformRef.prototype, 'destroy').and.callThrough(); | ||
| const controller = new AbortController(); | ||
| controller.abort(); | ||
|
|
||
| const request = new Request('http://localhost/home', { signal: controller.signal }); | ||
| await expectAsync(app.handle(request)).toBeRejectedWithError(DOMException); | ||
|
|
||
| await setTimeout(0); | ||
| expect(destroySpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should clean up the abort event listener on request.signal after rendering completes', async () => { | ||
| const controller = new AbortController(); | ||
| const request = new Request('http://localhost/home', { signal: controller.signal }); | ||
| const removeEventListenerSpy = spyOn( | ||
| request.signal, | ||
| 'removeEventListener', | ||
| ).and.callThrough(); | ||
|
|
||
| const response = await app.handle(request); | ||
| expect(await response?.text()).toContain('Home works'); | ||
| await setTimeout(0); | ||
|
|
||
| expect(removeEventListenerSpy).toHaveBeenCalledWith('abort', jasmine.any(Function)); | ||
| }); | ||
|
|
||
| it('should reject reading response body if request is aborted after handle() resolves but before rendering completes', async () => { | ||
| const controller = new AbortController(); | ||
| const response = await app.handle( | ||
| new Request('http://localhost/home', { signal: controller.signal }), | ||
| ); | ||
| expect(response).not.toBeNull(); | ||
|
|
||
| controller.abort(); | ||
|
|
||
| await expectAsync(response?.text()).toBeRejectedWithError( | ||
| DOMException, | ||
| /Request for: http:\/\/localhost\/home was aborted/, | ||
| ); | ||
| await setTimeout(0); | ||
| }); | ||
|
|
||
| it('should return configured headers for pages with specific header settings', async () => { | ||
| const response = await app.handle(new Request('http://localhost/page-with-headers')); | ||
| const headers = response?.headers.entries() ?? []; | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: Is there a risk this calls
destroytwice across thecatchandfinallyclauses?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can actually remove the
catchblock altogether sincefinallyalready handles callingdestroy()when an error occurs (ashasNavigationErrorremainstrue). Done.