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:15 UTC
(httpcomponents-core) 01/04: Bug fix: Made shared output buffer #writeCompleted block until the end of stream has been written out
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();
+ }
}
}