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/21 19:48:14 UTC

(httpcomponents-core) branch 5.3.x updated (c14e4947f -> fe377cfb2)

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

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


    from c14e4947f HTTPCORE-775: The SSLIOSession::write does not handle SSLEngineResult#BUFFER_OVERFLOW
     new 88e9ab944 Bug fix: Made shared output buffer #writeCompleted block until the end of stream has been written out
     new 6f1936706 Bug fix: Corrected shutdown of async HTTP/1.1 connections half-closed by the opposite endpoint
     new 748412905 Bug fix: Half-closed SSL sessions must not shut down until explicitly closed by the application (protocol) layer
     new fe377cfb2 Bug fix: Shared input buffer to propagate aborted status as I/O interrupted exception

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../http/impl/nio/AbstractHttp1StreamDuplexer.java |  4 +-
 .../nio/support/classic/SharedInputBuffer.java     | 14 +++---
 .../nio/support/classic/SharedOutputBuffer.java    | 52 ++++++++++++----------
 .../apache/hc/core5/reactor/ssl/SSLIOSession.java  |  9 ++--
 .../nio/support/classic/TestSharedInputBuffer.java |  5 ++-
 .../support/classic/TestSharedOutputBuffer.java    | 32 ++++++++++---
 6 files changed, 73 insertions(+), 43 deletions(-)


(httpcomponents-core) 03/04: Bug fix: Half-closed SSL sessions must not shut down until explicitly closed by the application (protocol) layer

Posted by ol...@apache.org.
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

commit 7484129050f571da85a4bcd2b616425f5aa68098
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Wed Jan 15 15:04:34 2025 +0100

    Bug fix: Half-closed SSL sessions must not shut down until explicitly closed by the application (protocol) layer
---
 .../main/java/org/apache/hc/core5/reactor/ssl/SSLIOSession.java  | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/httpcore5/src/main/java/org/apache/hc/core5/reactor/ssl/SSLIOSession.java b/httpcore5/src/main/java/org/apache/hc/core5/reactor/ssl/SSLIOSession.java
index 7aacf574b..f7887788a 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/reactor/ssl/SSLIOSession.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/reactor/ssl/SSLIOSession.java
@@ -101,6 +101,7 @@ enum TLSHandShakeState { READY, INITIALIZED, HANDSHAKING, COMPLETE }
     private volatile Status status = Status.ACTIVE;
     private volatile Timeout socketTimeout;
     private volatile TlsDetails tlsDetails;
+    private volatile boolean appClosed;
 
     /**
      * Creates new instance of {@code SSLIOSession} class.
@@ -477,7 +478,8 @@ private void updateEventMask() {
                     && (handshakeStatus == HandshakeStatus.NOT_HANDSHAKING || handshakeStatus == HandshakeStatus.FINISHED)
                     && !this.outEncrypted.hasData()
                     && this.sslEngine.isOutboundDone()
-                    && (this.endOfStream || this.sslEngine.isInboundDone())) {
+                    && (this.endOfStream || this.sslEngine.isInboundDone())
+                    && appClosed) {
                 this.status = Status.CLOSED;
             }
             // Abnormal session termination
@@ -520,8 +522,6 @@ private void updateEventMask() {
             // Do we have encrypted data ready to be sent?
             if (this.outEncrypted.hasData()) {
                 newMask = newMask | EventMask.WRITE;
-            } else if (this.sslEngine.isOutboundDone()) {
-                newMask = newMask & ~EventMask.WRITE;
             }
 
             // Update the mask if necessary
@@ -648,7 +648,7 @@ private void encryptData(final IOSession protocolSession) throws IOException {
         this.session.getLock().lock();
         try {
             appReady = (this.appEventMask & SelectionKey.OP_WRITE) > 0
-                    && this.status == Status.ACTIVE
+                    && this.status.compareTo(Status.CLOSED) < 0
                     && this.sslEngine.getHandshakeStatus() == HandshakeStatus.NOT_HANDSHAKING;
         } finally {
             this.session.getLock().unlock();
@@ -724,6 +724,7 @@ public void close() {
     public void close(final CloseMode closeMode) {
         this.session.getLock().lock();
         try {
+            appClosed = true;
             if (closeMode == CloseMode.GRACEFUL) {
                 if (this.status.compareTo(Status.CLOSING) >= 0) {
                     return;


(httpcomponents-core) 01/04: Bug fix: Made shared output buffer #writeCompleted block until the end of stream has been written out

Posted by ol...@apache.org.
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

commit 88e9ab94429fec6b97910a8031862b7c472435ba
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Sun Jan 12 12:47:49 2025 +0100

    Bug fix: Made shared output buffer #writeCompleted block until the end of stream has been written out
---
 .../nio/support/classic/SharedOutputBuffer.java    | 52 ++++++++++++----------
 .../support/classic/TestSharedOutputBuffer.java    | 32 ++++++++++---
 2 files changed, 54 insertions(+), 30 deletions(-)

diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/classic/SharedOutputBuffer.java b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/classic/SharedOutputBuffer.java
index bf25a9b78..1db488319 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/classic/SharedOutputBuffer.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/classic/SharedOutputBuffer.java
@@ -29,6 +29,7 @@
 import java.io.IOException;
 import java.io.InterruptedIOException;
 import java.nio.ByteBuffer;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.locks.ReentrantLock;
 
 import org.apache.hc.core5.annotation.Contract;
@@ -41,20 +42,25 @@
 @Contract(threading = ThreadingBehavior.SAFE)
 public final class SharedOutputBuffer extends AbstractSharedBuffer implements ContentOutputBuffer {
 
+    private final AtomicBoolean endStreamPropagated;
     private volatile DataStreamChannel dataStreamChannel;
     private volatile boolean hasCapacity;
-    private volatile boolean endStreamPropagated;
 
     public SharedOutputBuffer(final ReentrantLock lock, final int initialBufferSize) {
         super(lock, initialBufferSize);
         this.hasCapacity = false;
-        this.endStreamPropagated = false;
+        this.endStreamPropagated = new AtomicBoolean();
     }
 
     public SharedOutputBuffer(final int bufferSize) {
         this(new ReentrantLock(), bufferSize);
     }
 
+    @Override
+    public boolean isEndStream() {
+        return endStreamPropagated.get();
+    }
+
     public void flush(final DataStreamChannel channel) throws IOException {
         lock.lock();
         try {
@@ -65,7 +71,9 @@ public void flush(final DataStreamChannel channel) throws IOException {
                 dataStreamChannel.write(buffer());
             }
             if (!buffer().hasRemaining() && endStream) {
-                propagateEndStream();
+                if (endStreamPropagated.compareAndSet(false, true)) {
+                    dataStreamChannel.endStream();
+                }
             }
             condition.signalAll();
         } finally {
@@ -132,14 +140,7 @@ public void writeCompleted() throws IOException {
         try {
             if (!endStream) {
                 endStream = true;
-                if (dataStreamChannel != null) {
-                    setOutputMode();
-                    if (buffer().hasRemaining()) {
-                        dataStreamChannel.requestOutput();
-                    } else {
-                        propagateEndStream();
-                    }
-                }
+                waitEndStream();
             }
         } finally {
             lock.unlock();
@@ -147,27 +148,32 @@ public void writeCompleted() throws IOException {
     }
 
     private void waitFlush() throws InterruptedIOException {
-        setOutputMode();
         if (dataStreamChannel != null) {
             dataStreamChannel.requestOutput();
         }
-        ensureNotAborted();
+        setOutputMode();
         while (buffer().hasRemaining() || !hasCapacity) {
-            try {
-                condition.await();
-            } catch (final InterruptedException ex) {
-                Thread.currentThread().interrupt();
-                throw new InterruptedIOException(ex.getMessage());
-            }
             ensureNotAborted();
+            waitForSignal();
         }
         setInputMode();
     }
 
-    private void propagateEndStream() throws IOException {
-        if (!endStreamPropagated) {
-            dataStreamChannel.endStream();
-            endStreamPropagated = true;
+    private void waitEndStream() throws InterruptedIOException {
+        if (dataStreamChannel != null) {
+            dataStreamChannel.requestOutput();
+        }
+        while (!endStreamPropagated.get() && !aborted) {
+            waitForSignal();
+        }
+    }
+
+    private void waitForSignal() throws InterruptedIOException {
+        try {
+            condition.await();
+        } catch (final InterruptedException ex) {
+            Thread.currentThread().interrupt();
+            throw new InterruptedIOException(ex.getMessage());
         }
     }
 
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/nio/support/classic/TestSharedOutputBuffer.java b/httpcore5/src/test/java/org/apache/hc/core5/http/nio/support/classic/TestSharedOutputBuffer.java
index dd33259ae..3a03e14d1 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/http/nio/support/classic/TestSharedOutputBuffer.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/http/nio/support/classic/TestSharedOutputBuffer.java
@@ -219,17 +219,35 @@ void testMultithreadingWriteStreamAbort() throws Exception {
     }
 
     @Test
-    void testEndStreamOnlyCalledOnce() throws IOException {
-
-        final DataStreamChannel channel = Mockito.mock(DataStreamChannel.class);
+    void testEndStreamOnlyCalledOnce() throws Exception {
         final SharedOutputBuffer outputBuffer = new SharedOutputBuffer(20);
 
-        outputBuffer.flush(channel);
+        final WritableByteChannelMock channel = new WritableByteChannelMock(1024);
+        final DataStreamChannelMock dataStreamChannel = Mockito.spy(new DataStreamChannelMock(channel));
 
-        outputBuffer.writeCompleted();
-        outputBuffer.flush(channel);
+        final ExecutorService executorService = Executors.newFixedThreadPool(2);
+        try {
+            final Future<Boolean> task1 = executorService.submit(() -> {
+                outputBuffer.writeCompleted();
+                return Boolean.TRUE;
+            });
+            final Future<Boolean> task2 = executorService.submit(() -> {
+                for (;;) {
+                    outputBuffer.flush(dataStreamChannel);
+                    if (outputBuffer.isEndStream()) {
+                        break;
+                    }
+                }
+                return Boolean.TRUE;
+            });
 
-        Mockito.verify(channel, Mockito.times(1)).endStream();
+            Assertions.assertEquals(Boolean.TRUE, task1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()));
+            Assertions.assertEquals(Boolean.TRUE, task2.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()));
+
+            Mockito.verify(dataStreamChannel, Mockito.times(1)).endStream();
+        } finally {
+            executorService.shutdownNow();
+        }
     }
 
 }


(httpcomponents-core) 02/04: Bug fix: Corrected shutdown of async HTTP/1.1 connections half-closed by the opposite endpoint

Posted by ol...@apache.org.
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

commit 6f1936706b4049b74193545fd828e70be519fb83
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Mon Jan 13 23:30:02 2025 +0100

    Bug fix: Corrected shutdown of async HTTP/1.1 connections half-closed by the opposite endpoint
---
 .../apache/hc/core5/http/impl/nio/AbstractHttp1StreamDuplexer.java    | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/AbstractHttp1StreamDuplexer.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/AbstractHttp1StreamDuplexer.java
index b46de7b8c..673a0556c 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/AbstractHttp1StreamDuplexer.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/AbstractHttp1StreamDuplexer.java
@@ -270,7 +270,7 @@ public final void onInput(final ByteBuffer src) throws HttpException, IOExceptio
             inTransportMetrics.incrementBytesTransferred(n);
         }
 
-        if (connState.compareTo(ConnectionState.GRACEFUL_SHUTDOWN) >= 0 && inbuf.hasData() && inputIdle()) {
+        if (connState.compareTo(ConnectionState.GRACEFUL_SHUTDOWN) >= 0 && !inbuf.hasData() && inputIdle()) {
             ioSession.clearEvent(SelectionKey.OP_READ);
             return;
         }
@@ -347,7 +347,7 @@ public final void onInput(final ByteBuffer src) throws HttpException, IOExceptio
         } while (inbuf.hasData());
 
         if (endOfStream && !inbuf.hasData()) {
-            if (outputIdle() && inputIdle()) {
+            if (inputIdle()) {
                 requestShutdown(CloseMode.GRACEFUL);
             } else {
                 shutdownSession(new ConnectionClosedException("Connection closed by peer"));


(httpcomponents-core) 04/04: Bug fix: Shared input buffer to propagate aborted status as I/O interrupted exception

Posted by ol...@apache.org.
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

commit fe377cfb2dd7ec0241c1842d60b545c616cca0a5
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Fri Jan 17 22:33:53 2025 +0100

    Bug fix: Shared input buffer to propagate aborted status as I/O interrupted exception
---
 .../core5/http/nio/support/classic/SharedInputBuffer.java  | 14 ++++++++------
 .../http/nio/support/classic/TestSharedInputBuffer.java    |  5 ++++-
 2 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/classic/SharedInputBuffer.java b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/classic/SharedInputBuffer.java
index cb3d30245..b0a155f56 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/classic/SharedInputBuffer.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/classic/SharedInputBuffer.java
@@ -108,15 +108,19 @@ private void awaitInput() throws InterruptedIOException {
         }
     }
 
+    private void ensureNotAborted() throws InterruptedIOException {
+        if (aborted) {
+            throw new InterruptedIOException("Operation aborted");
+        }
+    }
+
     @Override
     public int read() throws IOException {
         lock.lock();
         try {
             setOutputMode();
             awaitInput();
-            if (aborted) {
-                return -1;
-            }
+            ensureNotAborted();
             if (!buffer().hasRemaining() && endStream) {
                 return -1;
             }
@@ -140,9 +144,7 @@ public int read(final byte[] b, final int off, final int len) throws IOException
         try {
             setOutputMode();
             awaitInput();
-            if (aborted) {
-                return -1;
-            }
+            ensureNotAborted();
             if (!buffer().hasRemaining() && endStream) {
                 return -1;
             }
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/nio/support/classic/TestSharedInputBuffer.java b/httpcore5/src/test/java/org/apache/hc/core5/http/nio/support/classic/TestSharedInputBuffer.java
index 1335d9ca4..38eeb1b5d 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/http/nio/support/classic/TestSharedInputBuffer.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/http/nio/support/classic/TestSharedInputBuffer.java
@@ -27,11 +27,13 @@
 
 package org.apache.hc.core5.http.nio.support.classic;
 
+import java.io.InterruptedIOException;
 import java.nio.ByteBuffer;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.util.Random;
 import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
@@ -194,7 +196,8 @@ void testMultithreadingReadStreamAbort() throws Exception {
         final Future<Integer> task2 = executorService.submit((Callable<Integer>) inputBuffer::read);
 
         Assertions.assertEquals(Boolean.TRUE, task1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()));
-        Assertions.assertEquals(Integer.valueOf(-1), task2.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()));
+        final ExecutionException ex = Assertions.assertThrows(ExecutionException.class, () -> task2.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()));
+        Assertions.assertInstanceOf(InterruptedIOException.class, ex.getCause());
         Mockito.verify(capacityChannel, Mockito.never()).update(10);
     }