Backtestsjsouprun-20261006-181938#2516

Issue #2516 Merged fix PR #2584 Run 6 Oct 2026 · run-20261006-181938 jhy/jsoup

Review HttpClient's thread use under heavy request load

Worse

The merged fix was judged better than our patch.

In this judgment, Patch A = our agent, Patch B = the merged fix.

Both patches fix the root cause the same way: requests with no authenticator and no custom SSLContext now share one static HttpClient instead of each Connection building its own client with its own selector and worker threads. Both also rebuild the shared client when the JVM default SSLContext changes.

Patch A goes further with a weakly keyed pool of shared clients per custom SSLContext. That extra machinery (WeakHashMap plus WeakReference values) is beyond the issue's ask and adds some subtlety: clients can be collected and rebuilt. Patch A also keeps the old unsynchronized per-connection logic for the authenticator path. Its standout test is an integration test that counts SelectorManager threads across repeated `Jsoup.connect` calls, which directly measures the reported symptom.

Patch B introduces an immutable ClientState holding (client, authenticator, sslContext) with a single `matches` check. It uses double-checked locking for both the shared client and the per-connection client, so concurrent requests on a session can't race to create duplicate clients. It replaces the two loosely coupled fields (`client`, `lastAuth`) with one volatile state field, which is clearer and fixes a latent ordering quirk in the old reuse logic. Its tests check:
- sharing across connections
- that concurrent lazy initialization yields a single client
- default-SSL refresh
- rebuilds when the authenticator or SSL context changes
- an integration test that mixed proxied and direct concurrent requests through the now-shared client keep proxy routes isolated

That last test covers the main new risk of sharing a client, since proxy settings are per-request thread-locals. Patch B's test refactor to @AfterEach is minor churn but harmless.

Overall, both are correct. Patch B's design is more robust under the concurrent heavy-load scenario the issue describes, and its tests better cover the correctness risks introduced by sharing. Patch A has the nicer direct thread-count test but more complex pooling and no concurrency hardening.

The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.

Solve

20m 5s

Wall-clock time in the container

Full test suite

No new failures

2,208 passed · 0 skipped · 2,208 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

252vs 312 merged

Changed lines, ours vs merged

#2515 reports that many repeated requests will spawn many threads, probably redundant. They do get reaped on GC pressure, but there is likely to be better ways to reuse them via a keep-alive pool or similar.

@rs1gg thanks for raising this!

The two patches

Our patch+243 −9
CHANGES.mdindex 7d95fc90..bd553246 100644@@ -11,6 +11,7 @@ * Added `Elements#before(Node)`, `after(Node)`, `prepend(Node)`, and `append(Node)` to match the existing HTML string methods. [#953](https://github.com/jhy/jsoup/issues/953) * XML serialization now repairs element and attribute names that start with an invalid character, rather than outputting `<null>` elements or dropping attributes. For example, an attribute named `1a` is written as `_1a`. Additional leading underscores keep repaired attribute names unique if they conflict with another attribute. [#2573](https://github.com/jhy/jsoup/issues/2573) * Large file-backed uploads through `Connection.requestBodyStream(InputStream)` now stream directly with the JDK `HttpClient` on Java 11+, rather than being loaded fully into memory first.+* Reduced thread use when making many requests with the JDK `HttpClient` on Java 11+. Requests without a per-request authenticator now share a pooled `HttpClient` (one per `SSLContext`), rather than each new `Connection` creating its own client with its own selector and worker threads. Connections and threads are now reused across requests. [#2515](https://github.com/jhy/jsoup/issues/2515)  ### Changes * Aligned the XML parser stack depth and lookups to the configured maximum, which now defaults to 512 for both HTML and XML. [#2570](https://github.com/jhy/jsoup/pull/2570)src/main/java11/org/jsoup/helper/HttpClientExecutor.javaindex 30c47ee2..a7dbad64 100644@@ -6,6 +6,7 @@ import org.jspecify.annotations.Nullable; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream;+import java.lang.ref.WeakReference; import java.net.Proxy; import java.net.ProxySelector; import java.net.SocketAddress;@@ -15,12 +16,17 @@ import java.net.http.HttpClient; import java.net.http.HttpHeaders; import java.net.http.HttpRequest; import java.net.http.HttpResponse;+import java.security.NoSuchAlgorithmException; import java.time.Duration; import java.util.ArrayList; import java.util.Collections; import java.util.List;+import java.util.Map;+import java.util.WeakHashMap; import java.util.concurrent.atomic.AtomicReference; +import javax.net.ssl.SSLContext;+ import static org.jsoup.helper.HttpConnection.Response; import static org.jsoup.helper.HttpConnection.Response.writePost; @@ -42,32 +48,90 @@ class HttpClientExecutor extends RequestExecutor {     }      /**-     Retrieve the HttpClient from the Connection, or create a new one. Allows for connection pooling of requests in the-     same Connection (session).+     Retrieve an HttpClient for this request. Each HttpClient runs its own selector thread and worker pool, so creating+     one per Connection spawns many redundant threads under repeated requests. Requests without a per-request+     authenticator share a pooled client (one per SSLContext), which keeps connections and threads alive for reuse.+     Requests with an authenticator use a client held by their Connection (session), as the authenticator is bound to+     the client.      */     HttpClient client() {+        if (req.authenticator == null) {+            HttpClient client = sharedClient(req.sslContext);+            req.connection.client = client; // holds the (weakly pooled) client while this session is in use+            req.connection.lastAuth = null;+            return client;+        }+         // we try to reuse the same Client across requests in a given Connection; but if the request's auth or ssl context have changed, we need to create a new client+        RequestAuthenticator prevAuth = req.connection.lastAuth;+        req.connection.lastAuth = req.authenticator;         if (req.connection.client != null) {             HttpClient client = (HttpClient) req.connection.client;             boolean reuse = true; -            RequestAuthenticator prevAuth = req.connection.lastAuth;-            req.connection.lastAuth = req.authenticator;-            if (prevAuth != req.authenticator) // might both be null+            if (prevAuth != req.authenticator)                 reuse = false;             if (req.sslContext != null && !(client.sslContext() == req.sslContext)) // client returns default context if not otherwise set                 reuse = false;             if (reuse) return client;         } +        HttpClient client = newClient(req.authenticator, req.sslContext);+        req.connection.client = client;+        return client;+    }++    static HttpClient newClient(@Nullable RequestAuthenticator authenticator, @Nullable SSLContext sslContext) {         HttpClient.Builder builder = HttpClient.newBuilder();         builder.followRedirects(HttpClient.Redirect.NEVER); // customized redirects         builder.proxy(new ProxyWrap()); // thread local impl for per request; called on executing thread-        if (req.authenticator != null) builder.authenticator(new AuthenticationHandler(req.authenticator));-        if (req.sslContext    != null) builder.sslContext(req.sslContext);+        if (authenticator != null) builder.authenticator(new AuthenticationHandler(authenticator));+        if (sslContext    != null) builder.sslContext(sslContext);+        return builder.build();+    } -        HttpClient client = builder.build();-        req.connection.client = client;+    /**+     Client shared by all requests that have no authenticator and no custom SSLContext. Rebuilt if the JVM default+     SSLContext changes, as the HttpClient captures the default when it is built.+     */+    private static volatile @Nullable HttpClient defaultClient;++    /**+     Clients shared by requests that set a custom SSLContext (and no authenticator). Keyed weakly on the SSLContext,+     and holding the client weakly, so an unused client (and its threads) can still be collected.+     */+    private static final Map<SSLContext, WeakReference<HttpClient>> SslClients = new WeakHashMap<>();++    static HttpClient sharedClient(@Nullable SSLContext sslContext) {+        if (sslContext == null) return defaultClient();+        synchronized (SslClients) {+            WeakReference<HttpClient> ref = SslClients.get(sslContext);+            HttpClient client = ref != null ? ref.get() : null;+            if (client == null) {+                client = newClient(null, sslContext);+                SslClients.put(sslContext, new WeakReference<>(client));+            }+            return client;+        }+    }++    private static HttpClient defaultClient() {+        SSLContext current;+        try {+            current = SSLContext.getDefault();+        } catch (NoSuchAlgorithmException e) {+            return newClient(null, null); // let the HttpClient report the failure as it would otherwise+        }+        HttpClient client = defaultClient;+        if (client == null || client.sslContext() != current) {+            synchronized (HttpClientExecutor.class) {+                client = defaultClient;+                if (client == null || client.sslContext() != current) {+                    client = newClient(null, null);+                    defaultClient = client;+                }+            }+        }         return client;     } src/test/java11/org/jsoup/helper/HttpClientExecutorTest.javaindex a2a6eb67..7e2364e6 100644@@ -1,20 +1,25 @@ package org.jsoup.helper; +import org.jsoup.Connection; import org.jsoup.internal.SharedConstants; import org.junit.jupiter.api.Test;  import java.io.IOException; import java.io.InputStream;+import java.lang.ref.WeakReference; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.ProxySelector; import java.net.SocketAddress; import java.net.URI;+import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.util.Collections; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; +import javax.net.ssl.SSLContext;+ import static org.jsoup.Connection.Method.POST; import static org.junit.jupiter.api.Assertions.*; @@ -73,6 +78,107 @@ public class HttpClientExecutorTest {         }     } +    private static Object clientFor(HttpConnection.Request request) {+        return HttpClientTestAccess.client(RequestDispatch.get(request, null));+    }++    private static HttpConnection.Request newRequest() {+        return (HttpConnection.Request) HttpConnection.connect("https://example.com").request();+    }++    @Test void separateConnectionsShareClient() {+        // each HttpClient runs its own threads, so independent Jsoup.connect() calls should not each build one+        try {+            enableHttpClient();+            Object one = clientFor(newRequest());+            Object two = clientFor(newRequest());+            assertSame(one, two);+        } finally {+            disableHttpClient();+        }+    }++    @Test void separateConnectionsWithSameSslContextShareClient() throws Exception {+        try {+            enableHttpClient();+            SSLContext ctx = SSLContext.getInstance("TLS");+            ctx.init(null, null, null);+            HttpConnection.Request r1 = newRequest(); r1.sslContext(ctx);+            HttpConnection.Request r2 = newRequest(); r2.sslContext(ctx);+            Object one = clientFor(r1);+            assertSame(one, clientFor(r2));+            assertSame(ctx, ((HttpClient) one).sslContext());+            assertNotSame(one, clientFor(newRequest())); // default ssl doesn't get the custom client++            SSLContext other = SSLContext.getInstance("TLS");+            other.init(null, null, null);+            HttpConnection.Request r3 = newRequest(); r3.sslContext(other);+            Object three = clientFor(r3);+            assertNotSame(one, three);+            assertSame(other, ((HttpClient) three).sslContext());+        } finally {+            disableHttpClient();+        }+    }++    @Test void sharedClientFollowsDefaultSslContext() throws Exception {+        // the HttpClient captures the default SSLContext when built, so a later SSLContext.setDefault must be honored+        SSLContext orig = SSLContext.getDefault();+        try {+            enableHttpClient();+            Object one = clientFor(newRequest());+            assertSame(orig, ((HttpClient) one).sslContext());++            SSLContext other = SSLContext.getInstance("TLS");+            other.init(null, null, null);+            SSLContext.setDefault(other);+            Object two = clientFor(newRequest());+            assertNotSame(one, two);+            assertSame(other, ((HttpClient) two).sslContext());+            assertSame(two, clientFor(newRequest()));+        } finally {+            SSLContext.setDefault(orig);+            disableHttpClient();+        }+    }++    @Test void sessionHoldsSslContextClient() throws Exception {+        // the pool holds custom SSLContext clients weakly; the session must keep its client (and connections) alive+        try {+            enableHttpClient();+            SSLContext ctx = SSLContext.getInstance("TLS");+            ctx.init(null, null, null);+            Connection session = HttpConnection.connect("https://example.com").sslContext(ctx);+            WeakReference<Object> ref = new WeakReference<>(clientFor((HttpConnection.Request) session.newRequest().request()));+            for (int i = 0; i < 5 && ref.get() != null; i++) System.gc();+            assertNotNull(ref.get());+            assertSame(ref.get(), clientFor((HttpConnection.Request) session.newRequest().request()));+        } finally {+            disableHttpClient();+        }+    }++    @Test void authenticatedSessionReusesItsOwnClient() {+        try {+            enableHttpClient();+            RequestAuthenticator auth = ctx -> null;+            Connection session = HttpConnection.connect("https://example.com").auth(auth);++            Object first = clientFor((HttpConnection.Request) session.newRequest().request());+            Object second = clientFor((HttpConnection.Request) session.newRequest().request());+            assertSame(first, second); // same session + same authenticator reuses from the first request+            assertTrue(((HttpClient) first).authenticator().isPresent());++            assertNotSame(first, clientFor(newRequest())); // authenticator not leaked into the shared client+            assertFalse(((HttpClient) clientFor(newRequest())).authenticator().isPresent());++            Connection other = HttpConnection.connect("https://example.com").auth(auth);+            assertNotSame(first, clientFor((HttpConnection.Request) other.request())); // other sessions have their own+        } finally {+            disableHttpClient();+        }+    }+     public static void enableHttpClient() {         System.setProperty(SharedConstants.UseHttpClient, "true");     }src/test/java11/org/jsoup/helper/HttpClientTestAccess.javaindex baec3641..efafaa6a 100644@@ -2,6 +2,7 @@ package org.jsoup.helper;  import java.lang.reflect.Constructor; import java.lang.reflect.Field;+import java.lang.reflect.Method; import java.net.Proxy; import java.net.ProxySelector; import java.net.URL;@@ -43,6 +44,17 @@ final class HttpClientTestAccess {         }     } +    static Object client(RequestExecutor executor) {+        // the HttpClient that the executor will send its request with+        try {+            Method method = executorClass().getDeclaredMethod("client");+            method.setAccessible(true);+            return method.invoke(executor);+        } catch (ReflectiveOperationException e) {+            throw new IllegalStateException("Could not invoke HttpClientExecutor.client", e);+        }+    }+     static ProxySelector newProxyWrap() {         try {             Constructor<?> constructor = loadClass(ProxyWrapClassName).getDeclaredConstructor();src/test/java11/org/jsoup/integration/HttpClientThreadTest.javanew file mode 100644index 00000000..95d52eec@@ -0,0 +1,51 @@+package org.jsoup.integration;++import org.jsoup.Jsoup;+import org.jsoup.helper.HttpClientExecutorTest;+import org.jsoup.nodes.Document;+import org.junit.jupiter.api.AfterAll;+import org.junit.jupiter.api.BeforeAll;+import org.junit.jupiter.api.Test;++import java.io.IOException;++import static org.junit.jupiter.api.Assertions.*;++/**+ Checks that repeated independent requests via the HttpClient reuse threads, rather than each starting a new+ HttpClient with its own selector manager thread and worker pool.+ */+public class HttpClientThreadTest {+    @BeforeAll+    static void setUp() {+        TestServer.start();+        HttpClientExecutorTest.enableHttpClient();+    }++    @AfterAll+    static void reset() {+        HttpClientExecutorTest.disableHttpClient();+    }++    static long selectorThreads() {+        return Thread.getAllStackTraces().keySet().stream()+            .filter(t -> t.isAlive() && t.getName().endsWith("SelectorManager"))+            .count();+    }++    @Test void repeatedRequestsDoNotSpawnClientThreads() throws IOException {+        String url = TestServer.origin().echo.url();+        Jsoup.connect(url).get(); // warm up the shared client+        long before = selectorThreads();++        int requests = 25;+        for (int i = 0; i < requests; i++) {+            Document doc = Jsoup.connect(url).data("requestNum", String.valueOf(i)).get();+            assertEquals(String.valueOf(i), ConnectTest.ihVal("requestNum", doc));+        }++        long after = selectorThreads();+        assertTrue(after - before <= 0,+            "Expected no new HttpClient selector threads after " + requests + " requests, but went from " + before + " to " + after);+    }+} 
Merged fix · PR #2584+271 −41
CHANGES.mdindex 7d95fc90..90de2f8e 100644@@ -11,6 +11,7 @@ * Added `Elements#before(Node)`, `after(Node)`, `prepend(Node)`, and `append(Node)` to match the existing HTML string methods. [#953](https://github.com/jhy/jsoup/issues/953) * XML serialization now repairs element and attribute names that start with an invalid character, rather than outputting `<null>` elements or dropping attributes. For example, an attribute named `1a` is written as `_1a`. Additional leading underscores keep repaired attribute names unique if they conflict with another attribute. [#2573](https://github.com/jhy/jsoup/issues/2573) * Large file-backed uploads through `Connection.requestBodyStream(InputStream)` now stream directly with the JDK `HttpClient` on Java 11+, rather than being loaded fully into memory first.+* Extended Java 11+ HTTP client reuse from requests sharing a `Jsoup.newSession()` to ordinary `Jsoup.connect(...)` calls, reducing transport thread and connection setup churn under sustained request loads. Sessions with custom authentication or SSL contexts continue to use their own client. [#2584](https://github.com/jhy/jsoup/pull/2584)  ### Changes * Aligned the XML parser stack depth and lookups to the configured maximum, which now defaults to 512 for both HTML and XML. [#2570](https://github.com/jhy/jsoup/pull/2570)src/main/java/org/jsoup/helper/HttpConnection.javaindex 243c97a5..4e4a2106 100644@@ -74,8 +74,7 @@ public class HttpConnection implements Connection {      private HttpConnection.Request req;     private Connection.@Nullable Response res;-    @Nullable Object client; // The HttpClient for this Connection, if via the HttpClientExecutor-    @Nullable RequestAuthenticator lastAuth; // The previous Authenticator used by this Connection, if via the HttpClientExecutor+    volatile @Nullable Object clientState; // Java 11 HttpClient state, held as Object for Java 8 compatibility      /**      Create a new Connection, with the request URL specified.src/main/java11/org/jsoup/helper/HttpClientExecutor.javaindex 30c47ee2..849f2f16 100644@@ -15,12 +15,15 @@ import java.net.http.HttpClient; import java.net.http.HttpHeaders; import java.net.http.HttpRequest; import java.net.http.HttpResponse;+import java.security.NoSuchAlgorithmException; import java.time.Duration; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.concurrent.atomic.AtomicReference; +import javax.net.ssl.SSLContext;+ import static org.jsoup.helper.HttpConnection.Response; import static org.jsoup.helper.HttpConnection.Response.writePost; @@ -29,9 +32,11 @@ import static org.jsoup.helper.HttpConnection.Response.writePost;  property {@code jsoup.useHttpClient} to {@code false}.  */ class HttpClientExecutor extends RequestExecutor {-    // HttpClient expects proxy settings per client; we do per request, so held as a thread local. Can't do same for-    // auth because that callback is on a worker thread, so can only do auth per Connection. So we create a new client-    // if the authenticator is different between requests+    private static final Object SharedClientLock = new Object();+    private static volatile @Nullable ClientState sharedClientState;++    // HttpClient expects proxy settings per client; jsoup supports them per request, so resolve them from the calling+    // thread.     static ThreadLocal<@Nullable Proxy> perRequestProxy = new ThreadLocal<>();      @Nullable@@ -42,33 +47,57 @@ class HttpClientExecutor extends RequestExecutor {     }      /**-     Retrieve the HttpClient from the Connection, or create a new one. Allows for connection pooling of requests in the-     same Connection (session).+     Returns the shared client for default requests, or the owning connection's configured client.      */     HttpClient client() {-        // we try to reuse the same Client across requests in a given Connection; but if the request's auth or ssl context have changed, we need to create a new client-        if (req.connection.client != null) {-            HttpClient client = (HttpClient) req.connection.client;-            boolean reuse = true;--            RequestAuthenticator prevAuth = req.connection.lastAuth;-            req.connection.lastAuth = req.authenticator;-            if (prevAuth != req.authenticator) // might both be null-                reuse = false;-            if (req.sslContext != null && !(client.sslContext() == req.sslContext)) // client returns default context if not otherwise set-                reuse = false;-            if (reuse) return client;+        RequestAuthenticator auth = req.authenticator;+        // HttpClient captures the JVM default SSL context when built, and that default can change at runtime.+        SSLContext sslContext = req.sslContext != null ? req.sslContext : defaultSslContext();+        if (auth == null && req.sslContext == null) return sharedClient(sslContext);+        return configuredClient(auth, sslContext);+    }++    private HttpClient configuredClient(@Nullable RequestAuthenticator auth, SSLContext sslContext) {+        ClientState state = (ClientState) req.connection.clientState;+        if (state != null && state.matches(auth, sslContext)) return state.client;+        synchronized (req.connection) {+            // another request may have initialized this connection's client while we waited for the lock.+            state = (ClientState) req.connection.clientState;+            if (state == null || !state.matches(auth, sslContext)) {+                state = new ClientState(auth, sslContext);+                req.connection.clientState = state;+            }+            return state.client;         }+    }++    private static HttpClient sharedClient(SSLContext sslContext) {+        ClientState state = sharedClientState;+        if (state != null && state.matches(null, sslContext)) return state.client;+        synchronized (SharedClientLock) {+            state = sharedClientState;+            if (state != null && state.matches(null, sslContext)) return state.client;+            state = new ClientState(null, sslContext);+            sharedClientState = state;+            return state.client;+        }+    } +    private static HttpClient newClient(@Nullable RequestAuthenticator authenticator, SSLContext sslContext) {         HttpClient.Builder builder = HttpClient.newBuilder();         builder.followRedirects(HttpClient.Redirect.NEVER); // customized redirects         builder.proxy(new ProxyWrap()); // thread local impl for per request; called on executing thread-        if (req.authenticator != null) builder.authenticator(new AuthenticationHandler(req.authenticator));-        if (req.sslContext    != null) builder.sslContext(req.sslContext);+        if (authenticator != null) builder.authenticator(new AuthenticationHandler(authenticator));+        builder.sslContext(sslContext);+        return builder.build();+    } -        HttpClient client = builder.build();-        req.connection.client = client;-        return client;+    private static SSLContext defaultSslContext() {+        try {+            return SSLContext.getDefault();+        } catch (NoSuchAlgorithmException e) {+            throw new IllegalStateException("No default SSL context is available", e);+        }     }      @Override@@ -193,4 +222,20 @@ class HttpClientExecutor extends RequestExecutor {             }         }     }++    private static final class ClientState {+        final HttpClient client;+        final @Nullable RequestAuthenticator authenticator;+        final SSLContext sslContext;++        ClientState(@Nullable RequestAuthenticator authenticator, SSLContext sslContext) {+            this.client = newClient(authenticator, sslContext);+            this.authenticator = authenticator;+            this.sslContext = sslContext;+        }++        boolean matches(@Nullable RequestAuthenticator authenticator, SSLContext sslContext) {+            return this.authenticator == authenticator && this.sslContext == sslContext;+        }+    } }src/test/java11/org/jsoup/helper/HttpClientExecutorTest.javaindex a2a6eb67..1627d167 100644@@ -1,8 +1,12 @@ package org.jsoup.helper; +import org.jsoup.Connection;+import org.jsoup.Jsoup; import org.jsoup.internal.SharedConstants;+import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; +import javax.net.ssl.SSLContext; import java.io.IOException; import java.io.InputStream; import java.net.InetSocketAddress;@@ -10,15 +14,29 @@ import java.net.Proxy; import java.net.ProxySelector; import java.net.SocketAddress; import java.net.URI;+import java.net.http.HttpClient; import java.net.http.HttpRequest;+import java.util.ArrayList; import java.util.Collections; import java.util.List;+import java.util.Set;+import java.util.concurrent.ConcurrentHashMap;+import java.util.concurrent.CountDownLatch;+import java.util.concurrent.ExecutorService;+import java.util.concurrent.Executors;+import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicInteger;+import java.util.function.IntFunction;  import static org.jsoup.Connection.Method.POST; import static org.junit.jupiter.api.Assertions.*;  public class HttpClientExecutorTest {+    @AfterEach void resetHttpClient() {+        HttpClientTestAccess.resetSharedClient();+        disableHttpClient();+    }+     @Test void loadsMultiReleaseHttpClientExecutor() {         // sanity check that the test is resolving the packaged Java 11 override, not a copy on the test classpath         String resource = HttpClientTestAccess.executorClassResource().toExternalForm();@@ -26,13 +44,9 @@ public class HttpClientExecutorTest {     }      @Test void getsHttpClient() {-        try {-            enableHttpClient();-            RequestExecutor executor = RequestDispatch.get(new HttpConnection.Request(), null);-            assertTrue(HttpClientTestAccess.isHttpClientExecutor(executor));-        } finally {-            disableHttpClient(); // reset to previous default for JDK8 compat tests-        }+        enableHttpClient();+        RequestExecutor executor = RequestDispatch.get(new HttpConnection.Request(), null);+        assertTrue(HttpClientTestAccess.isHttpClientExecutor(executor));     }      @Test void getsHttpClientByDefault() {@@ -41,6 +55,81 @@ public class HttpClientExecutorTest {         assertTrue(HttpClientTestAccess.isHttpClientExecutor(executor));     } +    @Test void sharesDefaultClientAcrossConnections() {+        enableHttpClient();+        Object first = HttpClientTestAccess.client(Jsoup.connect("https://example.com/one"));+        Object second = HttpClientTestAccess.client(Jsoup.connect("https://example.com/two"));+        assertSame(first, second);+    }++    @Test void sharesDefaultClientAcrossConcurrentConnections() throws Exception {+        enableHttpClient();+        HttpClientTestAccess.resetSharedClient();+        Set<Object> clients = concurrentClients(i -> Jsoup.connect("https://example.com/" + i));+        assertEquals(1, clients.size());+    }++    @Test void refreshesDefaultClientWhenSslContextChanges() throws Exception {+        SSLContext original = SSLContext.getDefault();+        SSLContext replacement = SSLContext.getInstance("TLS");+        replacement.init(null, null, null);+        try {+            enableHttpClient();+            HttpClientTestAccess.resetSharedClient();+            Object first = HttpClientTestAccess.client(Jsoup.connect("https://example.com/one"));+            SSLContext.setDefault(replacement);+            Object second = HttpClientTestAccess.client(Jsoup.connect("https://example.com/two"));+            assertNotSame(first, second);+            assertSame(replacement, ((HttpClient) second).sslContext());+        } finally {+            SSLContext.setDefault(original);+        }+    }++    @Test void reusesConfiguredClientWithinSession() {+        enableHttpClient();+        Connection session = Jsoup.newSession().auth(context -> null);+        Object first = HttpClientTestAccess.client(session.newRequest("https://example.com/one"));+        Object second = HttpClientTestAccess.client(session.newRequest("https://example.com/two"));+        assertSame(first, second);+    }++    @Test void rebuildsClientWhenAuthenticatorChanges() {+        enableHttpClient();+        RequestAuthenticator firstAuth = context -> null;+        RequestAuthenticator secondAuth = context -> null;+        assertNotSame(firstAuth, secondAuth);++        Connection session = Jsoup.newSession();+        Object first = HttpClientTestAccess.client(+            session.newRequest("https://example.com/one").auth(firstAuth));+        Object second = HttpClientTestAccess.client(+            session.newRequest("https://example.com/two").auth(secondAuth));+        assertNotSame(first, second);+    }++    @Test void rebuildsClientWhenSslContextChanges() throws Exception {+        SSLContext firstContext = SSLContext.getInstance("TLS");+        SSLContext secondContext = SSLContext.getInstance("TLS");+        firstContext.init(null, null, null);+        secondContext.init(null, null, null);+        enableHttpClient();++        Connection session = Jsoup.newSession();+        Object first = HttpClientTestAccess.client(+            session.newRequest("https://example.com/one").sslContext(firstContext));+        Object second = HttpClientTestAccess.client(+            session.newRequest("https://example.com/two").sslContext(secondContext));+        assertNotSame(first, second);+    }++    @Test void createsOneClientForConcurrentSessionRequests() throws Exception {+        enableHttpClient();+        Connection session = Jsoup.newSession().auth(context -> null);+        Set<Object> clients = concurrentClients(i -> session.newRequest("https://example.com/" + i));+        assertEquals(1, clients.size());+    }+     @Test void requestBodyStreamIsNotBuffered() {         AtomicInteger reads = new AtomicInteger();         InputStream stream = new InputStream() {@@ -60,17 +149,13 @@ public class HttpClientExecutorTest {     }      @Test void downgradesSocksProxyToUrlConnectionExecutor() {-        try {-            enableHttpClient();-            HttpConnection.Request request = new HttpConnection.Request();-            request.proxy(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("localhost", 1080)));+        enableHttpClient();+        HttpConnection.Request request = new HttpConnection.Request();+        request.proxy(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("localhost", 1080))); -            // SOCKS handling only matters on the Java 11+ path where HttpClient would otherwise be selected (and just bypasses)-            RequestExecutor executor = RequestDispatch.get(request, null);-            assertInstanceOf(UrlConnectionExecutor.class, executor);-        } finally {-            disableHttpClient(); // reset to previous default for JDK8 compat tests-        }+        // SOCKS handling only matters on the Java 11+ path where HttpClient would otherwise be selected (and just bypasses)+        RequestExecutor executor = RequestDispatch.get(request, null);+        assertInstanceOf(UrlConnectionExecutor.class, executor);     }      public static void enableHttpClient() {@@ -165,4 +250,31 @@ public class HttpClientExecutorTest {             ProxySelector.setDefault(original);         }     }++    private static Set<Object> concurrentClients(IntFunction<Connection> connectionFactory) throws Exception {+        int count = 40;+        ExecutorService executor = Executors.newFixedThreadPool(count);+        // hold every worker at the first lookup so lazy initialization is concurrent+        CountDownLatch ready = new CountDownLatch(count);+        CountDownLatch start = new CountDownLatch(1);+        Set<Object> clients = ConcurrentHashMap.newKeySet();+        List<Future<?>> futures = new ArrayList<>(count);+        try {+            for (int i = 0; i < count; i++) {+                int request = i;+                futures.add(executor.submit(() -> {+                    ready.countDown();+                    start.await();+                    clients.add(HttpClientTestAccess.client(connectionFactory.apply(request)));+                    return null;+                }));+            }+            ready.await();+            start.countDown();+            for (Future<?> future : futures) future.get();+            return clients;+        } finally {+            executor.shutdownNow();+        }+    } }src/test/java11/org/jsoup/helper/HttpClientTestAccess.javaindex baec3641..8ee3c948 100644@@ -1,7 +1,10 @@ package org.jsoup.helper; +import org.jsoup.Connection;+ import java.lang.reflect.Constructor; import java.lang.reflect.Field;+import java.lang.reflect.Method; import java.net.Proxy; import java.net.ProxySelector; import java.net.URL;@@ -43,6 +46,28 @@ final class HttpClientTestAccess {         }     } +    static Object client(Connection connection) {+        try {+            HttpConnection.Request request = (HttpConnection.Request) connection.request();+            RequestExecutor executor = RequestDispatch.get(request, null);+            Method method = executorClass().getDeclaredMethod("client");+            method.setAccessible(true);+            return method.invoke(executor);+        } catch (ReflectiveOperationException e) {+            throw new IllegalStateException("Could not retrieve the configured HttpClient", e);+        }+    }++    static void resetSharedClient() {+        try {+            Field field = executorClass().getDeclaredField("sharedClientState");+            field.setAccessible(true);+            field.set(null, null);+        } catch (ReflectiveOperationException e) {+            throw new IllegalStateException("Could not reset the shared HttpClient", e);+        }+    }+     static ProxySelector newProxyWrap() {         try {             Constructor<?> constructor = loadClass(ProxyWrapClassName).getDeclaredConstructor();src/test/java11/org/jsoup/integration/HttpClientConnectIT.javaindex 4bb4d31a..27cfc72a 100644@@ -1,9 +1,25 @@ package org.jsoup.integration; +import org.jsoup.Connection;+import org.jsoup.Jsoup; import org.jsoup.helper.HttpClientExecutorTest; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Disabled;+import org.junit.jupiter.api.Test;++import java.util.ArrayList;+import java.util.List;+import java.util.concurrent.CountDownLatch;+import java.util.concurrent.ExecutorService;+import java.util.concurrent.Executors;+import java.util.concurrent.Future;++import static org.jsoup.integration.TestServer.ProxyVia;+import static org.jsoup.integration.TestServer.origin;+import static org.jsoup.integration.TestServer.proxySettings;+import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertNull;  public class HttpClientConnectIT extends ConnectIT {     @BeforeAll@@ -28,4 +44,36 @@ public class HttpClientConnectIT extends ConnectIT {     @Override @Disabled     public void canInterruptThenJoinASpawnedThread() throws InterruptedException {     }++    @Test public void concurrentRequestsKeepProxyRoutesIsolated() throws Exception {+        int requestCount = 32;+        TestServer.ProxySettings proxy = proxySettings();+        String url = origin().hello.url();+        ExecutorService callers = Executors.newFixedThreadPool(requestCount);+        CountDownLatch ready = new CountDownLatch(requestCount);+        CountDownLatch start = new CountDownLatch(1);+        List<Future<?>> futures = new ArrayList<>(requestCount);+        try {+            // mix direct and proxied requests to verify that sharing the client does not share proxy settings+            for (int i = 0; i < requestCount; i++) {+                boolean viaProxy = i % 2 == 0;+                futures.add(callers.submit(() -> {+                    ready.countDown();+                    start.await();+                    Connection connection = Jsoup.connect(url);+                    if (viaProxy) connection.proxy(proxy.hostname, proxy.port);+                    Connection.Response response = connection.execute();+                    response.parse();+                    if (viaProxy) assertEquals(ProxyVia, response.header("Via"));+                    else assertNull(response.header("Via"));+                    return null;+                }));+            }+            ready.await();+            start.countDown();+            for (Future<?> future : futures) future.get();+        } finally {+            callers.shutdownNow();+        }+    } }