You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by "arturobernalg (via GitHub)" <gi...@apache.org> on 2026/03/05 15:25:33 UTC

[PR] HTTPCLIENT-2416 - Fix pool entry leak on late lease completion [httpcomponents-client]

arturobernalg opened a new pull request, #809:
URL: https://github.com/apache/httpcomponents-client/pull/809

   This patch fixes a race in PoolingHttpClientConnectionManager#lease().
   
   When LeaseRequest#get(...) exits with TimeoutException or InterruptedException, the current code calls leaseFuture.cancel(true). If the underlying lease completes just before cancellation is observed, cancel(true) returns false and the PoolEntry can remain leased, because no ConnectionEndpoint was returned to the caller and nothing releases it.
   
   The fix handles that case in the exceptional path: if cancel(true) returns false, the code attempts a zero-timeout get() on the lease future and, if a late-completed PoolEntry is obtained, releases it immediately back to the pool.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org


Re: [PR] HTTPCLIENT-2416 - Fix pool entry leak on late lease completion [httpcomponents-client]

Posted by "arturobernalg (via GitHub)" <gi...@apache.org>.
arturobernalg closed pull request #809: HTTPCLIENT-2416 - Fix pool entry leak on late lease completion
URL: https://github.com/apache/httpcomponents-client/pull/809


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org


Re: [PR] HTTPCLIENT-2416 - Fix pool entry leak on late lease completion [httpcomponents-client]

Posted by "arturobernalg (via GitHub)" <gi...@apache.org>.
arturobernalg commented on code in PR #809:
URL: https://github.com/apache/httpcomponents-client/pull/809#discussion_r2894011282


##########
httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/PoolingHttpClientConnectionManager.java:
##########
@@ -388,8 +389,20 @@ public ConnectionEndpoint get(
                     final PoolEntry<HttpRoute, ManagedHttpClientConnection> poolEntry;
                     try {
                         poolEntry = leaseFuture.get(timeout.getDuration(), timeout.getTimeUnit());
-                    } catch (final TimeoutException ex) {
-                        leaseFuture.cancel(true);
+                    } catch (final TimeoutException | InterruptedException ex) {
+                        if (!leaseFuture.cancel(true)) {
+                            try {
+                                final PoolEntry<HttpRoute, ManagedHttpClientConnection> latePoolEntry = leaseFuture.get(

Review Comment:
   @ok2c Then I'll check the core  and the 3 `ConnPool`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org


Re: [PR] HTTPCLIENT-2416 - Fix pool entry leak on late lease completion [httpcomponents-client]

Posted by "ok2c (via GitHub)" <gi...@apache.org>.
ok2c commented on code in PR #809:
URL: https://github.com/apache/httpcomponents-client/pull/809#discussion_r2890909333


##########
httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/PoolingHttpClientConnectionManager.java:
##########
@@ -388,8 +389,20 @@ public ConnectionEndpoint get(
                     final PoolEntry<HttpRoute, ManagedHttpClientConnection> poolEntry;
                     try {
                         poolEntry = leaseFuture.get(timeout.getDuration(), timeout.getTimeUnit());
-                    } catch (final TimeoutException ex) {
-                        leaseFuture.cancel(true);
+                    } catch (final TimeoutException | InterruptedException ex) {
+                        if (!leaseFuture.cancel(true)) {
+                            try {
+                                final PoolEntry<HttpRoute, ManagedHttpClientConnection> latePoolEntry = leaseFuture.get(

Review Comment:
   @arturobernalg If the method fails once the proposed fix to call another time? That does not look right. There has to be a better fix.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org


Re: [PR] HTTPCLIENT-2416 - Fix pool entry leak on late lease completion [httpcomponents-client]

Posted by "arturobernalg (via GitHub)" <gi...@apache.org>.
arturobernalg commented on PR #809:
URL: https://github.com/apache/httpcomponents-client/pull/809#issuecomment-4011342223

   Close it in favor of [4b5836b](https://github.com/apache/httpcomponents-core/pull/649/changes/4b5836b593c022e3307c489c732aa79929395324) 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org


Re: [PR] HTTPCLIENT-2416 - Fix pool entry leak on late lease completion [httpcomponents-client]

Posted by "arturobernalg (via GitHub)" <gi...@apache.org>.
arturobernalg commented on code in PR #809:
URL: https://github.com/apache/httpcomponents-client/pull/809#discussion_r2891452748


##########
httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/PoolingHttpClientConnectionManager.java:
##########
@@ -388,8 +389,20 @@ public ConnectionEndpoint get(
                     final PoolEntry<HttpRoute, ManagedHttpClientConnection> poolEntry;
                     try {
                         poolEntry = leaseFuture.get(timeout.getDuration(), timeout.getTimeUnit());
-                    } catch (final TimeoutException ex) {
-                        leaseFuture.cancel(true);
+                    } catch (final TimeoutException | InterruptedException ex) {
+                        if (!leaseFuture.cancel(true)) {
+                            try {
+                                final PoolEntry<HttpRoute, ManagedHttpClientConnection> latePoolEntry = leaseFuture.get(

Review Comment:
   @ok2c  I dropped the second get() and reworked it to use the pool.lease(..., FutureCallback) to capture late completions and release the PoolEntry on timeout/interrupt/cancel.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org


Re: [PR] HTTPCLIENT-2416 - Fix pool entry leak on late lease completion [httpcomponents-client]

Posted by "benjaminp (via GitHub)" <gi...@apache.org>.
benjaminp commented on PR #809:
URL: https://github.com/apache/httpcomponents-client/pull/809#issuecomment-4007343485

   Thanks, this does seem to resolve my examples raised on the ticket.
   
   I do have a suggested fix of my own, which is a bit less code:
   ```
   diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/InternalExecRuntime.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/InternalExecRuntime.java
   index 7813bc171..dbc7d17b1 100644
   --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/InternalExecRuntime.java
   +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/InternalExecRuntime.java
   @@ -28,6 +28,7 @@
    package org.apache.hc.client5.http.impl.classic;
    
    import java.io.IOException;
   +import java.util.concurrent.CancellationException;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.TimeoutException;
    import java.util.concurrent.atomic.AtomicReference;
   @@ -118,10 +119,10 @@ public void acquireEndpoint(
                        log.debug("{} acquired endpoint {}", id, ConnPoolSupport.getId(connectionEndpoint));
                    }
                } catch (final TimeoutException ex) {
   -                connRequest.cancel();
   +                cancelLeaklessly(connRequest);
                    throw new ConnectionRequestTimeoutException(ex.getMessage());
                } catch (final InterruptedException interrupted) {
   -                connRequest.cancel();
   +                cancelLeaklessly(connRequest);
                    Thread.currentThread().interrupt();
                    throw new RequestFailedException("Request aborted", interrupted);
                } catch (final ExecutionException ex) {
   @@ -137,6 +138,18 @@ public void acquireEndpoint(
            }
        }
    
   +    private void cancelLeaklessly(final LeaseRequest request) {
   +        if (!request.cancel()) {
   +            try {
   +                discardEndpoint(request.get(Timeout.INFINITE));
   +            } catch (final InterruptedException | TimeoutException e) {
   +                throw new IllegalStateException(e);
   +            } catch (final ExecutionException | CancellationException e) {
   +                // ignore
   +            }
   +        }
   +    }
   +
        ConnectionEndpoint ensureValid() {
            final ConnectionEndpoint endpoint = endpointRef.get();
            if (endpoint == null) {
   ```
   The idea is the check if cancelation on `InterruptedException` or `TimeoutException` actually mutates future. If it did not, it means the future was already completed and may have a connection that must be released.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org


Re: [PR] HTTPCLIENT-2416 - Fix pool entry leak on late lease completion [httpcomponents-client]

Posted by "ok2c (via GitHub)" <gi...@apache.org>.
ok2c commented on code in PR #809:
URL: https://github.com/apache/httpcomponents-client/pull/809#discussion_r2892065000


##########
httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/PoolingHttpClientConnectionManager.java:
##########
@@ -388,8 +389,20 @@ public ConnectionEndpoint get(
                     final PoolEntry<HttpRoute, ManagedHttpClientConnection> poolEntry;
                     try {
                         poolEntry = leaseFuture.get(timeout.getDuration(), timeout.getTimeUnit());
-                    } catch (final TimeoutException ex) {
-                        leaseFuture.cancel(true);
+                    } catch (final TimeoutException | InterruptedException ex) {
+                        if (!leaseFuture.cancel(true)) {
+                            try {
+                                final PoolEntry<HttpRoute, ManagedHttpClientConnection> latePoolEntry = leaseFuture.get(

Review Comment:
   @arturobernalg This hardly looks any better to me. I like @benjaminp approach better (other than the exception handling part) but I would try to push the fix into PoolingHttpClientConnectionManager or possibly even into core. 
   
   I will try to look into it on the weekend



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org