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… · netty/netty@5885648

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
@@ -81,6 +81,7 @@ public class Http2ConnectionHandler extends ByteToMessageDecoder implements Http
8181
private BaseDecoder byteDecoder;
8282
private long gracefulShutdownTimeoutMillis;
8383
private boolean inFlush;
84+
private boolean flushAgain;
8485

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

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

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

471495
@Override
472496
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
473-
// Writability is expected to change while we are writing. We cannot allow this event to trigger reentering
474-
// the allocation and write loop. Reentering the event loop will lead to over or illegal allocation.
475497
try {
476-
if (ctx.channel().isWritable() && !inFlush) {
498+
// Only flush on a writability change if the flow controller has frames queued. A toggle with
499+
// nothing to write (e.g. SslHandler during setup) must not flush, or the flush -> writability ->
500+
// flush cycle livelocks (#17256).
501+
if (ctx.channel().isWritable() && hasPendingData()) {
477502
flush(ctx);
478503
}
479504
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
@@ -21,6 +21,8 @@
2121
import io.netty.channel.ChannelHandlerContext;
2222
import io.netty.channel.ChannelPipeline;
2323
import io.netty.channel.DefaultChannelConfig;
24+
import io.netty.channel.WriteBufferWaterMark;
25+
import io.netty.channel.embedded.EmbeddedChannel;
2426
import io.netty.handler.codec.http.HttpResponseStatus;
2527
import io.netty.handler.codec.http2.Http2Exception.ShutdownHint;
2628
import io.netty.util.ReferenceCountUtil;
@@ -41,6 +43,7 @@
4143
import org.mockito.invocation.InvocationOnMock;
4244
import org.mockito.stubbing.Answer;
4345

46+
import java.net.InetSocketAddress;
4447
import java.util.List;
4548
import java.util.concurrent.CountDownLatch;
4649
import java.util.concurrent.TimeUnit;
@@ -333,6 +336,48 @@ public Void answer(InvocationOnMock invocation) throws Throwable {
333336
verify(remoteFlow).channelWritabilityChanged();
334337
}
335338

339+
/**
340+
* A large body must fully drain even when the channel briefly becomes unwritable while it is being written
341+
* and then flips back to writable synchronously during {@code flush()}. That writable transition re-enters
342+
* {@link Http2ConnectionHandler#channelWritabilityChanged(ChannelHandlerContext)} while the flush is still in
343+
* progress; the handler must not recurse (which livelocks), but it must still write the frames that were
344+
* queued in the remote flow controller. Dropping the reentrant flush strands the tail of the body forever.
345+
*/
346+
@Test
347+
public void largeWriteDrainsFullyWhenWritabilityTogglesDuringFlush() throws Exception {
348+
// Small watermarks so writing ~1 KiB flips the channel unwritable, and EmbeddedChannel draining the
349+
// outbound buffer during flush() flips it back to writable synchronously (ChannelOutboundBuffer.remove
350+
// fires the event inline), re-entering flush().
351+
EmbeddedChannel channel = new EmbeddedChannel();
352+
channel.config().setWriteBufferWaterMark(new WriteBufferWaterMark(512, 1024));
353+
354+
Http2ConnectionHandler handler = new Http2ConnectionHandlerBuilder()
355+
.server(false)
356+
.frameListener(new Http2FrameAdapter())
357+
.validateHeaders(false)
358+
.gracefulShutdownTimeoutMillis(0)
359+
.build();
360+
361+
channel.connect(new InetSocketAddress(0));
362+
channel.pipeline().addLast(handler);
363+
channel.pipeline().fireChannelActive();
364+
// Discard the connection preface + SETTINGS the handler wrote on activation.
365+
channel.releaseOutbound();
366+
367+
ChannelHandlerContext ctx = channel.pipeline().context(handler);
368+
handler.encoder().writeHeaders(ctx, 3, new DefaultHttp2Headers(), 0, false, ctx.newPromise());
369+
370+
// Larger than the write high-watermark but within the default 65535-byte flow-control window, so only
371+
// channel writability (not flow control) gates the write.
372+
final int size = 60 * 1024;
373+
ByteBuf data = Unpooled.buffer(size).writeZero(size);
374+
Promise<Void> dataPromise = ctx.newPromise();
375+
handler.encoder().writeData(ctx, 3, data, 0, false, dataPromise);
376+
handler.flush(ctx);
377+
assertTrue(dataPromise.isSuccess());
378+
channel.finishAndReleaseAll();
379+
}
380+
336381
@Test
337382
public void serverReceivingInvalidClientPrefaceStringShouldHandleException() throws Exception {
338383
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.