You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2025/10/21 10:59:44 UTC

(httpcomponents-core) 01/03: Ensure connection is closed immediately upon socket timeout

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

olegk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git

commit cea8af047300c6b7fcebdbaebdf3442054e8865d
Author: Alexis Le Dantec <al...@palantir.com>
AuthorDate: Mon Oct 20 15:49:43 2025 +0200

    Ensure connection is closed immediately upon socket timeout
---
 .../testing/classic/ClassicIntegrationTest.java    | 113 +++++++++++++++++++--
 .../hc/core5/http/impl/io/BHttpConnectionBase.java |   5 +
 2 files changed, 109 insertions(+), 9 deletions(-)

diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicIntegrationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicIntegrationTest.java
index 22f27425c..ab6aa4f56 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicIntegrationTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicIntegrationTest.java
@@ -32,11 +32,16 @@
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.net.SocketTimeoutException;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Random;
+import java.util.concurrent.*;
+import java.util.function.Consumer;
 
 import org.apache.hc.core5.http.ClassicHttpRequest;
 import org.apache.hc.core5.http.ClassicHttpResponse;
@@ -51,7 +56,16 @@
 import org.apache.hc.core5.http.Method;
 import org.apache.hc.core5.http.ProtocolException;
 import org.apache.hc.core5.http.URIScheme;
+import org.apache.hc.core5.http.config.CharCodingConfig;
 import org.apache.hc.core5.http.config.Http1Config;
+import org.apache.hc.core5.http.impl.DefaultAddressResolver;
+import org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy;
+import org.apache.hc.core5.http.impl.HttpProcessors;
+import org.apache.hc.core5.http.impl.io.DefaultBHttpClientConnectionFactory;
+import org.apache.hc.core5.http.impl.io.HttpRequestExecutor;
+import org.apache.hc.core5.http.io.HttpClientConnection;
+import org.apache.hc.core5.http.io.HttpConnectionFactory;
+import org.apache.hc.core5.http.io.SocketConfig;
 import org.apache.hc.core5.http.io.entity.AbstractHttpEntity;
 import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
 import org.apache.hc.core5.http.io.entity.EntityUtils;
@@ -59,21 +73,21 @@
 import org.apache.hc.core5.http.io.support.BasicHttpServerExpectationDecorator;
 import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
 import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
-import org.apache.hc.core5.http.protocol.DefaultHttpProcessor;
-import org.apache.hc.core5.http.protocol.HttpContext;
-import org.apache.hc.core5.http.protocol.HttpCoreContext;
-import org.apache.hc.core5.http.protocol.RequestConnControl;
-import org.apache.hc.core5.http.protocol.RequestContent;
-import org.apache.hc.core5.http.protocol.RequestExpectContinue;
-import org.apache.hc.core5.http.protocol.RequestTE;
-import org.apache.hc.core5.http.protocol.RequestTargetHost;
-import org.apache.hc.core5.http.protocol.RequestUserAgent;
+import org.apache.hc.core5.http.protocol.*;
+import org.apache.hc.core5.io.CloseMode;
+import org.apache.hc.core5.net.URIAuthority;
+import org.apache.hc.core5.testing.SSLTestContexts;
 import org.apache.hc.core5.testing.extension.classic.ClassicTestResources;
 import org.apache.hc.core5.util.Timeout;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.RegisterExtension;
 
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLParameters;
+import javax.net.ssl.SSLSocket;
+import javax.net.ssl.SSLSocketFactory;
+
 abstract class ClassicIntegrationTest {
 
     private static final Timeout TIMEOUT = Timeout.ofMinutes(1);
@@ -775,4 +789,85 @@ void testHeaderTooLargePost() throws Exception {
         }
     }
 
+    @Test
+    void testImmediateCloseUponSocketTimeout() throws Exception {
+        final int socketTimeoutMillis = 1000;
+        final int serverDelayMillis = 5 * socketTimeoutMillis;
+
+        final ClassicTestServer server = testResources.server();
+
+        final CountDownLatch serverDelayStarted = new CountDownLatch(1);
+
+        // Configure server to delay significantly before responding
+        server.register("*", (request, response, context) -> {
+            serverDelayStarted.countDown();
+            try {
+                // Delay much longer than socket timeout
+                Thread.sleep(serverDelayMillis);
+            } catch (final InterruptedException ex) {
+                Thread.currentThread().interrupt();
+            }
+            response.setCode(HttpStatus.SC_OK);
+            response.setEntity(new StringEntity("Delayed response", ContentType.TEXT_PLAIN));
+        });
+
+        server.start();
+
+        final HttpHost host = new HttpHost(scheme.id, "localhost", server.getPort());
+        final HttpCoreContext context = HttpCoreContext.create();
+        final HttpConnectionFactory<? extends HttpClientConnection> connFactory =
+                DefaultBHttpClientConnectionFactory.builder().build();
+
+        final HttpRequestExecutor requestExecutor = new HttpRequestExecutor();
+        final HttpProcessor processor = HttpProcessors.client();
+
+        final BasicClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
+        request.setAuthority(new URIAuthority(host));
+        request.setScheme(host.getSchemeName());
+
+        runWithSocket(host, socketTimeoutMillis, socket -> {
+            try {
+                final HttpClientConnection connection = connFactory.createConnection(socket);
+
+                requestExecutor.preProcess(request, processor, context);
+
+                final long startTime = System.currentTimeMillis();
+                try (final ClassicHttpResponse response = requestExecutor.execute(
+                        request, connection, context)) {
+                    Assertions.fail("Expected SocketTimeoutException not thrown");
+                } catch (final SocketTimeoutException e) {
+                    // Expected due to server delay exceeding socket timeout
+                }
+
+                final long endTime = System.currentTimeMillis();
+                final long durationMillis = endTime - startTime;
+                // Assert that the timeout occurred around the socket timeout duration
+                Assertions.assertTrue(durationMillis >= socketTimeoutMillis && durationMillis < socketTimeoutMillis * 2,
+                        String.format("Socket timeout should occur around %dms (took %dms)",
+                                socketTimeoutMillis, durationMillis));
+            } catch (final IOException | HttpException e) {
+                Assertions.fail("IOException during request execution: " + e.getMessage());
+            }
+        });
+    }
+
+    private static void runWithSocket(HttpHost host, int socketTimeoutMillis, Consumer<Socket> socketConsumer)
+            throws IOException {
+        try (final Socket clientSocket = new Socket()) {
+            clientSocket.setSoTimeout(socketTimeoutMillis);
+            clientSocket.connect(new InetSocketAddress(host.getHostName(), host.getPort()));
+            if (host.getSchemeName().equalsIgnoreCase("http")) {
+                socketConsumer.accept(clientSocket);
+                return;
+            }
+
+            try (SSLSocket sslSocket = (SSLSocket) SSLTestContexts.createClientSSLContext()
+                    .getSocketFactory()
+                    .createSocket(clientSocket, host.getHostName(), -1, true)) {
+                sslSocket.startHandshake();
+
+                socketConsumer.accept(sslSocket);
+            }
+        }
+    }
 }
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/BHttpConnectionBase.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/BHttpConnectionBase.java
index a30058037..f0aedb629 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/BHttpConnectionBase.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/BHttpConnectionBase.java
@@ -238,6 +238,11 @@ public void close(final CloseMode closeMode) {
                 try {
                     // force abortive close (RST)
                     baseSocket.setSoLinger(true, 0);
+                    // Some implementations like SSLSocketImpl read from the input stream to do a graceful close
+                    // We want an immediate close here, so we set a minimal timeout to prevent blocking
+                    // This specific case could happen if the underlying socketHolder is initialized through the
+                    // bind(Socket) method with an SSLSocket instance.
+                    baseSocket.setSoTimeout(1);
                 } catch (final IOException ignore) {
                 } finally {
                     Closer.closeQuietly(baseSocket);