ResponseBodyEmitter
relies on synchronization to protect against concurrent use of the response, and also for the caching of early messages (e.g. before the emitter is returned from the controller method) to be properly detected from another thread that sends after initialization.
After #32340, protection against concurrent use of the response is built deeper at the level of StandardServletAsyncWebRequest
, so we can revisit ResponseBodyEmitter
and leave only synchronization that is actually necessary.
See also https://github.com/spring-projects/spring-framework/issues/34762#issuecomment-2809047490 for related efforts in 6.2.x to optimize performance.
Comment From: rstoyanchev
StandardServletAsyncWebRequest
actually doesn't guard against concurrent use of sending threads, but only against a race with the Servlet container in error handling, and continue use of the response after the connection is closed. That means we do need the synchronisation.
Looking once again at the performance optimization in #33831, it reduced synchronisation on lifecycle methods, but not on the send methods, which is where it matters the most. Therefore I don't think it's worth revisiting this.