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: HTTP/2: Fix stream stall when toggling auto-read on a child cha… · netty/netty@9af412f

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-http2/src
    • main/java/io/netty/handler/codec/http2
      • AbstractHttp2StreamChannel.java
    • test/java/io/netty/handler/codec/http2
      • Http2MultiplexTest.java
      • Http2MultiplexTransportTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-http2/src/main/java/io/netty/handler/codec/http2/AbstractHttp2StreamChannel.java‎

Copy file name to clipboardExpand all lines: codec-http2/src/main/java/io/netty/handler/codec/http2/AbstractHttp2StreamChannel.java
+46-11Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -593,10 +593,11 @@ void fireChildRead(Http2Frame frame) {
593593
assert executor().inEventLoop();
594594
if (!isActive()) {
595595
ReferenceCountUtil.release(frame);
596-
} else if (readStatus != ReadStatus.IDLE) {
597-
// If a read is in progress or has been requested, there cannot be anything in the queue,
598-
// otherwise we would have drained it from the queue and processed it during the read cycle.
599-
assert inboundBuffer == null || inboundBuffer.isEmpty();
596+
} else if (readStatus != ReadStatus.IDLE && isInboundBufferEmpty()) {
597+
// A read is in progress or has been requested and nothing is queued, so dispatch directly. If frames are
598+
// still queued (e.g. this frame was delivered re-entrantly while doBeginRead() is running pipeline
599+
// callbacks after it stopped early) we must queue behind them below to preserve ordering; the read loop
600+
// that is still on the stack will drain them.
600601
final RecvByteBufAllocator.Handle allocHandle = unsafe.recvBufAllocHandle();
601602

602603
unsafe.doRead0(frame, allocHandle);
@@ -617,6 +618,10 @@ void fireChildRead(Http2Frame frame) {
617618
}
618619
}
619620

621+
private boolean isInboundBufferEmpty() {
622+
return inboundBuffer == null || inboundBuffer.isEmpty();
623+
}
624+
620625
void fireChildReadComplete() {
621626
assert executor().inEventLoop();
622627
assert readStatus != ReadStatus.IDLE || !readCompletePending;
@@ -812,19 +817,39 @@ public void read() {
812817
if (!isActive()) {
813818
return;
814819
}
815-
updateLocalWindowIfNeeded();
820+
final boolean windowUpdated = updateLocalWindowIfNeeded();
816821

817822
switch (readStatus) {
818823
case IDLE:
819824
readStatus = ReadStatus.IN_PROGRESS;
825+
// doBeginRead() always flushes (or defers to the parent's read-complete flush).
820826
doBeginRead();
821-
break;
827+
return;
822828
case IN_PROGRESS:
823829
readStatus = ReadStatus.REQUESTED;
824830
break;
825831
default:
826832
break;
827833
}
834+
if (windowUpdated) {
835+
// A WINDOW_UPDATE was just written above, but no read loop runs in this branch to flush it, so an
836+
// unflushed frame here can stall the remote peer forever (netty/netty#17600).
837+
//
838+
// flush() alone is not enough: it no-ops while isParentReadInProgress(), since
839+
// Http2MultiplexHandler batches all children's flushes into one ctx.flush() at the end of its read
840+
// loop (processPendingReadCompleteQueue()) -- but that batched flush only runs if some channel
841+
// registered via maybeAddChannelToReadCompletePendingQueue(), which doBeginRead()/fireChildRead()
842+
// do but this branch doesn't. If a cross-thread setAutoRead(true) lands here as the only activity
843+
// in the parent's read loop, the queue stays empty, the end-of-loop flush is skipped, and this
844+
// WINDOW_UPDATE (flowControlledBytes already zeroed) is never retried -- permanent stall.
845+
//
846+
// So register into that queue while the parent is reading; otherwise flush immediately.
847+
if (isParentReadInProgress()) {
848+
maybeAddChannelToReadCompletePendingQueue();
849+
} else {
850+
flush();
851+
}
852+
}
828853
}
829854

830855
private Object pollQueuedMessage() {
@@ -834,20 +859,23 @@ private Object pollQueuedMessage() {
834859
void doBeginRead() {
835860
if (readStatus == ReadStatus.IDLE) {
836861
// Don't wait for the user to request a read to notify of channel closure.
837-
if (readEOS && (inboundBuffer == null || inboundBuffer.isEmpty())) {
862+
if (readEOS && isInboundBufferEmpty()) {
838863
// Double check there is nothing left to flush such as a window update frame.
839864
flush();
840-
unsafe.close(CompletionHandler.ignore());
865+
closeIfReadEOSAndDrained();
841866
}
842867
} else {
843868
do { // Process messages until there are none left (or the user stopped requesting) and also handle EOS.
844869
Object message = pollQueuedMessage();
845870
if (message == null) {
846871
// Double check there is nothing left to flush such as a window update frame.
847872
flush();
848-
if (readEOS) {
849-
unsafe.close(CompletionHandler.ignore());
873+
if (!isInboundBufferEmpty()) {
874+
// The flush re-entrantly delivered more frames which got queued; keep reading if we
875+
// still should.
876+
continue;
850877
}
878+
closeIfReadEOSAndDrained();
851879
break;
852880
}
853881
final RecvByteBufAllocator.Handle allocHandle = recvBufAllocHandle();
@@ -919,7 +947,14 @@ void notifyReadComplete(RecvByteBufAllocator.Handle allocHandle, boolean forceRe
919947
// channel is not currently reading we need to force a flush at the child channel, because we cannot
920948
// rely upon flush occurring in channelReadComplete on the parent channel.
921949
flush();
922-
if (readEOS) {
950+
closeIfReadEOSAndDrained();
951+
}
952+
953+
private void closeIfReadEOSAndDrained() {
954+
// Pipeline callbacks and flush() can re-entrantly deliver more frames, including the one that ends the
955+
// stream, and queue them if the user stopped reading. Only close once all of them have been read,
956+
// otherwise the queued frames would be released and the stream silently truncated.
957+
if (readEOS && isInboundBufferEmpty()) {
923958
unsafe.close(CompletionHandler.ignore());
924959
}
925960
}
Collapse file

‎codec-http2/src/test/java/io/netty/handler/codec/http2/Http2MultiplexTest.java‎

Copy file name to clipboardExpand all lines: codec-http2/src/test/java/io/netty/handler/codec/http2/Http2MultiplexTest.java
+132Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,6 +1677,138 @@ public void windowUpdatesSendWhenAutoReadEnabled() {
16771677
assertTrue(flushSniffer.checkFlush());
16781678
}
16791679

1680+
@Test
1681+
public void windowUpdateFlushedWhenReadRequestedWhileReadInProgress() {
1682+
LastInboundHandler inboundHandler = new LastInboundHandler();
1683+
FlushSniffer flushSniffer = new FlushSniffer();
1684+
parentChannel.pipeline().addFirst(flushSniffer);
1685+
1686+
Http2StreamChannel childChannel = newInboundStream(3, false, inboundHandler);
1687+
assertTrue(childChannel.config().isAutoRead());
1688+
Http2HeadersFrame headersFrame = inboundHandler.readInbound();
1689+
assertNotNull(headersFrame);
1690+
1691+
// Each round: a read() is requested while the child is waiting for data (the same thing a
1692+
// setAutoRead(false) -> setAutoRead(true) flip does), auto-read is disabled again, and then a DATA frame
1693+
// arrives. The frame is still delivered because a read was pending, but its bytes are not returned yet.
1694+
for (int i = 0; i < 2; i++) {
1695+
childChannel.config().setAutoRead(false);
1696+
childChannel.config().setAutoRead(true);
1697+
childChannel.config().setAutoRead(false);
1698+
frameInboundWriter.writeInboundData(childChannel.stream().id(), bb(16 * 1024), 0, false);
1699+
verifyFramesMultiplexedToCorrectChannel(childChannel, inboundHandler, 1);
1700+
}
1701+
verify(frameWriter, never()).writeWindowUpdate(eqCodecCtx(), anyInt(), anyInt(), anyChannelPromise());
1702+
flushSniffer.checkFlush();
1703+
1704+
// Re-enabling auto-read triggers a read() which returns the consumed bytes to the flow controller. That
1705+
// results in a wire-level WINDOW_UPDATE which must be flushed, otherwise the remote peer may stall forever
1706+
// once its send window is exhausted.
1707+
childChannel.config().setAutoRead(true);
1708+
verify(frameWriter).writeWindowUpdate(eqCodecCtx(), eq(0), eq(32 * 1024), anyChannelPromise());
1709+
verify(frameWriter).writeWindowUpdate(
1710+
eqCodecCtx(), eq(childChannel.stream().id()), eq(32 * 1024), anyChannelPromise());
1711+
assertTrue(flushSniffer.checkFlush(), "WINDOW_UPDATE was written but never flushed");
1712+
}
1713+
1714+
@Test
1715+
public void framesDeliveredReentrantlyWhileFramesQueuedAreNotReordered() {
1716+
final LastInboundHandler inboundHandler = new LastInboundHandler();
1717+
final Http2StreamChannel childChannel = newInboundStream(3, false, inboundHandler);
1718+
assertNotNull(inboundHandler.readInbound());
1719+
final int streamId = childChannel.stream().id();
1720+
1721+
// Queue two frames while auto-read is disabled. The first is dispatched immediately as a read was pending.
1722+
childChannel.config().setAutoRead(false);
1723+
frameInboundWriter.writeInboundData(streamId, bb(1), 0, false);
1724+
assertDataFrame(inboundHandler, 1, false);
1725+
frameInboundWriter.writeInboundData(streamId, bb(2), 0, false);
1726+
frameInboundWriter.writeInboundData(streamId, bb(3), 0, false);
1727+
assertNull(inboundHandler.readInbound());
1728+
1729+
// Stop reading again after the first queued frame, and have a new frame arrive re-entrantly (e.g. because a
1730+
// flush synchronously triggered the remote peer to send more) while the read loop is still completing.
1731+
final AtomicBoolean armed = new AtomicBoolean(true);
1732+
childChannel.pipeline().addFirst(new ChannelInboundHandler() {
1733+
@Override
1734+
public void channelRead(ChannelHandlerContext ctx, Object msg) {
1735+
ctx.channel().config().setAutoRead(false);
1736+
ctx.fireChannelRead(msg);
1737+
}
1738+
1739+
@Override
1740+
public void channelReadComplete(ChannelHandlerContext ctx) {
1741+
if (armed.getAndSet(false)) {
1742+
frameInboundWriter.writeInboundData(streamId, bb(4), 0, false);
1743+
}
1744+
ctx.fireChannelReadComplete();
1745+
}
1746+
});
1747+
childChannel.config().setAutoRead(true);
1748+
assertDataFrame(inboundHandler, 2, false);
1749+
assertNull(inboundHandler.readInbound());
1750+
1751+
// The re-entrant frame must be delivered after the frame that was already queued.
1752+
childChannel.read();
1753+
assertDataFrame(inboundHandler, 3, false);
1754+
childChannel.read();
1755+
assertDataFrame(inboundHandler, 4, false);
1756+
assertNull(inboundHandler.readInbound());
1757+
}
1758+
1759+
@Test
1760+
public void endOfStreamDeliveredReentrantlyDoesNotDropQueuedFrames() {
1761+
final LastInboundHandler inboundHandler = new LastInboundHandler();
1762+
final Http2StreamChannel childChannel = newInboundStream(3, false, inboundHandler);
1763+
assertNotNull(inboundHandler.readInbound());
1764+
final int streamId = childChannel.stream().id();
1765+
// Half-close our side so that receiving END_STREAM fully closes the stream.
1766+
assertTrue(childChannel.writeAndFlush(
1767+
new DefaultHttp2HeadersFrame(new DefaultHttp2Headers(), true)).isSuccess());
1768+
1769+
childChannel.config().setAutoRead(false);
1770+
frameInboundWriter.writeInboundData(streamId, bb(1), 0, false);
1771+
assertDataFrame(inboundHandler, 1, false);
1772+
frameInboundWriter.writeInboundData(streamId, bb(2), 0, false);
1773+
assertNull(inboundHandler.readInbound());
1774+
1775+
// While the read loop completes, stop reading and have the rest of the stream (including END_STREAM)
1776+
// arrive re-entrantly. The last frame gets queued and the stream is closed at the HTTP/2 level.
1777+
final AtomicBoolean armed = new AtomicBoolean(true);
1778+
childChannel.pipeline().addFirst(new ChannelInboundHandler() {
1779+
@Override
1780+
public void channelReadComplete(ChannelHandlerContext ctx) {
1781+
if (armed.getAndSet(false)) {
1782+
ctx.channel().config().setAutoRead(false);
1783+
frameInboundWriter.writeInboundData(streamId, bb(3), 0, false);
1784+
frameInboundWriter.writeInboundData(streamId, bb(4), 0, true);
1785+
}
1786+
ctx.fireChannelReadComplete();
1787+
}
1788+
});
1789+
childChannel.config().setAutoRead(true);
1790+
assertDataFrame(inboundHandler, 2, false);
1791+
assertDataFrame(inboundHandler, 3, false);
1792+
assertNull(inboundHandler.readInbound());
1793+
1794+
// The queued END_STREAM frame must not be dropped by closing the channel early.
1795+
assertTrue(childChannel.isActive());
1796+
childChannel.read();
1797+
assertDataFrame(inboundHandler, 4, true);
1798+
assertFalse(childChannel.isActive());
1799+
}
1800+
1801+
private static void assertDataFrame(LastInboundHandler inboundHandler, int expectedBytes, boolean endStream) {
1802+
Http2DataFrame frame = inboundHandler.readInbound();
1803+
assertNotNull(frame, "expected a DATA frame with " + expectedBytes + " bytes");
1804+
try {
1805+
assertEquals(expectedBytes, frame.content().readableBytes());
1806+
assertEquals(endStream, frame.isEndStream());
1807+
} finally {
1808+
frame.release();
1809+
}
1810+
}
1811+
16801812
@ParameterizedTest(name = "{displayName} [{index}] value={0}")
16811813
@MethodSource("userEvents")
16821814
public void userEventsThatPropagatedToChildChannels(Object userEvent) {

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.