Skip to content

Commit f4bb0c5

Browse files
committed
Release buffer when serialization throws exception
Motivation: We need to release the previous allocated buffer if serialization throws an exception as otherwise we will leak it. Modifications: Correctly release buffer if serialization throws an exception Result: No more buffer leak when serialization fails
1 parent aa708e0 commit f4bb0c5

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

codec-ohttp/src/main/java/io/netty/incubator/codec/ohttp/OHttpServerCodec.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import io.netty.buffer.Unpooled;
2525
import io.netty.channel.ChannelFutureListener;
2626
import io.netty.channel.ChannelHandlerContext;
27-
import io.netty.handler.codec.DecoderException;
28-
import io.netty.handler.codec.EncoderException;
2927
import io.netty.handler.codec.http.DefaultFullHttpResponse;
3028
import io.netty.handler.codec.http.DefaultHttpContent;
3129
import io.netty.handler.codec.http.DefaultHttpRequest;
@@ -235,14 +233,18 @@ protected final void encode(ChannelHandlerContext ctx, HttpObject msg, List<Obje
235233
}
236234
if (oHttpContext != null) {
237235
boolean isLast = msg instanceof LastHttpContent;
236+
ByteBuf contentBytes = ctx.alloc().buffer();
238237
try {
239-
ByteBuf contentBytes = ctx.alloc().buffer();
240238
oHttpContext.serialize(ctx.alloc(), msg, contentBytes);
241239
// Use the correct version of HttpContent depending on if it was the last or not.
242240
HttpContent content = isLast ? new DefaultLastHttpContent(contentBytes) :
243241
new DefaultHttpContent(contentBytes);
244242
out.add(content);
243+
contentBytes = null;
245244
} finally {
245+
if (contentBytes != null) {
246+
contentBytes.release();
247+
}
246248
if (isLast && oHttpContext.sendLastHttpContent()) {
247249
destroyContext();
248250
}

0 commit comments

Comments
 (0)