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 — Auto-port 4.1: HTTP/2: Fix stream stall when toggling auto-read on a … · lltopk/netty@068e146

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
+47-12Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -603,10 +603,11 @@ void fireChildRead(Http2Frame frame) {
603603
assert eventLoop().inEventLoop();
604604
if (!isActive()) {
605605
ReferenceCountUtil.release(frame);
606-
} else if (readStatus != ReadStatus.IDLE) {
607-
// If a read is in progress or has been requested, there cannot be anything in the queue,
608-
// otherwise we would have drained it from the queue and processed it during the read cycle.
609-
assert inboundBuffer == null || inboundBuffer.isEmpty();
606+
} else if (readStatus != ReadStatus.IDLE && isInboundBufferEmpty()) {
607+
// A read is in progress or has been requested and nothing is queued, so dispatch directly. If frames are
608+
// still queued (e.g. this frame was delivered re-entrantly while doBeginRead() is running pipeline
609+
// callbacks after it stopped early) we must queue behind them below to preserve ordering; the read loop
610+
// that is still on the stack will drain them.
610611
final RecvByteBufAllocator.Handle allocHandle = unsafe.recvBufAllocHandle();
611612

612613
unsafe.doRead0(frame, allocHandle);
@@ -627,6 +628,10 @@ void fireChildRead(Http2Frame frame) {
627628
}
628629
}
629630

631+
private boolean isInboundBufferEmpty() {
632+
return inboundBuffer == null || inboundBuffer.isEmpty();
633+
}
634+
630635
void fireChildReadComplete() {
631636
assert eventLoop().inEventLoop();
632637
assert readStatus != ReadStatus.IDLE || !readCompletePending;
@@ -858,19 +863,39 @@ public void beginRead() {
858863
if (!isActive()) {
859864
return;
860865
}
861-
updateLocalWindowIfNeeded();
866+
final boolean windowUpdated = updateLocalWindowIfNeeded();
862867

863868
switch (readStatus) {
864869
case IDLE:
865870
readStatus = ReadStatus.IN_PROGRESS;
871+
// doBeginRead() always flushes (or defers to the parent's read-complete flush).
866872
doBeginRead();
867-
break;
873+
return;
868874
case IN_PROGRESS:
869875
readStatus = ReadStatus.REQUESTED;
870876
break;
871877
default:
872878
break;
873879
}
880+
if (windowUpdated) {
881+
// A WINDOW_UPDATE was just written above, but no read loop runs in this branch to flush it, so an
882+
// unflushed frame here can stall the remote peer forever (netty/netty#17600).
883+
//
884+
// flush() alone is not enough: it no-ops while isParentReadInProgress(), since
885+
// Http2MultiplexHandler batches all children's flushes into one ctx.flush() at the end of its read
886+
// loop (processPendingReadCompleteQueue()) -- but that batched flush only runs if some channel
887+
// registered via maybeAddChannelToReadCompletePendingQueue(), which doBeginRead()/fireChildRead()
888+
// do but this branch doesn't. If a cross-thread setAutoRead(true) lands here as the only activity
889+
// in the parent's read loop, the queue stays empty, the end-of-loop flush is skipped, and this
890+
// WINDOW_UPDATE (flowControlledBytes already zeroed) is never retried -- permanent stall.
891+
//
892+
// So register into that queue while the parent is reading; otherwise flush immediately.
893+
if (isParentReadInProgress()) {
894+
maybeAddChannelToReadCompletePendingQueue();
895+
} else {
896+
flush();
897+
}
898+
}
874899
}
875900

876901
private Object pollQueuedMessage() {
@@ -880,20 +905,23 @@ private Object pollQueuedMessage() {
880905
void doBeginRead() {
881906
if (readStatus == ReadStatus.IDLE) {
882907
// Don't wait for the user to request a read to notify of channel closure.
883-
if (readEOS && (inboundBuffer == null || inboundBuffer.isEmpty())) {
908+
if (readEOS && isInboundBufferEmpty()) {
884909
// Double check there is nothing left to flush such as a window update frame.
885910
flush();
886-
unsafe.closeForcibly();
911+
closeIfReadEOSAndDrained();
887912
}
888913
} else {
889914
do { // Process messages until there are none left (or the user stopped requesting) and also handle EOS.
890915
Object message = pollQueuedMessage();
891916
if (message == null) {
892917
// Double check there is nothing left to flush such as a window update frame.
893918
flush();
894-
if (readEOS) {
895-
unsafe.closeForcibly();
919+
if (!isInboundBufferEmpty()) {
920+
// The flush re-entrantly delivered more frames which got queued; keep reading if we
921+
// still should.
922+
continue;
896923
}
924+
closeIfReadEOSAndDrained();
897925
break;
898926
}
899927
final RecvByteBufAllocator.Handle allocHandle = recvBufAllocHandle();
@@ -965,8 +993,15 @@ void notifyReadComplete(RecvByteBufAllocator.Handle allocHandle, boolean forceRe
965993
// channel is not currently reading we need to force a flush at the child channel, because we cannot
966994
// rely upon flush occurring in channelReadComplete on the parent channel.
967995
flush();
968-
if (readEOS) {
969-
unsafe.closeForcibly();
996+
closeIfReadEOSAndDrained();
997+
}
998+
999+
private void closeIfReadEOSAndDrained() {
1000+
// Pipeline callbacks and flush() can re-entrantly deliver more frames, including the one that ends the
1001+
// stream, and queue them if the user stopped reading. Only close once all of them have been read,
1002+
// otherwise the queued frames would be released and the stream silently truncated.
1003+
if (readEOS && isInboundBufferEmpty()) {
1004+
closeForcibly();
9701005
}
9711006
}
9721007

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
@@ -1689,6 +1689,138 @@ public void windowUpdatesSendWhenAutoReadEnabled() {
16891689
assertTrue(flushSniffer.checkFlush());
16901690
}
16911691

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