From 83f4195ef8db65e1266f1f7ff952dd43c89010aa Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Mon, 2 Nov 2026 20:42:09 +0100 Subject: [PATCH] Notify the callbacks of lease requests failed by StrictConnPool#enumAvailable and #enumLeased enumAvailable() and enumLeased() expire pending lease requests through processPendingRequests() but did not fire the completion callbacks. The futures of requests that timed out stayed uncompleted until the next lease or release, so closeExpired() and closeIdle() did not enforce the lease request timeout on an otherwise idle pool. (cherry picked from commit 9983b0361) --- .../apache/hc/core5/pool/StrictConnPool.java | 2 + .../pool/TestPoolPendingRequestExpiry.java | 102 ++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 httpcore5/src/test/java/org/apache/hc/core5/pool/TestPoolPendingRequestExpiry.java diff --git a/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java b/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java index 15eb844e5..2a6751745 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java @@ -596,6 +596,7 @@ public void enumAvailable(final Callback> callback) { } finally { this.lock.unlock(); } + fireCallbacks(); } /** @@ -615,6 +616,7 @@ public void enumLeased(final Callback> callback) { } finally { this.lock.unlock(); } + fireCallbacks(); } private void purgePoolMap() { diff --git a/httpcore5/src/test/java/org/apache/hc/core5/pool/TestPoolPendingRequestExpiry.java b/httpcore5/src/test/java/org/apache/hc/core5/pool/TestPoolPendingRequestExpiry.java new file mode 100644 index 000000000..383e439e3 --- /dev/null +++ b/httpcore5/src/test/java/org/apache/hc/core5/pool/TestPoolPendingRequestExpiry.java @@ -0,0 +1,102 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.hc.core5.pool; + +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import org.apache.hc.core5.io.CloseMode; +import org.apache.hc.core5.util.TimeValue; +import org.apache.hc.core5.util.Timeout; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; + +final class TestPoolPendingRequestExpiry { + + private static ManagedConnPool createPool(final PoolConcurrencyPolicy policy) { + switch (policy) { + case STRICT: + return new StrictConnPool<>(1, 1); + case LAX: + return new LaxConnPool<>(1); + default: + return new RouteSegmentedConnPool<>( + 1, 1, TimeValue.NEG_ONE_MILLISECOND, PoolReusePolicy.LIFO, (c, mode) -> c.close(mode)); + } + } + + @ParameterizedTest + @EnumSource(PoolConcurrencyPolicy.class) + void closeExpiredFailsExpiredPendingRequest(final PoolConcurrencyPolicy policy) throws Exception { + final ManagedConnPool pool = createPool(policy); + try { + final PoolEntry leased = + pool.lease("r1", null, Timeout.ofSeconds(1), null).get(1, TimeUnit.SECONDS); + assertNotNull(leased); + final Future> pending = + pool.lease("r1", null, Timeout.ofMilliseconds(50), null); + + Thread.sleep(150); + pool.closeExpired(); + + final ExecutionException ex = assertThrows(ExecutionException.class, + () -> pending.get(5, TimeUnit.SECONDS)); + assertInstanceOf(TimeoutException.class, ex.getCause()); + } finally { + pool.close(CloseMode.IMMEDIATE); + } + } + + @ParameterizedTest + @EnumSource(PoolConcurrencyPolicy.class) + void closeIdleFailsExpiredPendingRequest(final PoolConcurrencyPolicy policy) throws Exception { + final ManagedConnPool pool = createPool(policy); + try { + final PoolEntry leased = + pool.lease("r1", null, Timeout.ofSeconds(1), null).get(1, TimeUnit.SECONDS); + assertNotNull(leased); + final Future> pending = + pool.lease("r1", null, Timeout.ofMilliseconds(50), null); + + Thread.sleep(150); + pool.closeIdle(TimeValue.ofMilliseconds(1)); + + final ExecutionException ex = assertThrows(ExecutionException.class, + () -> pending.get(5, TimeUnit.SECONDS)); + assertInstanceOf(TimeoutException.class, ex.getCause()); + } finally { + pool.close(CloseMode.IMMEDIATE); + } + } + +}