GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Port: Guard ChunkedWriteHandler.doFlush() against re-entrant invocati… · netty/netty@3ac1c84

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • handler/src
    • main/java/io/netty/handler/stream
      • ChunkedWriteHandler.java
    • test/java/io/netty/handler/stream
      • ChunkedWriteHandlerTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎handler/src/main/java/io/netty/handler/stream/ChunkedWriteHandler.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/stream/ChunkedWriteHandler.java
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public class ChunkedWriteHandler implements ChannelInboundHandler, ChannelOutbou
7474

7575
private Queue<PendingWrite> queue;
7676
private volatile ChannelHandlerContext ctx;
77+
private boolean inFlush;
78+
private boolean flushPending;
7779

7880
public ChunkedWriteHandler() {
7981
}
@@ -205,6 +207,31 @@ private void discard(Throwable cause) {
205207
}
206208

207209
private void doFlush(final ChannelHandlerContext ctx) {
210+
if (inFlush) {
211+
// doFlush() must not re-enter itself: ChunkedInput.readChunk(...) and isEndOfInput() run
212+
// between queue.peek() and queue.remove(), and user code executed there may
213+
// synchronously trigger flush(), resumeTransfer(), channelInactive(...) or
214+
// channelWritabilityChanged(...) on this very handler. A nested doFlush() would peek
215+
// and consume the same queue entry again, which made the outer invocation fail with
216+
// NoSuchElementException and let both invocations read from the same (already closed)
217+
// input. Remember the nested call instead: the outer invocation re-runs the drain
218+
// loop before returning, so a resumeTransfer(...) that arrives while it is about to
219+
// suspend is not lost (see the old lock-based guard and its flushRequired follow-up).
220+
flushPending = true;
221+
return;
222+
}
223+
inFlush = true;
224+
try {
225+
do {
226+
flushPending = false;
227+
doFlush0(ctx);
228+
} while (flushPending);
229+
} finally {
230+
inFlush = false;
231+
}
232+
}
233+
234+
private void doFlush0(final ChannelHandlerContext ctx) {
208235
final Channel channel = ctx.channel();
209236
if (!channel.isActive()) {
210237
// Even after discarding all previous queued objects we should propagate the flush through
Collapse file

‎handler/src/test/java/io/netty/handler/stream/ChunkedWriteHandlerTest.java‎

Copy file name to clipboardExpand all lines: handler/src/test/java/io/netty/handler/stream/ChunkedWriteHandlerTest.java
+175Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.netty.buffer.ByteBuf;
1919
import io.netty.buffer.ByteBufAllocator;
2020
import io.netty.buffer.Unpooled;
21+
import io.netty.channel.Channel;
2122
import io.netty.channel.ChannelHandlerContext;
2223
import io.netty.channel.ChannelOutboundHandler;
2324
import io.netty.channel.embedded.EmbeddedChannel;
@@ -821,4 +822,178 @@ boolean isClosed() {
821822
return closed;
822823
}
823824
}
825+
826+
@Test
827+
public void testResumeTransferReenteredFromReadChunkMustNotBeLostWhenSuspending() throws Exception {
828+
final ChunkedWriteHandler streamer = new ChunkedWriteHandler();
829+
final EmbeddedChannel ch = new EmbeddedChannel(streamer);
830+
SuspendResumeChunkedInput input = new SuspendResumeChunkedInput(streamer);
831+
Future<Void> future = ch.write(input);
832+
ch.flush();
833+
834+
// The input suspends (first readChunk(...) returns null) after its implementation has
835+
// already produced data and synchronously called resumeTransfer() from within
836+
// readChunk(...). A plain re-entrancy guard would drop that nested call and leave the
837+
// transfer suspended forever; the guard must re-run the drain loop instead.
838+
ByteBuf chunk = ch.readOutbound();
839+
assertNull(ch.readOutbound());
840+
assertFalse(ch.finishAndReleaseAll());
841+
842+
assertEquals(42, chunk.readInt());
843+
chunk.release();
844+
assertEquals(2, input.readChunkCalls());
845+
assertTrue(input.isClosed());
846+
assertTrue(future.isSuccess());
847+
}
848+
849+
private static final class SuspendResumeChunkedInput implements ChunkedInput<ByteBuf> {
850+
private final ChunkedWriteHandler streamer;
851+
private final AtomicInteger readChunkCalls = new AtomicInteger();
852+
private final AtomicBoolean closed = new AtomicBoolean();
853+
854+
SuspendResumeChunkedInput(ChunkedWriteHandler streamer) {
855+
this.streamer = streamer;
856+
}
857+
858+
int readChunkCalls() {
859+
return readChunkCalls.get();
860+
}
861+
862+
boolean isClosed() {
863+
return closed.get();
864+
}
865+
866+
@Override
867+
public boolean isEndOfInput() {
868+
return readChunkCalls.get() >= 2;
869+
}
870+
871+
@Override
872+
public void close() {
873+
closed.set(true);
874+
}
875+
876+
@Override
877+
public ByteBuf readChunk(ChannelHandlerContext ctx) throws Exception {
878+
return readChunk(ctx.alloc());
879+
}
880+
881+
@Override
882+
public ByteBuf readChunk(ByteBufAllocator allocator) throws Exception {
883+
if (readChunkCalls.incrementAndGet() == 1) {
884+
// Data becomes available "now", so the producer resumes synchronously, but this
885+
// call still reports "no chunk yet": doFlush(...) is about to suspend when the
886+
// nested resumeTransfer() arrives, and that call must not be dropped.
887+
streamer.resumeTransfer();
888+
return null;
889+
}
890+
ByteBuf buffer = allocator.buffer(4);
891+
buffer.writeInt(42);
892+
return buffer;
893+
}
894+
895+
@Override
896+
public long length() {
897+
return -1;
898+
}
899+
900+
@Override
901+
public long progress() {
902+
return 0;
903+
}
904+
}
905+
906+
@Test
907+
public void testFlushReenteredFromReadChunkMustNotConsumeQueueEntryTwice() throws Exception {
908+
final EmbeddedChannel ch = new EmbeddedChannel(new ChunkedWriteHandler());
909+
ReentrantFlushChunkedInput input = new ReentrantFlushChunkedInput(ch);
910+
Future<Void> future = ch.write(input);
911+
ch.flush();
912+
913+
ByteBuf first = ch.readOutbound();
914+
ByteBuf second = ch.readOutbound();
915+
assertNull(ch.readOutbound());
916+
// Before the re-entrancy guard, the nested flush() consumed the queue entry and both
917+
// invocations read from the same input; the outer doFlush(...) then failed with
918+
// NoSuchElementException from ArrayDeque.remove() on the already-empty queue.
919+
// Both chunks were read above, so no message must be left in the buffers.
920+
assertFalse(ch.finishAndReleaseAll());
921+
922+
assertEquals(1, first.readInt());
923+
first.release();
924+
assertEquals(2, second.readInt());
925+
second.release();
926+
// The input must have been read exactly twice, and closed exactly once, after the last
927+
// read: before the guard, close() happened from the nested invocation while the outer
928+
// readChunk(...) call had not even returned yet.
929+
assertEquals(2, input.readChunkCalls());
930+
assertTrue(input.isClosed());
931+
assertEquals(2, input.readChunkCallsAtClose());
932+
assertTrue(future.isSuccess());
933+
}
934+
935+
private static final class ReentrantFlushChunkedInput implements ChunkedInput<ByteBuf> {
936+
private final Channel channel;
937+
private final AtomicInteger readChunkCalls = new AtomicInteger();
938+
private final AtomicInteger readChunkCallsAtClose = new AtomicInteger(-1);
939+
private final AtomicBoolean closed = new AtomicBoolean();
940+
private int chunkNo;
941+
942+
ReentrantFlushChunkedInput(Channel channel) {
943+
this.channel = channel;
944+
}
945+
946+
int readChunkCalls() {
947+
return readChunkCalls.get();
948+
}
949+
950+
int readChunkCallsAtClose() {
951+
return readChunkCallsAtClose.get();
952+
}
953+
954+
boolean isClosed() {
955+
return closed.get();
956+
}
957+
958+
@Override
959+
public boolean isEndOfInput() {
960+
return chunkNo >= 2;
961+
}
962+
963+
@Override
964+
public void close() {
965+
closed.set(true);
966+
readChunkCallsAtClose.set(readChunkCalls.get());
967+
}
968+
969+
@Override
970+
public ByteBuf readChunk(ChannelHandlerContext ctx) throws Exception {
971+
return readChunk(ctx.alloc());
972+
}
973+
974+
@Override
975+
public ByteBuf readChunk(ByteBufAllocator allocator) throws Exception {
976+
int call = readChunkCalls.incrementAndGet();
977+
if (call == 1) {
978+
// Runs between queue.peek() and queue.remove() in doFlush(...). Re-enter the
979+
// handler synchronously, like incidental pipeline activity triggered from user
980+
// code (a flush, a writability change, a close notification) would.
981+
channel.flush();
982+
}
983+
chunkNo++;
984+
ByteBuf buffer = allocator.buffer(4);
985+
buffer.writeInt(chunkNo);
986+
return buffer;
987+
}
988+
989+
@Override
990+
public long length() {
991+
return -1;
992+
}
993+
994+
@Override
995+
public long progress() {
996+
return 0;
997+
}
998+
}
824999
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.