Skip to content

Commit

Permalink
[UNDERTOW-2448] At ServletPrintWriter.write(CharBuffer) do not mark e…
Browse files Browse the repository at this point in the history
…rror if buffer.remaining() == remaining after flush. We need to update remaining to do a proper check here.

The aforementioned == check can return true after a successfull flush, because "remaining" value is set to be the size of the bytes remaining in the buffer before encoding. If, at that stage, the buffer is empty, "remaining" value is set to buffer.capacity(). When encoding many bytes, we will end up with a full buffer. As a result, we will try to flush, and it is often the case that the buffer will be entirely cleared after flushing. This results in buffer.remaining() being the overall buffer size, i.e., buffer.capacity(). As a result, buffer.remaining() == remaining check returns true, and write is aborted, causing the broken responses we are seeing.

Signed-off-by: Flavia Rainone <[email protected]>
  • Loading branch information
fl4via committed Oct 15, 2024
1 parent 2d28090 commit 3c9eb96
Showing 1 changed file with 2 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,9 @@ public void write(final CharBuffer input) {
remainingContentLength -= writtenLength;
outputStream.updateWritten(writtenLength);
if (result.isOverflow() || !buffer.hasRemaining()) {
int remainingInBuffer = buffer.remaining();
outputStream.flushInternal();
if (buffer.remaining() == remaining) {
if (buffer.remaining() == remainingInBuffer) {
error = true;
return;
}
Expand Down

0 comments on commit 3c9eb96

Please sign in to comment.