MYFACES-4751 Destroy @ViewScoped beans of views that are never saved#995
Merged
Conversation
A view rendered without writing its state - e.g. a page with no UIForm, so no view state token is emitted - can never be restored, and unlike a saved view it is never registered in the session SerializedViewCollection. Consequently the normal "evict view -> destroy its view scope" path never ran for such a view, and any @ViewScoped beans created while rendering it lingered in the session (ViewScopeContextualStorageHolder) until the session expired. Requesting such a page repeatedly within one session therefore leaked one view-scope storage per request, unbounded. FaceletViewDeclarationLanguage.renderView now destroys the view scope of a non-transient view whose state was not written, at the end of the request, publishing PreDestroyViewMapEvent and destroying its (CDI or non-CDI) @ViewScoped storage. Add ViewScopeFormlessLeakTestCase: rendering a formless page bound to a @ViewScoped bean N times now destroys the bean N times instead of accumulating it in the session. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Member
|
thanks @BalusC! @melloware can you please port it to 4.0 and 5.0, assign the targets to the issue and merge all + close the issue? |
Contributor
|
@tandraschko yep i will take care of it! |
melloware
approved these changes
May 29, 2026
This was referenced May 29, 2026
melloware
added a commit
to melloware/myfaces
that referenced
this pull request
May 29, 2026
…ved (apache#995) A view rendered without writing its state - e.g. a page with no UIForm, so no view state token is emitted - can never be restored, and unlike a saved view it is never registered in the session SerializedViewCollection. Consequently the normal "evict view -> destroy its view scope" path never ran for such a view, and any @ViewScoped beans created while rendering it lingered in the session (ViewScopeContextualStorageHolder) until the session expired. Requesting such a page repeatedly within one session therefore leaked one view-scope storage per request, unbounded. FaceletViewDeclarationLanguage.renderView now destroys the view scope of a non-transient view whose state was not written, at the end of the request, publishing PreDestroyViewMapEvent and destroying its (CDI or non-CDI) @ViewScoped storage. Add ViewScopeFormlessLeakTestCase: rendering a formless page bound to a @ViewScoped bean N times now destroys the bean N times instead of accumulating it in the session. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
melloware
added a commit
that referenced
this pull request
May 29, 2026
…ved (#995) (#997) A view rendered without writing its state - e.g. a page with no UIForm, so no view state token is emitted - can never be restored, and unlike a saved view it is never registered in the session SerializedViewCollection. Consequently the normal "evict view -> destroy its view scope" path never ran for such a view, and any @ViewScoped beans created while rendering it lingered in the session (ViewScopeContextualStorageHolder) until the session expired. Requesting such a page repeatedly within one session therefore leaked one view-scope storage per request, unbounded. FaceletViewDeclarationLanguage.renderView now destroys the view scope of a non-transient view whose state was not written, at the end of the request, publishing PreDestroyViewMapEvent and destroying its (CDI or non-CDI) @ViewScoped storage. Add ViewScopeFormlessLeakTestCase: rendering a formless page bound to a @ViewScoped bean N times now destroys the bean N times instead of accumulating it in the session. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
melloware
added a commit
that referenced
this pull request
May 29, 2026
…ved (#995) (#996) A view rendered without writing its state - e.g. a page with no UIForm, so no view state token is emitted - can never be restored, and unlike a saved view it is never registered in the session SerializedViewCollection. Consequently the normal "evict view -> destroy its view scope" path never ran for such a view, and any @ViewScoped beans created while rendering it lingered in the session (ViewScopeContextualStorageHolder) until the session expired. Requesting such a page repeatedly within one session therefore leaked one view-scope storage per request, unbounded. FaceletViewDeclarationLanguage.renderView now destroys the view scope of a non-transient view whose state was not written, at the end of the request, publishing PreDestroyViewMapEvent and destroying its (CDI or non-CDI) @ViewScoped storage. Add ViewScopeFormlessLeakTestCase: rendering a formless page bound to a @ViewScoped bean N times now destroys the bean N times instead of accumulating it in the session. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
MYFACES-4751
Problem
Discovered in eclipse-ee4j/mojarra#5753 (comment). A Facelets view rendered without writing its state — e.g. a page with no
UIForm, so no view state token is emitted — can never be restored. Unlike a saved view, it is never registered in the sessionSerializedViewCollection, so the usual evict view → destroy its view scope cleanup never runs for it. Any@ViewScopedbeans created while rendering it therefore linger in the session (ViewScopeContextualStorageHolder) until the session expires; no@PreDestroyis ever invoked.Requesting such a page repeatedly within one session leaks one view-scope storage per request, unbounded. With a moderately large view (e.g. a ~2000-row table bound to a
@ViewScopedbean) refreshed a few hundred times, this exhausts the heap (OutOfMemoryError). A heap dump shows ~N retainedContextualStorage/@ViewScopedbean instances (one per request) where onlyNUMBER_OF_VIEWS_IN_SESSIONshould be retained.Root cause
Server-side
@ViewScopedcleanup is driven solely bySerializedViewCollectioneviction (ViewScopeContext.destroyAll(viewScopeId)). A view whose state is never written is never put into that collection (StateCacheServerSide's save path isn't reached), so its lazily-created view-scope storage is orphaned. InFaceletViewDeclarationLanguage.renderViewthis is the "GET case without any form that triggers state saving" branch, which never destroyed the view scope.Fix
In that branch, destroy the view scope of a non-transient view whose state was not written, at the end of the request, via
viewMap.clear()— which publishesPreDestroyViewMapEventand destroys the (CDI or non-CDI)@ViewScopedstorage.Test
ViewScopeFormlessLeakTestCaserenders a formless page bound to a@ViewScopedbean N times in one session and asserts the bean is destroyed N times (its@PreDestroyruns) instead of accumulating. It fails without the fix (expected: <5> but was: <0>) and passes with it.Notes
Reproduced and verified on a real deployment (TomEE WebProfile, server-side state saving): before the fix the formless GET-refresh OOM-thrashed; after, the retained
@ViewScopedstorage stays bounded and the workload completes in a 512 MB heap. The issue is not 4.1.x-specific — the 4.0.x line exhibits it too — so it may warrant backporting to 4.0.x.