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:25:43 UTC

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

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 81a3c82473e93cdf8ef5b7873e44046f24c6d871
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);
     }