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 — HTTP/2: Drain queued frames stranded by a writability change during f… · bryce-anderson/netty@7b05104

File tree

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

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

Copy file name to clipboardExpand all lines: codec-http2/src/main/java/io/netty/handler/codec/http2/Http2ConnectionHandler.java
+31-6Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public class Http2ConnectionHandler extends ByteToMessageDecoder implements Http
8080
private BaseDecoder byteDecoder;
8181
private long gracefulShutdownTimeoutMillis;
8282
private boolean inFlush;
83+
private boolean flushAgain;
8384

8485
protected Http2ConnectionHandler(Http2ConnectionDecoder decoder, Http2ConnectionEncoder encoder,
8586
Http2Settings initialSettings) {
@@ -190,11 +191,23 @@ public void onHttpServerUpgrade(Http2Settings settings) throws Http2Exception {
190191

191192
@Override
192193
public void flush(ChannelHandlerContext ctx) {
194+
if (inFlush) {
195+
// Reentrant flush (e.g. from a synchronous writability change) must not recurse — that livelocks
196+
// (#17256); the in-progress flush loop picks it up.
197+
flushAgain = true;
198+
return;
199+
}
193200
inFlush = true;
194201
try {
195-
// Trigger pending writes in the remote flow controller.
196-
encoder.flowController().writePendingBytes();
197-
ctx.flush();
202+
do {
203+
flushAgain = false;
204+
// Trigger pending writes in the remote flow controller.
205+
encoder.flowController().writePendingBytes();
206+
ctx.flush();
207+
// Honor any flush re-requested while we were flushing (a resumed write, or a direct flush from
208+
// elsewhere in the pipeline) regardless of writability — flushing already-written data doesn't
209+
// need a writable channel.
210+
} while (flushAgain);
198211
} catch (Http2Exception e) {
199212
onError(ctx, true, e);
200213
} catch (Throwable cause) {
@@ -204,6 +217,17 @@ public void flush(ChannelHandlerContext ctx) {
204217
}
205218
}
206219

220+
private boolean hasPendingData() {
221+
final Http2RemoteFlowController flowController = encoder.flowController();
222+
try {
223+
// Stop at the first stream that still has a flow-controlled frame queued. Frame-based, so it counts
224+
// zero-length frames (e.g. trailing headers) that carry no flow-control bytes.
225+
return connection().forEachActiveStream(stream -> !flowController.hasFlowControlled(stream)) != null;
226+
} catch (Http2Exception e) {
227+
return false;
228+
}
229+
}
230+
207231
private abstract class BaseDecoder {
208232
public abstract void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception;
209233
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { }
@@ -460,10 +484,11 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
460484

461485
@Override
462486
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
463-
// Writability is expected to change while we are writing. We cannot allow this event to trigger reentering
464-
// the allocation and write loop. Reentering the event loop will lead to over or illegal allocation.
465487
try {
466-
if (ctx.channel().isWritable() && !inFlush) {
488+
// Only flush on a writability change if the flow controller has frames queued. A toggle with
489+
// nothing to write (e.g. SslHandler during setup) must not flush, or the flush -> writability ->
490+
// flush cycle livelocks (#17256).
491+
if (ctx.channel().isWritable() && hasPendingData()) {
467492
flush(ctx);
468493
}
469494
encoder.flowController().channelWritabilityChanged();
Collapse file

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

Copy file name to clipboardExpand all lines: codec-http2/src/test/java/io/netty/handler/codec/http2/Http2ConnectionHandlerTest.java
+45Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import io.netty.channel.ChannelPromise;
2727
import io.netty.channel.DefaultChannelConfig;
2828
import io.netty.channel.DefaultChannelPromise;
29+
import io.netty.channel.WriteBufferWaterMark;
30+
import io.netty.channel.embedded.EmbeddedChannel;
2931
import io.netty.handler.codec.http.HttpResponseStatus;
3032
import io.netty.handler.codec.http2.Http2CodecUtil.SimpleChannelPromiseAggregator;
3133
import io.netty.handler.codec.http2.Http2Exception.ShutdownHint;
@@ -47,6 +49,7 @@
4749
import org.mockito.invocation.InvocationOnMock;
4850
import org.mockito.stubbing.Answer;
4951

52+
import java.net.InetSocketAddress;
5053
import java.util.List;
5154
import java.util.concurrent.CountDownLatch;
5255
import java.util.concurrent.TimeUnit;
@@ -363,6 +366,48 @@ public Void answer(InvocationOnMock invocation) throws Throwable {
363366
verify(remoteFlow).channelWritabilityChanged();
364367
}
365368

369+
/**
370+
* A large body must fully drain even when the channel briefly becomes unwritable while it is being written
371+
* and then flips back to writable synchronously during {@code flush()}. That writable transition re-enters
372+
* {@link Http2ConnectionHandler#channelWritabilityChanged(ChannelHandlerContext)} while the flush is still in
373+
* progress; the handler must not recurse (which livelocks), but it must still write the frames that were
374+
* queued in the remote flow controller. Dropping the reentrant flush strands the tail of the body forever.
375+
*/
376+
@Test
377+
public void largeWriteDrainsFullyWhenWritabilityTogglesDuringFlush() throws Exception {
378+
// Small watermarks so writing ~1 KiB flips the channel unwritable, and EmbeddedChannel draining the
379+
// outbound buffer during flush() flips it back to writable synchronously (ChannelOutboundBuffer.remove
380+
// fires the event inline), re-entering flush().
381+
EmbeddedChannel channel = new EmbeddedChannel();
382+
channel.config().setWriteBufferWaterMark(new WriteBufferWaterMark(512, 1024));
383+
384+
Http2ConnectionHandler handler = new Http2ConnectionHandlerBuilder()
385+
.server(false)
386+
.frameListener(new Http2FrameAdapter())
387+
.validateHeaders(false)
388+
.gracefulShutdownTimeoutMillis(0)
389+
.build();
390+
391+
channel.connect(new InetSocketAddress(0));
392+
channel.pipeline().addLast(handler);
393+
channel.pipeline().fireChannelActive();
394+
// Discard the connection preface + SETTINGS the handler wrote on activation.
395+
channel.releaseOutbound();
396+
397+
ChannelHandlerContext ctx = channel.pipeline().context(handler);
398+
handler.encoder().writeHeaders(ctx, 3, new DefaultHttp2Headers(), 0, false, ctx.newPromise());
399+
400+
// Larger than the write high-watermark but within the default 65535-byte flow-control window, so only
401+
// channel writability (not flow control) gates the write.
402+
final int size = 60 * 1024;
403+
ByteBuf data = Unpooled.buffer(size).writeZero(size);
404+
ChannelPromise dataPromise = ctx.newPromise();
405+
handler.encoder().writeData(ctx, 3, data, 0, false, dataPromise);
406+
handler.flush(ctx);
407+
assertTrue(dataPromise.isSuccess());
408+
channel.finishAndReleaseAll();
409+
}
410+
366411
@Test
367412
public void serverReceivingInvalidClientPrefaceStringShouldHandleException() throws Exception {
368413
when(connection.isServer()).thenReturn(true);

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.