This is an automated email from the ASF dual-hosted git repository.

ffang pushed a commit to branch 3.6.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/3.6.x-fixes by this push:
     new ebb4c0768d3 CXF-9140: Keep a shared hc5 client open while other 
conduits use it (#3725)
ebb4c0768d3 is described below

commit ebb4c0768d34d90c862df73c8e7b87a048d38537
Author: Freeman(Yue) Fang <[email protected]>
AuthorDate: Tue Nov 17 17:40:57 2026 -0500

    CXF-9140: Keep a shared hc5 client open while other conduits use it (#3725)
    
    * CXF-9140: Keep a shared hc5 client open while other conduits use it
    
    AsyncHTTPConduitFactory shares one hc5 client between the conduits with
    the same HTTPClientPolicy instance, but closing any of these conduits
    shut the client down. The calls in flight on the other conduits then
    hung, or failed after the receive timeout, and their later calls failed,
    as they kept using the client. A conduit shut the client down also when
    it had not used it, and each time it was closed.
    
    Count the conduits using a client, and shut it down when the last one
    releases it, as HttpClientHTTPConduit does for a shared HttpClient. A
    conduit releases the client it got once, on close(), and only if the
    policy still maps to that client: after update() has replaced the
    clients, a conduit that got one before does not release the new one.
    setupNIOClient() holds a reference for the client it creates, so that it
    stays open until the factory is shut down.
    
    * CXF-9140: Give each conduit its own hc5 client
    
    Instead of sharing a client between the conduits with the same
    HTTPClientPolicy instance, with a count of the conduits using it, each
    conduit now gets its own hc5 client, and shuts it down on close(). The
    default policy is created per conduit, so most conduits already had
    their own client. Closing a conduit cannot affect the calls of other
    conduits any longer, also not after update() has restarted the clients.
    The factory keeps the clients only to shut them down, and to apply the
    connection limits.
    
    setupNIOClient() and close(HTTPClientPolicy) are deprecated and do
    nothing, as there is no client per policy any longer.
    
    * CXF-9140: Keep the clients of the factory in a set
    
    Each conduit has its own client now, so the factory only needs the set
    of its clients. release() finds the conduit's client in it.
    
    (cherry picked from commit 92d03194c8ae8ecd712a82d9aaabb6a9f927b32a)
    (cherry picked from commit 28446074de014551738e512a478d8b2cbf390eb2)
---
 .../http/asyncclient/hc5/AsyncHTTPConduit.java     |  10 +-
 .../asyncclient/hc5/AsyncHTTPConduitFactory.java   |  66 +++++-----
 .../hc5/URLConnectionAsyncHTTPConduit.java         |  10 +-
 .../hc5/AsyncHTTPConduitClientTest.java            | 144 +++++++++++++++++++++
 .../systest/hc5/jaxrs/JAXRSAsyncClientTest.java    |  29 +++++
 5 files changed, 225 insertions(+), 34 deletions(-)

diff --git 
a/rt/transports/http-hc5/src/main/java/org/apache/cxf/transport/http/asyncclient/hc5/AsyncHTTPConduit.java
 
b/rt/transports/http-hc5/src/main/java/org/apache/cxf/transport/http/asyncclient/hc5/AsyncHTTPConduit.java
index e2ce436e11e..c45033c236b 100644
--- 
a/rt/transports/http-hc5/src/main/java/org/apache/cxf/transport/http/asyncclient/hc5/AsyncHTTPConduit.java
+++ 
b/rt/transports/http-hc5/src/main/java/org/apache/cxf/transport/http/asyncclient/hc5/AsyncHTTPConduit.java
@@ -1007,8 +1007,14 @@ public class AsyncHTTPConduit extends 
HttpClientHTTPConduit {
     @Override
     public void close() {
         super.close();
-        if (factory != null) {
-            factory.close(this.getClient());
+        final CloseableHttpAsyncClient httpClient;
+        synchronized (this) {
+            // shut the client down once, and only if this conduit got one
+            httpClient = client;
+            client = null;
+        }
+        if (factory != null && httpClient != null) {
+            factory.release(httpClient);
         }
     }
 
diff --git 
a/rt/transports/http-hc5/src/main/java/org/apache/cxf/transport/http/asyncclient/hc5/AsyncHTTPConduitFactory.java
 
b/rt/transports/http-hc5/src/main/java/org/apache/cxf/transport/http/asyncclient/hc5/AsyncHTTPConduitFactory.java
index 50e1c69c936..68524e42606 100644
--- 
a/rt/transports/http-hc5/src/main/java/org/apache/cxf/transport/http/asyncclient/hc5/AsyncHTTPConduitFactory.java
+++ 
b/rt/transports/http-hc5/src/main/java/org/apache/cxf/transport/http/asyncclient/hc5/AsyncHTTPConduitFactory.java
@@ -22,6 +22,7 @@ package org.apache.cxf.transport.http.asyncclient.hc5;
 import java.io.IOException;
 import java.net.URI;
 import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.logging.Logger;
 
@@ -118,9 +119,8 @@ public class AsyncHTTPConduitFactory implements 
HTTPConduitFactory {
     };
     
     /**
-     * See please https://issues.apache.org/jira/browse/CXF-8678 and 
-     * https://issues.apache.org/jira/browse/HTTPCLIENT-2209, the context 
propagation
-     * is necessary to remove per-HTTPClientPolicy caching.
+     * The client of a conduit, with its connection manager. Each conduit has 
its own client, which it shuts
+     * down on close(), see please 
https://issues.apache.org/jira/browse/CXF-9140.
      */
     private static class AsyncClient {
         private final PoolingAsyncClientConnectionManager connectionManager;
@@ -140,7 +140,8 @@ public class AsyncHTTPConduitFactory implements 
HTTPConduitFactory {
         }
     }
 
-    private volatile Map<HTTPClientPolicy, AsyncClient> clients = new 
ConcurrentHashMap<>();
+    // the clients of the conduits, to shut them down with the factory
+    private final Set<AsyncClient> clients = ConcurrentHashMap.newKeySet();
 
     private boolean isShutdown;
     private UseAsyncPolicy policy;
@@ -183,9 +184,7 @@ public class AsyncHTTPConduitFactory implements 
HTTPConduitFactory {
     }
 
     private void restartReactor() {
-        final Map<HTTPClientPolicy, AsyncClient> clients2 = clients;
-        clients = new ConcurrentHashMap<>();
-        shutdown(clients2);
+        shutdownClients();
     }
 
     private boolean setProperties(Map<String, Object> s) {
@@ -205,8 +204,8 @@ public class AsyncHTTPConduitFactory implements 
HTTPConduitFactory {
         maxPerRoute = getInt(s.get(MAX_PER_HOST_CONNECTIONS), maxPerRoute);
 
         if (!clients.isEmpty()) {
-            for (Map.Entry<HTTPClientPolicy, AsyncClient> entry: 
clients.entrySet()) {
-                final PoolingAsyncClientConnectionManager connectionManager = 
entry.getValue().getConnectionManager();
+            for (AsyncClient client: clients) {
+                final PoolingAsyncClientConnectionManager connectionManager = 
client.getConnectionManager();
                 connectionManager.setMaxTotal(maxConnections);
                 connectionManager.setDefaultMaxPerRoute(maxPerRoute);
             }
@@ -289,17 +288,13 @@ public class AsyncHTTPConduitFactory implements 
HTTPConduitFactory {
     }
 
     public void shutdown() {
-        shutdown(clients);
-        clients.clear();
+        shutdownClients();
         isShutdown = true;
     }
 
-    private static void shutdown(Map<HTTPClientPolicy, AsyncClient> clients) {
-        if (!clients.isEmpty()) {
-            for (Map.Entry<HTTPClientPolicy, AsyncClient> entry: 
clients.entrySet()) {
-                shutdown(entry.getValue().getClient());
-                entry.getValue().getConnectionManager().close();
-            }
+    private void shutdownClients() {
+        for (AsyncClient client: clients) {
+            release(client);
         }
     }
 
@@ -326,14 +321,11 @@ public class AsyncHTTPConduitFactory implements 
HTTPConduitFactory {
         }
     }
 
-    public synchronized void setupNIOClient(HTTPClientPolicy clientPolicy, 
final TlsStrategy tlsStrategy) {
-        final AsyncClient client = clients.get(clientPolicy);
-
-        if (client != null) {
-            return;
-        }
-        
-        clients.computeIfAbsent(clientPolicy, key -> createNIOClient(key, 
tlsStrategy));
+    /**
+     * @deprecated each conduit creates its own client, so there is no client 
to set up for a policy
+     */
+    @Deprecated
+    public void setupNIOClient(HTTPClientPolicy clientPolicy, final 
TlsStrategy tlsStrategy) {
     }
     
     private AsyncClient createNIOClient(HTTPClientPolicy clientPolicy, final 
TlsStrategy tlsStrategy) {
@@ -411,9 +403,10 @@ public class AsyncHTTPConduitFactory implements 
HTTPConduitFactory {
     public CloseableHttpAsyncClient createClient(final 
URLConnectionHTTPConduit c, final TlsStrategy tlsStrategy) 
             throws IOException {
 
-        return clients
-            .computeIfAbsent(c.getClient(), key -> createNIOClient(key, 
tlsStrategy))
-            .getClient();
+        // a new client for each conduit, which shuts it down with release() 
on close()
+        final AsyncClient client = createNIOClient(c.getClient(), tlsStrategy);
+        clients.add(client);
+        return client.getClient();
     }
     
     int getMaxConnections() {
@@ -454,9 +447,22 @@ public class AsyncHTTPConduitFactory implements 
HTTPConduitFactory {
         }
     }
 
+    /**
+     * @deprecated each conduit has its own client, which it shuts down on 
close()
+     */
+    @Deprecated
     public void close(HTTPClientPolicy clientPolicy) {
-        final AsyncClient client = clients.remove(clientPolicy);
-        if (client != null) {
+    }
+
+    /**
+     * Shuts down the client a conduit got from {@link #createClient}.
+     */
+    void release(CloseableHttpAsyncClient httpClient) {
+        clients.stream().filter(c -> c.getClient() == 
httpClient).findAny().ifPresent(this::release);
+    }
+
+    private void release(AsyncClient client) {
+        if (clients.remove(client)) {
             shutdown(client.getClient());
             client.getConnectionManager().close();
         }
diff --git 
a/rt/transports/http-hc5/src/main/java/org/apache/cxf/transport/http/asyncclient/hc5/URLConnectionAsyncHTTPConduit.java
 
b/rt/transports/http-hc5/src/main/java/org/apache/cxf/transport/http/asyncclient/hc5/URLConnectionAsyncHTTPConduit.java
index ee620945106..0016ee3d017 100644
--- 
a/rt/transports/http-hc5/src/main/java/org/apache/cxf/transport/http/asyncclient/hc5/URLConnectionAsyncHTTPConduit.java
+++ 
b/rt/transports/http-hc5/src/main/java/org/apache/cxf/transport/http/asyncclient/hc5/URLConnectionAsyncHTTPConduit.java
@@ -1000,8 +1000,14 @@ public class URLConnectionAsyncHTTPConduit extends 
URLConnectionHTTPConduit {
     @Override
     public void close() {
         super.close();
-        if (factory != null) {
-            factory.close(this.getClient());
+        final CloseableHttpAsyncClient httpClient;
+        synchronized (this) {
+            // shut the client down once, and only if this conduit got one
+            httpClient = client;
+            client = null;
+        }
+        if (factory != null && httpClient != null) {
+            factory.release(httpClient);
         }
     }
 
diff --git 
a/rt/transports/http-hc5/src/test/java/org/apache/cxf/transport/http/asyncclient/hc5/AsyncHTTPConduitClientTest.java
 
b/rt/transports/http-hc5/src/test/java/org/apache/cxf/transport/http/asyncclient/hc5/AsyncHTTPConduitClientTest.java
new file mode 100644
index 00000000000..f236e563f43
--- /dev/null
+++ 
b/rt/transports/http-hc5/src/test/java/org/apache/cxf/transport/http/asyncclient/hc5/AsyncHTTPConduitClientTest.java
@@ -0,0 +1,144 @@
+/**
+ * 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.
+ */
+
+package org.apache.cxf.transport.http.asyncclient.hc5;
+
+import java.util.Collections;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.bus.extension.ExtensionManagerBus;
+import org.apache.cxf.service.model.EndpointInfo;
+import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
+import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
+import org.apache.hc.core5.reactor.IOReactorStatus;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNotSame;
+
+/**
+ * Each conduit has its own hc5 client, also when conduits have the same 
policy, and closing a conduit
+ * shuts down only its own client
+ */
+public class AsyncHTTPConduitClientTest {
+    private Bus bus;
+    private AsyncHTTPConduitFactory factory;
+    private final HTTPClientPolicy policy = new HTTPClientPolicy();
+
+    @Before
+    public void setUp() {
+        bus = new ExtensionManagerBus();
+        factory = new AsyncHTTPConduitFactory(bus);
+    }
+
+    @After
+    public void tearDown() {
+        factory.shutdown();
+        bus.shutdown(true);
+    }
+
+    @Test
+    public void testClosingConduitKeepsClientOfOtherConduits() throws 
Exception {
+        AsyncHTTPConduit conduit1 = newConduit();
+        AsyncHTTPConduit conduit2 = newConduit();
+        CloseableHttpAsyncClient client1 = 
awaitActive(conduit1.getHttpAsyncClient(null));
+        CloseableHttpAsyncClient client2 = 
awaitActive(conduit2.getHttpAsyncClient(null));
+        assertNotSame(client1, client2);
+
+        conduit1.close();
+        // closing it again, as a finalizer may do, does no harm
+        conduit1.close();
+        assertNotEquals(IOReactorStatus.ACTIVE, client1.getStatus());
+        assertEquals(IOReactorStatus.ACTIVE, client2.getStatus());
+
+        conduit2.close();
+        assertNotEquals(IOReactorStatus.ACTIVE, client2.getStatus());
+    }
+
+    @Test
+    public void testClosingConduitWithoutClientKeepsClientOfOtherConduits() 
throws Exception {
+        AsyncHTTPConduit conduit = newConduit();
+        CloseableHttpAsyncClient client = 
awaitActive(conduit.getHttpAsyncClient(null));
+
+        newConduit().close();
+        assertEquals(IOReactorStatus.ACTIVE, client.getStatus());
+        conduit.close();
+    }
+
+    @Test
+    public void testConduitGetsNewClientAfterClose() throws Exception {
+        AsyncHTTPConduit conduit = newConduit();
+        CloseableHttpAsyncClient client = 
awaitActive(conduit.getHttpAsyncClient(null));
+        conduit.close();
+
+        // a conduit used after it was closed does not keep using the client 
it shut down
+        CloseableHttpAsyncClient newClient = conduit.getHttpAsyncClient(null);
+        assertNotSame(client, newClient);
+        awaitActive(newClient);
+        conduit.close();
+    }
+
+    @Test
+    public void testUpdateDoesNotShutDownClientOfOtherConduits() throws 
Exception {
+        AsyncHTTPConduit conduit1 = newConduit();
+        awaitActive(conduit1.getHttpAsyncClient(null));
+
+        // a property that needs a restart of the reactor shuts the clients 
down
+        
factory.update(Collections.singletonMap(AsyncHTTPConduitFactory.SO_KEEPALIVE, 
"true"));
+        AsyncHTTPConduit conduit2 = newConduit();
+        CloseableHttpAsyncClient client2 = 
awaitActive(conduit2.getHttpAsyncClient(null));
+
+        // the conduit that had a client before does not shut down the new one
+        conduit1.close();
+        assertEquals(IOReactorStatus.ACTIVE, client2.getStatus());
+        conduit2.close();
+    }
+
+    @Test
+    public void testShutdownShutsDownClientsOfConduits() throws Exception {
+        CloseableHttpAsyncClient client1 = 
awaitActive(newConduit().getHttpAsyncClient(null));
+        CloseableHttpAsyncClient client2 = 
awaitActive(newConduit().getHttpAsyncClient(null));
+
+        factory.shutdown();
+        assertNotEquals(IOReactorStatus.ACTIVE, client1.getStatus());
+        assertNotEquals(IOReactorStatus.ACTIVE, client2.getStatus());
+    }
+
+    // the client becomes active asynchronously, once its I/O reactor has 
started
+    private static CloseableHttpAsyncClient 
awaitActive(CloseableHttpAsyncClient client) throws Exception {
+        for (int i = 0; i < 500 && client.getStatus() == 
IOReactorStatus.INACTIVE; i++) {
+            Thread.sleep(10);
+        }
+        assertEquals(IOReactorStatus.ACTIVE, client.getStatus());
+        return client;
+    }
+
+    private AsyncHTTPConduit newConduit() throws Exception {
+        EndpointInfo ei = new EndpointInfo();
+        ei.setAddress("http://localhost/test";);
+        AsyncHTTPConduit conduit = new AsyncHTTPConduit(bus, ei, null, 
factory);
+        conduit.finalizeConfig();
+        conduit.setClient(policy);
+        return conduit;
+    }
+}
diff --git 
a/systests/transport-hc5/src/test/java/org/apache/cxf/systest/hc5/jaxrs/JAXRSAsyncClientTest.java
 
b/systests/transport-hc5/src/test/java/org/apache/cxf/systest/hc5/jaxrs/JAXRSAsyncClientTest.java
index e1ea7dac93d..150365b9e8c 100644
--- 
a/systests/transport-hc5/src/test/java/org/apache/cxf/systest/hc5/jaxrs/JAXRSAsyncClientTest.java
+++ 
b/systests/transport-hc5/src/test/java/org/apache/cxf/systest/hc5/jaxrs/JAXRSAsyncClientTest.java
@@ -28,6 +28,7 @@ import java.lang.reflect.Type;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
@@ -123,6 +124,28 @@ public class JAXRSAsyncClientTest extends 
AbstractBusClientServerTestBase {
         }
     }
 
+    @Test
+    public void testClosingClientWithSamePolicyKeepsCallsOfOtherClients() 
throws Exception {
+        // WebClients configured with the same policy, each one with its own 
hc5 client
+        HTTPClientPolicy policy = new HTTPClientPolicy();
+        policy.setReceiveTimeout(5000);
+        String address = "http://localhost:"; + PORT + "/bookstore/patch";
+        WebClient wc = createWebClientWithPolicy(address, policy);
+        WebClient other = createWebClientWithPolicy(address, policy);
+        assertEquals("Patch", other.invoke("PATCH", new Book("Patch", 123L), 
Book.class).getName());
+
+        // the server answers this call after 2 s, so the other WebClient is 
closed while it is in flight
+        Future<Book> book = CompletableFuture.supplyAsync(
+            () -> wc.invoke("PATCH", new Book("Timeout", 123L), Book.class));
+        Thread.sleep(500);
+        other.close();
+        assertEquals("Timeout", book.get(10, TimeUnit.SECONDS).getName());
+
+        // and the later calls succeed too
+        assertEquals("Patch", wc.invoke("PATCH", new Book("Patch", 124L), 
Book.class).getName());
+        wc.close();
+    }
+
     @Test
     public void testPatchBookInputStream() throws Exception {
         String address = "http://localhost:"; + PORT + "/bookstore/patch";
@@ -553,6 +576,12 @@ public class JAXRSAsyncClientTest extends 
AbstractBusClientServerTestBase {
         return wc;
     }
 
+    private WebClient createWebClientWithPolicy(String address, 
HTTPClientPolicy policy) {
+        final WebClient wc = createWebClient(address).type("application/xml");
+        WebClient.getConfig(wc).getHttpConduit().setClient(policy);
+        return wc;
+    }
+
     private InvocationCallback<Object> createCallback(final Holder<Object> 
holder) {
         return new InvocationCallback<Object>() {
             public void completed(Object response) {

Reply via email to