Skip to content

Destroy successfully initialized filters if CompositeFilter.init fails - #37459

Open
lejuho wants to merge 1 commit into
spring-projects:mainfrom
lejuho:composite-filter-init-cleanup
Open

lejuho wants to merge 1 commit into
spring-projects:mainfrom
lejuho:composite-filter-init-cleanup

Conversation

@lejuho

@lejuho lejuho commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Overview

Follow-up to #37445 / #37430.

CompositeFilter.init() initializes nested filters in order. If a later filter's init throws, the Servlet container does not call destroy() on the composite (the Servlet specification only invokes destroy after a successful init). Filters that had already been initialized were therefore left without cleanup.

Changes

  • On init failure, destroy filters that were already initialized, in reverse order, before rethrowing.
  • Add any destroy() failures as suppressed exceptions on the original failure.
  • Do not call destroy() on the filter whose init failed.

Testing

Added tests covering cleanup order, skipped destroy for the failing filter, suppressed destroy failures, and RuntimeException / Error from init.

./gradlew :spring-web:test --tests "org.springframework.web.filter.CompositeFilterTests" :spring-web:checkstyleMain :spring-web:checkstyleTest

Result: BUILD SUCCESSFUL (Checkstyle passes).

Related

If a nested filter's init() throws, the Servlet container will not call
destroy() on the CompositeFilter. Previously, filters that had already
been initialized were left without cleanup.

Destroy those earlier filters in reverse order before rethrowing, and
add any destroy() failures as suppressed exceptions. Do not destroy the
filter whose init() failed, matching the Servlet specification.

See spring-projectsgh-37445

Signed-off-by: lejuho <zedd1324@gmail.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 12, 2026
@sbrannen sbrannen added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Oct 12, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: web Issues in web modules (web, webmvc, webflux, websocket) status: waiting-for-triage An issue we've not yet triaged or decided on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants