fix(platform-server): destroy PlatformRef when error happens during the bootstrap() phase, e.g. in APP_INIIALIZER - to prevent memory leaks in SSR#58112
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:
|
alan-agius4
left a comment
There was a problem hiding this comment.
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?
PlatformRefis not destroyed if an error happens during thebootstrap()phase (e.g. inAPP_INITIALIZER). Then the main app's injector is not destroyed and thereforengOnDestroyhooks 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'
ngOnDestroyhooks contained an important teardown logic (e.g. unsubscribing from RxJS observable).Issue Number: #58111
What is the new behavior?
PlatformRefis guaranteed to be destroyed inrenderApplication()andrenderModule()functions of@angular/platform-serverboth in the case of successful render and in the case of a failure (resulting in a rejected promise)PLATFORM_DESTROY_LISTENERSthat 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