Skip to content

Commit

Permalink
Bug fix: Shared input buffer to propagate aborted status as I/O inter…
Browse files Browse the repository at this point in the history
…rupted exception
  • Loading branch information
ok2c committed Jan 21, 2025
1 parent 7484129 commit fe377cf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit fe377cf

Please sign in to comment.