Skip to content

Commit

Permalink
Merge pull request #1317 from Netflix/fix-leaks
Browse files Browse the repository at this point in the history
Explicitly release bytebufs when reads are not propagated
  • Loading branch information
artgon authored Oct 12, 2022
2 parents 841a8e7 + cf6e8c6 commit 3d07002
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.AttributeKey;
import io.netty.util.ReferenceCountUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -78,6 +79,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
{
if (ctx.channel().attr(ATTR_CH_THROTTLED).get() != null) {
// Discard this msg as channel is in process of being closed.
ReferenceCountUtil.safeRelease(msg);
}
else {
super.channelRead(ctx, msg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.handler.codec.http2.DefaultHttp2ResetFrame;
import io.netty.handler.codec.http2.Http2Error;
import io.netty.util.ReferenceCountUtil;
import java.util.List;

/**
Expand Down Expand Up @@ -56,18 +57,21 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
List<String> lengthHeaders = req.headers().getAll(HttpHeaderNames.CONTENT_LENGTH);
if (lengthHeaders.size() > 1) {
ctx.writeAndFlush(new DefaultHttp2ResetFrame(Http2Error.PROTOCOL_ERROR));
ReferenceCountUtil.safeRelease(msg);
return;
} else if (lengthHeaders.size() == 1) {
expectedContentLength = Long.parseLong(lengthHeaders.get(0));
if (expectedContentLength < 0) {
// TODO(carl-mastrangelo): this is not right, but meh. Fix this to return a proper 400.
ctx.writeAndFlush(new DefaultHttp2ResetFrame(Http2Error.PROTOCOL_ERROR));
ReferenceCountUtil.safeRelease(msg);
return;
}
}
if (hasContentLength() && HttpUtil.isTransferEncodingChunked(req)) {
// TODO(carl-mastrangelo): this is not right, but meh. Fix this to return a proper 400.
ctx.writeAndFlush(new DefaultHttp2ResetFrame(Http2Error.PROTOCOL_ERROR));
ReferenceCountUtil.safeRelease(msg);
return;
}
}
Expand All @@ -77,13 +81,15 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
if (hasContentLength() && seenContentLength > expectedContentLength) {
// TODO(carl-mastrangelo): this is not right, but meh. Fix this to return a proper 400.
ctx.writeAndFlush(new DefaultHttp2ResetFrame(Http2Error.PROTOCOL_ERROR));
ReferenceCountUtil.safeRelease(msg);
return;
}
}
if (msg instanceof LastHttpContent) {
if (hasContentLength() && seenContentLength != expectedContentLength) {
// TODO(carl-mastrangelo): this is not right, but meh. Fix this to return a proper 400.
ctx.writeAndFlush(new DefaultHttp2ResetFrame(Http2Error.PROTOCOL_ERROR));
ReferenceCountUtil.safeRelease(msg);
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http2.Http2ResetFrame;
import io.netty.util.ReferenceCountUtil;

/**
* User: michaels@netflix.com
Expand All @@ -36,6 +37,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
if (msg instanceof Http2ResetFrame) {
// Inform zuul to cancel the request.
ctx.fireUserEventTriggered(new RequestCancelledEvent());
ReferenceCountUtil.safeRelease(msg);
}
else {
super.channelRead(ctx, msg);
Expand Down

0 comments on commit 3d07002

Please sign in to comment.