GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||
GeezerVison is showing github.com — FlowControlHandler: preserve reads requested during a read cycle by Asthenia0412 · Pull Request #17647 · netty/netty Conversation
Contributor
MotivationWhen auto-read is disabled, a downstream handler can request the next read re-entrantly while processing a message. ModificationKeep reads requested after the last delivered message outstanding for the next upstream read cycle. Add an ResultBoth regression tests reproduce the stall before the fix and pass with the updated read-cycle accounting. Fixes #17628 Tests
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for picking this up. I ran the change against our reproducers for #17628 and #17050. Verified: it fixes the reported case.
Still stalls: a read cycle that delivers no message.
This happens routinely:
Measured with this PR:
These two tests fail with this PR (and on 4.1). They could go into @Test public void testReadSurvivesUpstreamCycleWithoutMessage() throws Exception { final List<String> received = new ArrayList<String>(); EmbeddedChannel channel = new EmbeddedChannel(false, false, new FlowControlHandler(), new ChannelInboundHandlerAdapter() { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { received.add((String) msg); } }); channel.config().setAutoRead(false); channel.register(); channel.read(); channel.flushInbound(); // upstream read cycle that produced nothing (e.g. a partial frame) channel.writeInbound("msg"); // the transport keeps reading: the read() is still outstanding assertEquals(Arrays.asList("msg"), received); // was: [] assertFalse(channel.finishAndReleaseAll()); } @Test public void testReadInChannelReadSurvivesFrameSplitAcrossReads() throws Exception { final List<String> received = new ArrayList<String>(); EmbeddedChannel channel = new EmbeddedChannel(false, false, new FixedLengthFrameDecoder(4), new FlowControlHandler(), new ChannelInboundHandlerAdapter() { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { received.add(((ByteBuf) msg).toString(CharsetUtil.US_ASCII)); ReferenceCountUtil.release(msg); ctx.read(); } }); channel.config().setAutoRead(false); channel.register(); channel.read(); channel.writeInbound(Unpooled.copiedBuffer("m1..", CharsetUtil.US_ASCII)); channel.writeInbound(Unpooled.copiedBuffer("m2", CharsetUtil.US_ASCII)); // half a frame, no message channel.writeInbound(Unpooled.copiedBuffer("..", CharsetUtil.US_ASCII)); assertEquals(Arrays.asList("m1..", "m2.."), received); // was: [m1..] assertFalse(channel.finishAndReleaseAll()); } A smaller change that covers both cases. Keep the 4.1 code as it is and delete only the reset in
Only one existing assertion changes: the last block of The trade-off is that outstanding reads now survive empty cycles, so N Minor. writeOneInbound("a"); writeOneInbound("b"); read(); flushInbound(); writeInbound("c") It needs a About #17046. That PR targets 4.2. It re-issues
So neither PR supersedes the other. This one is closer to mergeable once the empty-cycle case is handled. Claude Code on behalf of allthingssecurity
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactionsUh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page.
Asthenia0412
force-pushed
the
codex/fix-17628-flow-control-relay-stall
branch
from
7f2c328 to
f99f811
Compare
|
There was an error while loading. Please reload this page.
🚨 Jobs failed without executing tests 🚨Failed Jobs without Test Failures❌ Build PR / verify-pr 🏷️ Commit: f99f811 Rerun ControlsClick the checkbox to trigger a rerun:
Learn more about TestLens at testlens.app/docs. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Hi @Asthenia0412, thanks for this fix. #17628 is blocking a Netty upgrade for us, so I'd like to help get this merged. Here is a follow-up patch on top of the PR head (
Verified locally on JDK 21:
For the maintainers: outstanding reads now survive empty upstream cycles, so N Apply with Patchdiff --git a/codec-http/src/test/java/io/netty/handler/codec/http/HttpServerCodecFlowControlTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/HttpServerCodecFlowControlTest.java index 6859dcf407..0e872fc431 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/HttpServerCodecFlowControlTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/HttpServerCodecFlowControlTest.java @@ -20,6 +20,7 @@ import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.ByteBuf; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInitializer; @@ -30,13 +31,13 @@ import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.flow.FlowControlHandler; +import io.netty.util.CharsetUtil; import io.netty.util.ReferenceCountUtil; import org.junit.jupiter.api.Test; import java.io.OutputStream; import java.net.InetSocketAddress; import java.net.Socket; -import java.nio.charset.StandardCharsets; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; @@ -82,7 +83,7 @@ class HttpServerCodecFlowControlTest { private Channel outbound; @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) { + public void channelRead(final ChannelHandlerContext ctx, Object msg) { if (msg instanceof HttpRequest) { ctx.channel().config().setAutoRead(false); Bootstrap bootstrap = new Bootstrap().group(ctx.channel().eventLoop()) @@ -94,9 +95,12 @@ class HttpServerCodecFlowControlTest { }); ChannelFuture connect = bootstrap.connect("127.0.0.1", targetPort); outbound = connect.channel(); - connect.addListener(future -> { - if (future.isSuccess()) { - ctx.channel().read(); + connect.addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) { + if (future.isSuccess()) { + ctx.channel().read(); + } } }); ReferenceCountUtil.release(msg); @@ -104,11 +108,15 @@ class HttpServerCodecFlowControlTest { ByteBuf body = ((HttpContent) msg).content().retain(); ReferenceCountUtil.release(msg); if (body.isReadable()) { - outbound.writeAndFlush(body).addListener(future -> { - if (future.isSuccess()) { - ctx.channel().read(); - } - }); + outbound.writeAndFlush(body).addListener( + new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) { + if (future.isSuccess()) { + ctx.channel().read(); + } + } + }); } else { body.release(); ctx.channel().read(); @@ -122,19 +130,32 @@ class HttpServerCodecFlowControlTest { }).bind("127.0.0.1", 0).sync().channel(); final int proxyPort = ((InetSocketAddress) proxy.localAddress()).getPort(); - Thread client = new Thread(() -> { - try (Socket socket = new Socket("127.0.0.1", proxyPort)) { - OutputStream out = socket.getOutputStream(); - out.write(("PUT /upload HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: " + bodySize - + "\r\n\r\n").getBytes(StandardCharsets.US_ASCII)); - byte[] chunk = new byte[16 * 1024]; - for (int written = 0; written < bodySize; written += chunk.length) { - out.write(chunk); - out.flush(); + Thread client = new Thread(new Runnable() { + @Override + public void run() { + Socket socket = null; + try { + socket = new Socket("127.0.0.1", proxyPort); + OutputStream out = socket.getOutputStream(); + out.write(("PUT /upload HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: " + bodySize + + "\r\n\r\n").getBytes(CharsetUtil.US_ASCII)); + byte[] chunk = new byte[16 * 1024]; + for (int written = 0; written < bodySize; written += chunk.length) { + out.write(chunk); + out.flush(); + } + targetReceivedBody.await(10, TimeUnit.SECONDS); + } catch (Exception ignored) { + // A blocked client write is part of the relay-stall failure mode. + } finally { + if (socket != null) { + try { + socket.close(); + } catch (Exception ignored) { + // ignore + } + } } - targetReceivedBody.await(10, TimeUnit.SECONDS); - } catch (Exception ignored) { - // A blocked client write is part of the relay-stall failure mode. } }, "flow-control-relay-client"); client.setDaemon(true); diff --git a/handler/src/main/java/io/netty/handler/flow/FlowControlHandler.java b/handler/src/main/java/io/netty/handler/flow/FlowControlHandler.java index 3ca36be7ff..fff4a0b243 100644 --- a/handler/src/main/java/io/netty/handler/flow/FlowControlHandler.java +++ b/handler/src/main/java/io/netty/handler/flow/FlowControlHandler.java @@ -76,15 +76,16 @@ public class FlowControlHandler extends ChannelDuplexHandler { /** * Number of unsatisfied downstream {@code read()} calls. A downstream {@code read()} is considered unsatisfied - * if auto-read is off and if it has not yet been paired with a {@code fireChannelRead} or - * a cumulative {@code fireChannelReadComplete}. + * if auto-read is off and if it has not yet been paired with a {@code fireChannelRead}. * <p> - * A {@code read()} can be satisfied in three ways, whichever comes first: + * A {@code read()} can be satisfied in two ways, whichever comes first: * <ul> * <li>inside the {@code read()} call itself, by {@code dequeue()}ing a message</li> * <li>in a {@code channelRead()}</li> - * <li>in a {@code channelReadComplete()}</li> * </ul> + * An upstream {@code channelReadComplete()} does not satisfy outstanding reads, as the upstream read cycle may + * not have produced any message for this handler. + * <p> * A {@code read()} can be satisfied with auto-read on. * <p> * When one or more {@code read()} calls are unsatisfied, a downstream {@code channelReadComplete} is fired @@ -96,16 +97,6 @@ public class FlowControlHandler extends ChannelDuplexHandler { */ private int unsatisfiedReads; - /** - * {@code true} after a message was received and before its upstream read cycle completes. - */ - private boolean readCycleHasMessage; - - /** - * Number of downstream reads requested after the most recently received message. - */ - private int readsAfterLastMessage; - /** * {@code true} while a {@link #dequeue(ChannelHandlerContext)} loop is on the stack. */ @@ -175,9 +166,6 @@ public class FlowControlHandler extends ChannelDuplexHandler { public void read(ChannelHandlerContext ctx) throws Exception { if (!config.isAutoRead()) { unsatisfiedReads++; - if (readCycleHasMessage) { - readsAfterLastMessage++; - } } boolean didSatisfyARead = dequeue(ctx); @@ -200,8 +188,6 @@ public class FlowControlHandler extends ChannelDuplexHandler { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - readCycleHasMessage = true; - readsAfterLastMessage = 0; if (queue == null) { queue = RecyclableArrayDeque.newInstance(); } @@ -217,13 +203,12 @@ public class FlowControlHandler extends ChannelDuplexHandler { @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { - // Upstream closed the read cycle. Preserve outstanding reads across empty cycles because a decoder may - // issue another upstream read after receiving only a partial frame. Spurious upstream completions with - // no pending read are dropped. - boolean hadUnsatisfiedReads = unsatisfiedReads > 0; - readsAfterLastMessage = 0; - readCycleHasMessage = false; - if (config.isAutoRead() || hadUnsatisfiedReads) { + // Upstream closed the read cycle. Outstanding reads are NOT reset here: the upstream read cycle may have + // produced no message for us (e.g. a decoder in front of us received only a partial frame), or the + // downstream handler may have requested another read while processing the last message of this cycle. + // In both cases the transport keeps reading and the next message must still be delivered. + // Spurious upstream completions with no pending read are dropped. + if (config.isAutoRead() || unsatisfiedReads > 0) { ctx.fireChannelReadComplete(); } } Prepared with Claude Code. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Since there's been no activity here, I've opened #18472. It is this PR squashed onto the current |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.