-
Notifications
You must be signed in to change notification settings - Fork 26.3k
fix(platform-server): destroy PlatformRef
when error happens during the bootstrap()
phase, e.g. in APP_INIIALIZER
- to prevent memory leaks in SSR
#58112
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
Conversation
Hi Krzysztof, thanks for looking into this. Could you please rebase your PR on the |
Thank you @JeanMeche for your reply. |
The changes are breaking the |
22b7bca
to
a13f1fb
Compare
Thank you @JeanMeche for letting me know. Explanation:
|
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.
LGTM, just a couple of NITs
… the `bootstrap()` phase The `bootstrap()` phase might fail e.g. due to an rejected promise in some `APP_INIIALIZER`. If `PlatformRef` is not destroyed, then the main app's injector is not destroyed and therefore `ngOnDestroy` hooks of singleton services is not called on the end (failure) of SSR. This could lead to possible memory leaks in custom SSR apps, if their singleton services' `ngOnDestroy` hooks contained an important teardown logic (e.g. unsubscribing from RxJS observable). Note: I needed to fix by the way another thing too: now we destroy `moduleRef` when `platformInjector` is destroyed - by setting a `PLATFORM_DESTROY_LISTENER` fixes angular#58111
@Platonn thanks for creating this PR 👍 It looks like there is a failing test (see the |
@AndrewKushnir It looks like the failing test is flaky. CI is fine after a re-run. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
PlatformRef
is not destroyed if an error happens during thebootstrap()
phase (e.g. inAPP_INITIALIZER
). Then the main app's injector is not destroyed and thereforengOnDestroy
hooks of singleton services were not called on the end (failure) of SSR.Note: This could lead to possible memory leaks in custom SSR apps, if their singleton services'
ngOnDestroy
hooks contained an important teardown logic (e.g. unsubscribing from RxJS observable).Issue Number: #58111
What is the new behavior?
PlatformRef
is guaranteed to be destroyed inrenderApplication()
andrenderModule()
functions of@angular/platform-server
both in the case of successful render and in the case of a failure (resulting in a rejected promise)PLATFORM_DESTROY_LISTENERS
that destroys the app's main injector is now set not only in case ofrenderApplication()
, but also in case ofrenderModule()
Does this PR introduce a breaking change?
Other information