We found a difference in when request scoped beans are destructed between tomcat and undertow. Normally when a session is redirected or a new request is made the bean is destroyed before the user gets its result back. When using a redirect: as a result it does the same in tomcat, but in undertow it returns the result before the request scope beans are destoyed. All the versions we tested it in where affected.

The oldest version we tested it in was: spring-boot 2.3.12.RELEASE / spring-framework 5.2.24.RELEASE

And the newest version we tested it in was: spring-boot 3.1.2 / spring-framework 6.0.11

I have attached a multi maven project with a project for tomcat and a project for undertow in it.

requestscopeddestruction.zip

Comment From: vdotjansen

I am not sure if this is a spring-boot or a spring-framerwork issue. I am hope I file the issue at the correct project.

Comment From: vdotjansen

Could someone have a look at this issue?

Comment From: jhoeller

At the Spring Framework level, this is triggered by a requestCompleted() call on the corresponding RequestAttributes instance. This can happen from three places: commonly DispatcherServlet itself at the end of its service call, or RequestContextListener/RequestContextFilter when configured in the web app (overriding the DispatcherServlet part there).

There is nothing we do specific to the target server there at the Spring MVC level there. I suspect that the runtime difference above comes from Tomcat vs Undertow triggering the ServletRequestListener.requestDestroyed() callback at different times; in that respect, RequestContextListener can have server-dependent behavior, unfortunately.

Comment From: vdotjansen

Alright so not a bug. It might be good to note somewhere in the documentation that there might be differences on based Application Server.

Comment From: jhoeller

We are currently investigating the concrete difference between Tomcat and Undertow. Either way, it is certainly a difference in web server behavior, possibly also in interaction with the HttpServletResponse.sendRedirect handoff flushing immediately vs at the end of the request processing cycle (when Servlet listeners and filters are triggered).

Comment From: vdotjansen

We are currently investigating the concrete difference between Tomcat and Undertow.

This is very interesting. We are really interested in the differences as our customers use either JBoss EAP or Tomcat and will benefit everyone.