Skip to content

Commit fe377cf

Browse files
committed
Bug fix: Shared input buffer to propagate aborted status as I/O interrupted exception
1 parent 7484129 commit fe377cf

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/classic/SharedInputBuffer.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,19 @@ private void awaitInput() throws InterruptedIOException {
108108
}
109109
}
110110

111+
private void ensureNotAborted() throws InterruptedIOException {
112+
if (aborted) {
113+
throw new InterruptedIOException("Operation aborted");
114+
}
115+
}
116+
111117
@Override
112118
public int read() throws IOException {
113119
lock.lock();
114120
try {
115121
setOutputMode();
116122
awaitInput();
117-
if (aborted) {
118-
return -1;
119-
}
123+
ensureNotAborted();
120124
if (!buffer().hasRemaining() && endStream) {
121125
return -1;
122126
}
@@ -140,9 +144,7 @@ public int read(final byte[] b, final int off, final int len) throws IOException
140144
try {
141145
setOutputMode();
142146
awaitInput();
143-
if (aborted) {
144-
return -1;
145-
}
147+
ensureNotAborted();
146148
if (!buffer().hasRemaining() && endStream) {
147149
return -1;
148150
}

httpcore5/src/test/java/org/apache/hc/core5/http/nio/support/classic/TestSharedInputBuffer.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727

2828
package org.apache.hc.core5.http.nio.support.classic;
2929

30+
import java.io.InterruptedIOException;
3031
import java.nio.ByteBuffer;
3132
import java.nio.charset.Charset;
3233
import java.nio.charset.StandardCharsets;
3334
import java.util.Random;
3435
import java.util.concurrent.Callable;
36+
import java.util.concurrent.ExecutionException;
3537
import java.util.concurrent.ExecutorService;
3638
import java.util.concurrent.Executors;
3739
import java.util.concurrent.Future;
@@ -194,7 +196,8 @@ void testMultithreadingReadStreamAbort() throws Exception {
194196
final Future<Integer> task2 = executorService.submit((Callable<Integer>) inputBuffer::read);
195197

196198
Assertions.assertEquals(Boolean.TRUE, task1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()));
197-
Assertions.assertEquals(Integer.valueOf(-1), task2.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()));
199+
final ExecutionException ex = Assertions.assertThrows(ExecutionException.class, () -> task2.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()));
200+
Assertions.assertInstanceOf(InterruptedIOException.class, ex.getCause());
198201
Mockito.verify(capacityChannel, Mockito.never()).update(10);
199202
}
200203

0 commit comments

Comments
 (0)