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/01/11 12:02:24 UTC

(httpcomponents-core) branch 5.3.x updated: Bug fix: in some fringe cases the request may still be not fully completed while the response has already been fully committed by the client-side async HTTP/1.1 stream handler. Connections that cannot be kept alive must be closed only once both request and response streams are fully complete

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

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


The following commit(s) were added to refs/heads/5.3.x by this push:
     new bc1a034f7 Bug fix: in some fringe cases the request may still be not fully completed while the response has already been fully committed by the client-side async HTTP/1.1 stream handler. Connections that cannot be kept alive must be closed only once both request and response streams are fully complete
bc1a034f7 is described below

commit bc1a034f776ab8ce2bdb26312b3081b5ffdfc494
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Sat Jan 11 12:15:51 2025 +0100

    Bug fix: in some fringe cases the request may still be not fully completed while the response has already been fully committed by the client-side async HTTP/1.1 stream handler. Connections that cannot be kept alive must be closed only once both request and response streams are fully complete
---
 .../hc/core5/testing/nio/Http1IntegrationTest.java | 73 +++++++++++++++++++++-
 .../http/impl/nio/ClientHttp1StreamHandler.java    |  9 ++-
 2 files changed, 78 insertions(+), 4 deletions(-)

diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1IntegrationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1IntegrationTest.java
index 9c8b80ba3..39d1c66bc 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1IntegrationTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1IntegrationTest.java
@@ -124,6 +124,7 @@ import org.apache.hc.core5.http.protocol.RequestConnControl;
 import org.apache.hc.core5.http.protocol.RequestContent;
 import org.apache.hc.core5.http.protocol.RequestTargetHost;
 import org.apache.hc.core5.http.protocol.RequestValidateHost;
+import org.apache.hc.core5.http.support.BasicResponseBuilder;
 import org.apache.hc.core5.reactor.IOSession;
 import org.apache.hc.core5.reactor.ProtocolIOSession;
 import org.apache.hc.core5.testing.SSLTestContexts;
@@ -288,7 +289,7 @@ abstract class Http1IntegrationTest {
     }
 
     @Test
-    void testPostIdentityTransferOutOfSequenceResponse() throws Exception {
+    void testPostIdentityTransferOutOfSequenceResponseNotOK() throws Exception {
         final Http1TestServer server = resources.server();
         final Http1TestClient client = resources.client();
 
@@ -322,6 +323,76 @@ abstract class Http1IntegrationTest {
         }
     }
 
+    @Test
+    void testPostOutOfSequenceResponseOK() throws Exception {
+        final Http1TestServer server = resources.server();
+        final Http1TestClient client = resources.client();
+
+        server.register("/hello", () -> new ImmediateResponseExchangeHandler(200, "Welcome"));
+        final InetSocketAddress serverEndpoint = server.start();
+
+        client.start();
+
+        final int reqNo = 5;
+
+        final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
+        final ClientSessionEndpoint streamEndpoint = connectFuture.get();
+
+        for (int i = 0; i < reqNo; i++) {
+            final Future<Message<HttpResponse, String>> future = streamEndpoint.execute(
+                    new BasicRequestProducer(Method.POST,
+                            createRequestURI(serverEndpoint, "/hello"),
+                            new MultiLineEntityProducer("Hello", 512 * i)),
+                    new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
+            final Message<HttpResponse, String> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
+
+            Assertions.assertNotNull(result);
+            final HttpResponse response = result.getHead();
+            final String entity = result.getBody();
+            Assertions.assertNotNull(response);
+            Assertions.assertEquals(200, response.getCode());
+            Assertions.assertEquals("Welcome", entity);
+        }
+    }
+
+    @Test
+    void testPostOutOfSequenceResponseOKConnectionClose() throws Exception {
+        final Http1TestServer server = resources.server();
+        final Http1TestClient client = resources.client();
+
+        server.register("/hello", () -> new ImmediateResponseExchangeHandler(
+                BasicResponseBuilder.create(200)
+                        .addHeader(HttpHeaders.CONNECTION, "Close")
+                        .build(),
+                "Welcome"));
+        final InetSocketAddress serverEndpoint = server.start();
+
+        client.start();
+
+        final int reqNo = 5;
+
+        for (int i = 0; i < reqNo; i++) {
+            final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
+            final ClientSessionEndpoint streamEndpoint = connectFuture.get();
+
+            final Future<Message<HttpResponse, String>> future = streamEndpoint.execute(
+                    new BasicRequestProducer(Method.POST,
+                            createRequestURI(serverEndpoint, "/hello"),
+                            new MultiLineEntityProducer("Hello", 512 * i)),
+                    new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
+            final Message<HttpResponse, String> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
+
+            streamEndpoint.close();
+
+            Assertions.assertNotNull(result);
+            final HttpResponse response = result.getHead();
+            final String entity = result.getBody();
+            Assertions.assertNotNull(response);
+            Assertions.assertEquals(200, response.getCode());
+            Assertions.assertEquals("Welcome", entity);
+        }
+    }
+
     @Test
     void testSimpleGetsPipelined() throws Exception {
         final Http1TestServer server = resources.server();
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ClientHttp1StreamHandler.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ClientHttp1StreamHandler.java
index 62dd4c242..18cc5af96 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ClientHttp1StreamHandler.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ClientHttp1StreamHandler.java
@@ -91,8 +91,11 @@ class ClientHttp1StreamHandler implements ResourceHolder {
 
             @Override
             public void endStream(final List<? extends Header> trailers) throws IOException {
-                outputChannel.complete(trailers);
                 requestState = MessageState.COMPLETE;
+                if (!keepAlive && responseState == MessageState.COMPLETE) {
+                    outputChannel.close();
+                }
+                outputChannel.complete(trailers);
             }
 
             @Override
@@ -269,10 +272,10 @@ class ClientHttp1StreamHandler implements ResourceHolder {
         if (done.get() || responseState != MessageState.BODY) {
             throw new ProtocolException("Unexpected message data");
         }
-        if (!keepAlive) {
+        responseState = MessageState.COMPLETE;
+        if (!keepAlive && requestState == MessageState.COMPLETE) {
             outputChannel.close();
         }
-        responseState = MessageState.COMPLETE;
         exchangeHandler.streamEnd(trailers);
     }