Skip to content

Commit 7664a9f

Browse files
committed
fix: prevent memory leak when http response is not used
1 parent e26ea94 commit 7664a9f

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

web-impl-netty/src/main/java/eu/cloudnetservice/ext/rest/netty/NettyHttpServerHandler.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import io.netty5.util.Resource;
4444
import io.netty5.util.Send;
4545
import io.netty5.util.concurrent.Future;
46+
import io.netty5.util.internal.SilentDispose;
4647
import java.net.URI;
4748
import java.net.URISyntaxException;
4849
import java.time.Instant;
@@ -242,6 +243,7 @@ private void handleMessage(
242243
}
243244

244245
// check if the response set in the context should actually be transferred to the client
246+
var closeResponse = true;
245247
if (!context.cancelSendResponse) {
246248
var response = context.httpServerResponse;
247249

@@ -271,6 +273,7 @@ private void handleMessage(
271273
HttpUtil.setTransferEncodingChunked(netty, false);
272274
HttpUtil.setContentLength(netty, netty.payload().readableBytes());
273275
future = channel.writeAndFlush(netty);
276+
closeResponse = false; // already done by http content encoder
274277
}
275278

276279
// add the listener that fires the exception if an error occurs during writing of the response
@@ -279,6 +282,11 @@ private void handleMessage(
279282
future.addListener(channel, ChannelFutureListeners.CLOSE);
280283
}
281284
}
285+
286+
if (closeResponse) {
287+
var response = context.httpServerResponse.httpResponse;
288+
SilentDispose.tryPropagatingDispose(response);
289+
}
282290
}
283291

284292
private void postToFallbackHandler(@NonNull NettyHttpServerContext context) {

0 commit comments

Comments
 (0)