@@ -1689,6 +1689,138 @@ public void windowUpdatesSendWhenAutoReadEnabled() { |
1689 | 1689 | assertTrue(flushSniffer.checkFlush()); |
1690 | 1690 | } |
1691 | 1691 |
|
| 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 | + |
1692 | 1824 | @ParameterizedTest(name = "{displayName} [{index}] value={0}") |
1693 | 1825 | @MethodSource("userEvents") |
1694 | 1826 | public void userEventsThatPropagatedToChildChannels(Object userEvent) { |
|