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

Rejecting pipelined requests may cause a leak #1304

Merged
merged 1 commit into from
Sep 14, 2022
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 @@ -25,6 +25,7 @@
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.util.ReferenceCountUtil;

/**
* @author michaels
Expand All @@ -41,6 +42,8 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
// continue to next handler in pipeline.
if (fireStartEvent(ctx, (HttpRequest) msg)) {
super.channelRead(ctx, msg);
} else {
ReferenceCountUtil.release(msg);
}
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,24 @@

package com.netflix.netty.common;

import static com.netflix.netty.common.HttpLifecycleChannelHandler.ATTR_HTTP_PIPELINE_REJECT;
import static org.junit.Assert.fail;
import com.google.common.truth.Truth;
import com.netflix.netty.common.HttpLifecycleChannelHandler.CompleteEvent;
import com.netflix.netty.common.HttpLifecycleChannelHandler.CompleteReason;
import com.netflix.netty.common.HttpLifecycleChannelHandler.State;
import com.netflix.netty.common.HttpServerLifecycleChannelHandler.HttpServerLifecycleInboundChannelHandler;
import com.netflix.netty.common.HttpServerLifecycleChannelHandler.HttpServerLifecycleOutboundChannelHandler;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.UnpooledByteBufAllocator;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.ReferenceCountUtil;
import org.junit.Test;

public class HttpServerLifecycleChannelHandlerTest {
Expand Down Expand Up @@ -52,7 +62,7 @@ public void completionEventReasonIsUpdatedOnPipelineReject() {

channel.attr(HttpLifecycleChannelHandler.ATTR_STATE).set(State.STARTED);
// emulate pipeline rejection
channel.attr(HttpLifecycleChannelHandler.ATTR_HTTP_PIPELINE_REJECT).set(Boolean.TRUE);
channel.attr(ATTR_HTTP_PIPELINE_REJECT).set(Boolean.TRUE);
// Fire close
channel.pipeline().close();

Expand All @@ -72,4 +82,26 @@ public void completionEventReasonIsCloseByDefault() {

Truth.assertThat(reasonHandler.getCompleteEvent().getReason()).isEqualTo(CompleteReason.CLOSE);
}

@Test
public void pipelineRejectReleasesIfNeeded() {

EmbeddedChannel channel = new EmbeddedChannel(new HttpServerLifecycleInboundChannelHandler());

ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
try {
Truth.assertThat(buffer.refCnt()).isEqualTo(1);
FullHttpRequest httpRequest = new DefaultFullHttpRequest(
HttpVersion.HTTP_1_1, HttpMethod.GET, "/whatever", buffer);
channel.attr(HttpLifecycleChannelHandler.ATTR_STATE).set(State.STARTED);
channel.writeInbound(httpRequest);

Truth.assertThat(channel.attr(ATTR_HTTP_PIPELINE_REJECT).get()).isEqualTo(Boolean.TRUE);
Truth.assertThat(buffer.refCnt()).isEqualTo(0);
} finally {
if(buffer.refCnt() != 0) {
ReferenceCountUtil.release(buffer);
}
}
}
}