Expected behavior
A proxy that relays an HTTP request body between two channels, using FlowControlHandler together with the auto-read toggle its javadoc documents, should relay the whole body. Reads are requested one at a time, each after the previous relay write has landed on the second channel.
This works on 4.1.135 and earlier: the target receives all 1 MiB.
Actual behavior
From 4.1.136 onwards the relay stalls after a few messages. The target receives ~34 KB of the 1 MiB body and no further reads are ever issued, so the exchange hangs until something times out.
4.1.133.Final target received 1048576 of 1048576 bytes RESULT: complete
4.1.135.Final target received 1048576 of 1048576 bytes RESULT: complete
4.1.136.Final target received 34750 of 1048576 bytes RESULT: STALLED
4.1.137.Final target received 34750 of 1048576 bytes RESULT: STALLED
4.1.138.Final target received 34750 of 1048576 bytes RESULT: STALLED
Steps to reproduce
Single file, no TLS, no third-party code. Compile and run against each version with netty-common, netty-buffer, netty-transport, netty-codec, netty-codec-http, netty-handler, netty-resolver on the classpath. Exit code 0 means the whole body arrived, 1 means it stalled.
FlowControlRelayRepro.java
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.flow.FlowControlHandler;
import io.netty.util.ReferenceCountUtil;
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;
public final class FlowControlRelayRepro {
private static final int BODY_SIZE = 1 << 20;
private static final AtomicLong TARGET_BYTES = new AtomicLong();
private static final CountDownLatch TARGET_GOT_EVERYTHING = new CountDownLatch(1);
public static void main(final String[] args) throws Exception {
final EventLoopGroup group = new NioEventLoopGroup(1);
try {
final Channel target = new ServerBootstrap().group(group).channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(final SocketChannel ch) {
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
final ByteBuf buf = (ByteBuf) msg;
final long total = TARGET_BYTES.addAndGet(buf.readableBytes());
buf.release();
if (total >= BODY_SIZE) {
TARGET_GOT_EVERYTHING.countDown();
}
}
});
}
}).bind("127.0.0.1", 0).sync().channel();
final int targetPort = ((InetSocketAddress) target.localAddress()).getPort();
final Channel proxy = new ServerBootstrap().group(group).channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(final SocketChannel ch) {
ch.pipeline().addLast(new HttpServerCodec());
ch.pipeline().addLast(new FlowControlHandler());
ch.pipeline().addLast(new RelayHandler(targetPort));
}
}).bind("127.0.0.1", 0).sync().channel();
final int proxyPort = ((InetSocketAddress) proxy.localAddress()).getPort();
final Thread client = new Thread(() -> {
try (Socket socket = new Socket("127.0.0.1", proxyPort)) {
final OutputStream out = socket.getOutputStream();
out.write(("PUT /upload HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: " + BODY_SIZE
+ "\r\n\r\n").getBytes(StandardCharsets.US_ASCII));
out.flush();
final byte[] chunk = new byte[16 * 1024];
for (int written = 0; written < BODY_SIZE; written += chunk.length) {
out.write(chunk);
out.flush();
}
TARGET_GOT_EVERYTHING.await(30, TimeUnit.SECONDS);
} catch (Exception ignored) {
// The write blocks when the relay stalls; that is the symptom.
}
}, "client");
client.setDaemon(true);
client.start();
final boolean delivered = TARGET_GOT_EVERYTHING.await(30, TimeUnit.SECONDS);
System.out.println("target received " + TARGET_BYTES.get() + " of " + BODY_SIZE + " bytes");
System.out.println(delivered ? "RESULT: complete" : "RESULT: STALLED");
System.exit(delivered ? 0 : 1);
} finally {
group.shutdownGracefully(0, 1, TimeUnit.SECONDS);
}
}
private static final class RelayHandler extends ChannelInboundHandlerAdapter {
private final int targetPort;
private Channel outbound;
private RelayHandler(final int targetPort) {
this.targetPort = targetPort;
}
@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
if (msg instanceof HttpRequest) {
ctx.channel().config().setAutoRead(false);
final ChannelFuture connect = new Bootstrap().group(ctx.channel().eventLoop())
.channel(NioSocketChannel.class)
.option(ChannelOption.AUTO_READ, false)
.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(final Channel ch) {
}
}).connect("127.0.0.1", this.targetPort);
this.outbound = connect.channel();
connect.addListener(future -> {
if (future.isSuccess()) {
ctx.channel().read();
}
});
ReferenceCountUtil.release(msg);
return;
}
if (msg instanceof HttpContent) {
final ByteBuf body = ((HttpContent) msg).content().retain();
ReferenceCountUtil.release(msg);
if (body.readableBytes() == 0) {
body.release();
ctx.channel().read();
return;
}
this.outbound.writeAndFlush(body).addListener(future -> {
if (future.isSuccess()) {
ctx.channel().read();
}
});
return;
}
ReferenceCountUtil.release(msg);
}
}
}
Isolated to FlowControlHandler
Bisected with the application code held constant, then narrowed by artifact and finally by class:
| Variable |
Result |
| Netty BOM 4.1.135 |
pass |
| Netty BOM 4.1.136 |
fail |
BOM 4.1.135 with netty-transport alone forward to 4.1.136 |
pass |
BOM 4.1.135 with netty-handler alone forward to 4.1.136 |
fail |
All 4.1.136, but io.netty.handler.flow.FlowControlHandler* classes substituted from 4.1.135 |
pass |
| Same, substitution removed |
fail |
The last two are the decisive pair: same jars otherwise, only that one class swapped. Removing FlowControlHandler from the pipeline entirely at 4.1.138 also passes.
The 4.1.136 changes to that class are #16912 ("FlowControlHandler: Fix autoRead behavior") and #16983 ("respect auto-read when toggled while dequeueing").
Why this pattern
FlowControlHandler's own javadoc documents exactly this shape — setAutoRead(false) on receiving an HttpRequest, letting the handler hold the events the codec decoded from the same read:
class MyExampleHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof HttpRequest) {
ctx.channel().config().setAutoRead(false);
// The FlowControlHandler will hold any subsequent events ...
}
}
}
Two notes on what does and does not matter, both tested rather than assumed:
- Where the next
read() comes from is not the trigger. The reproducer above requests it from a write-completion listener on the second channel, which is what a proxy relaying between sockets does. A variant that instead calls ctx.channel().read() synchronously right after writeAndFlush stalls identically at 4.1.136 (34,750 of 1,048,576) and passes at 4.1.135.
- Real sockets and a multi-read body do seem to matter. An
EmbeddedChannel-based version of the same pipeline and toggle showed byte-identical behaviour on 4.1.133/135/136/138, so it does not surface this. The reproducer above therefore uses real sockets and a 1 MiB body spanning many reads.
I have not identified which specific change inside the class is responsible, only that substituting that one class resolves it.
Netty version(s)
Regression appears at 4.1.136.Final. Present in 4.1.137.Final and 4.1.138.Final. 4.1.133.Final and 4.1.135.Final are unaffected.
JVM version
OpenJDK 21.0.12
OS version
macOS (darwin 25.5.0), NIO transport