Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that a large response does not cause OOM in RESTEasy Classic #20907

Merged
merged 1 commit into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public void write(ByteBuf data, boolean last) throws IOException {
return;
}
if (throwable != null) {
if (data != null && data.refCnt() > 0) {
data.release();
}
throw new IOException(throwable);
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,24 +158,30 @@ public CompletionStage<Void> asyncWrite(final byte[] b, final int off, final int
return ret;
}

int rem = len;
int idx = off;
ByteBuf buffer = pooledBuffer;
CompletionStage<Void> ret = CompletableFuture.completedFuture(null);
if (buffer == null) {
pooledBuffer = buffer = allocator.allocateBuffer();
int bufferSize = allocator.getBufferSize();
int bufferCount = len / bufferSize;
int remainder = len % bufferSize;
if (remainder != 0) {
bufferCount = bufferCount + 1;
}
while (rem > 0) {
int toWrite = Math.min(rem, buffer.writableBytes());
buffer.writeBytes(b, idx, toWrite);
rem -= toWrite;
idx += toWrite;
if (!buffer.isWritable()) {
ByteBuf tmpBuf = buffer;
this.pooledBuffer = buffer = allocator.allocateBuffer();
ret = ret.thenCompose(v -> response.writeNonBlocking(tmpBuf, false));

if (bufferCount == 1) {
pooledBuffer = allocator.allocateBuffer();
pooledBuffer.writeBytes(b);
} else {
for (int i = 0; i < bufferCount - 1; i++) {
int bufferIndex = i;
ret = ret.thenCompose(v -> {
ByteBuf tmpBuf = allocator.allocateBuffer();
tmpBuf.writeBytes(b, bufferIndex * bufferSize, bufferSize);
return response.writeNonBlocking(tmpBuf, false);
});
}
pooledBuffer = allocator.allocateBuffer();
pooledBuffer.writeBytes(b, (bufferCount - 1) * bufferSize, remainder);
}

return ret.thenCompose(v -> asyncUpdateWritten(len));
}

Expand Down