Race condition in HeaderWriterFilter when returning StreamingResponseBody in Spring MVC #33304
Labels
for: external-project
Needs a fix in external project
in: web
Issues in web modules (web, webmvc, webflux, websocket)
Affects: 6.0.x, 6.1.x (list is not exhaustive)
When using
HeaderWriterFilter
and controller methods that returnStreamingResponseBody
, headers may be set twice.The following two things happen in parallel:
StreamingResponseBody
. If enough data is transferred to the response body, the response is forcefully committed. This causes theHeaderWriterFilter
to write headers from the asynchronous thread.HeaderWriterFilter
, which will write headers from the request handling thread.Both threads may successfully pass the
isDisableOnResponseCommitted()
check inHeaderWriterFilter.HeaderWriterResponse#writeHeaders
, causing theHeaderWriter
s to be invoked twice. This alone may already cause duplicate headers.It gets even worse: The implementation of
HttpServletResponse
is not guaranteed to be thread safe according to the Servlet Spec.When using Apache Tomcat, adding headers in parallel causes nasty race conditions (visible as
NullPointerException
s) that (because of garbage avoidance) may permanently break the inner workings of theirorg.apache.tomcat.util.http.MimeHeaders
class, leading to strange errors in threads that use recycled response instances.My current workaround: Write all headers and flush the response in the handler method.
The text was updated successfully, but these errors were encountered: