Skip to content

Commit

Permalink
Fix memory leaks when processing EmptyLastHttpContent
Browse files Browse the repository at this point in the history
Fix memory leak in test
  • Loading branch information
violetagg committed Sep 5, 2022
1 parent b61cf1b commit 24e6188
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
7 changes: 2 additions & 5 deletions build.gradle
Expand Up @@ -274,11 +274,8 @@ subprojects {

onOutput { descriptor, event ->
def evMsg = event.message
if (evMsg.contains("ResourceLeakDetector")) {
if (!evMsg.contains(" -Dio.netty.leakDetection")
&& !evMsg.contains("DEBUG io.netty.util.ResourceLeakDetectorFactory")) {
logger.error("ERROR: Test: " + descriptor + " produced resource leak: " + event.message)
}
if (evMsg.contains("LoggingLeakCallback")) {
logger.error("ERROR: Test: " + descriptor + " produced resource leak: " + event.message)
}
}
}
Expand Down
Expand Up @@ -646,14 +646,17 @@ protected void onInboundNext(ChannelHandlerContext ctx, Object msg) {
if (log.isDebugEnabled()) {
log.debug(format(channel(), "Received last HTTP packet"));
}
if (!(msg instanceof EmptyLastHttpContent)) {
if (!(msg instanceof EmptyLastHttpContent emptyLastHttpContent)) {
if (redirecting != null) {
Resource.dispose(msg);
}
else {
super.onInboundNext(ctx, msg);
}
}
else {
emptyLastHttpContent.close();
}

if (redirecting == null) {
// EmitResult is ignored as it is guaranteed that there will be only one emission of LastHttpContent
Expand Down
Expand Up @@ -576,9 +576,12 @@ protected void onInboundNext(ChannelHandlerContext ctx, Object msg) {
return;
}
if (msg instanceof HttpContent) {
if (!(msg instanceof EmptyLastHttpContent)) {
if (!(msg instanceof EmptyLastHttpContent emptyLastHttpContent)) {
super.onInboundNext(ctx, msg);
}
else {
emptyLastHttpContent.close();
}
if (msg instanceof LastHttpContent) {
//force auto read to enable more accurate close selection now inbound is done
channel().setOption(ChannelOption.AUTO_READ, true);
Expand Down
Expand Up @@ -172,7 +172,10 @@ private void doTestStatus(HttpResponseStatus status) {
HttpClientOperations ops = new HttpClientOperations(() -> channel,
ConnectionObserver.emptyListener(),
ClientCookieEncoder.STRICT, ClientCookieDecoder.STRICT);
ops.setNettyResponse(new DefaultFullHttpResponse(HTTP_1_1, status, channel.bufferAllocator().allocate(0)));
assertThat(ops.status().reasonPhrase()).isEqualTo(status.reasonPhrase());
try (DefaultFullHttpResponse response =
new DefaultFullHttpResponse(HTTP_1_1, status, channel.bufferAllocator().allocate(0))) {
ops.setNettyResponse(response);
assertThat(ops.status().reasonPhrase()).isEqualTo(status.reasonPhrase());
}
}
}

0 comments on commit 24e6188

Please sign in to comment.