Fix race condition in HeaderWriterFilter when using asynchronous processing - #19705
Open
justin-tay wants to merge 1 commit into
Open
justin-tay wants to merge 1 commit into
justin-tay wants to merge 1 commit into
Conversation
HeaderWriterFilter writes headers in a finally block once the filter chain returns, and again when the response is committed. With asynchronous processing, such as a StreamingResponseBody, these happen on different threads. The AtomicBoolean added previously stops the headers being written twice, but the thread that loses the race still commits the response immediately, so the servlet container can write out the headers while the other thread is modifying them. On Tomcat this produces a header with an empty name. Synchronize writeHeaders() so that the commit waits until the headers have been written. Closes spring-projectsgh-15510 Signed-off-by: Justin Tay <49700559+justin-tay@users.noreply.github.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.
Closes gh-15510
This fixes the issue where the headers are still being modified in the main thread by
HeaderWriterFilterwhile the asynchronous processing might have completed and flushed the response. TheAtomicBooleanadded previously stops the headers being written twice but doesn't fix this issue.On Tomcat this sometimes produces a header with an empty name which certain clients reject.
In
org.apache.tomcat.util.http.MimeHeadersif the following sequence happensWith
org.apache.coyote.http11.Http11OutputBufferThis causes just
:to appear as a header and I ended up getting this in my client.