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 — Guard ChunkedWriteHandler.doFlush() against re-entrant invocation fro… · lltopk/netty@3061561

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

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.