From a8aa34abab8b93b70c574a2f884ff9c205751045 Mon Sep 17 00:00:00 2001 From: Santiago Pericasgeertsen Date: Tue, 1 Feb 2022 15:16:17 -0500 Subject: [PATCH] Do not create close listener handlers for every new request (#3853) * Do not create listener handlers for every new request resulting in a memory leak. Use using channelInactive() instead. Signed-off-by: Santiago Pericasgeertsen * Ensure publisher is not null Co-authored-by: Daniel Kec Co-authored-by: Daniel Kec --- .../helidon/webclient/NettyClientHandler.java | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/webclient/webclient/src/main/java/io/helidon/webclient/NettyClientHandler.java b/webclient/webclient/src/main/java/io/helidon/webclient/NettyClientHandler.java index 2decdac6eb2..44f538756fd 100644 --- a/webclient/webclient/src/main/java/io/helidon/webclient/NettyClientHandler.java +++ b/webclient/webclient/src/main/java/io/helidon/webclient/NettyClientHandler.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2021 Oracle and/or its affiliates. + * Copyright (c) 2020, 2022 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -154,16 +154,6 @@ protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws IO } } - channel.closeFuture() - .addListener(f -> { - // Connection closed without last HTTP content received. Some server problem - // so we need to fail the publisher and report an exception. - if (!responseCloser.isClosed()) { - WebClientException exception = new WebClientException("Connection reset by the host"); - publisher.fail(exception); - } - }); - requestConfiguration.cookieManager().put(requestConfiguration.requestURI(), clientResponse.headers().toMap()); @@ -236,6 +226,17 @@ protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws IO } } + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + super.channelInactive(ctx); + // Connection closed without last HTTP content received. Some server problem + // so we need to fail the publisher and report an exception. + if (publisher != null && !responseCloser.isClosed()) { + WebClientException exception = new WebClientException("Connection reset by the host"); + publisher.fail(exception); + } + } + private boolean shouldResponseAutomaticallyClose(WebClientResponse clientResponse) { WebClientResponseHeaders headers = clientResponse.headers(); if (clientResponse.status() == Http.Status.NO_CONTENT_204) {