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@ed98441

File tree

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

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

Copy file name to clipboardExpand all lines: codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2RemoteFlowController.java
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,13 @@ public void writePendingBytes() throws Http2Exception {
265265
monitor.writePendingBytes();
266266
}
267267

268+
/**
269+
* Returns {@code true} if any stream still has flow-controlled frames queued. O(1).
270+
*/
271+
boolean hasPendingBytes() {
272+
return monitor.totalPendingBytes > 0;
273+
}
274+
268275
/**
269276
* The remote flow control state for a single stream.
270277
*/
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
+30-6Lines changed: 30 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,22 @@ 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+
// Re-drive frames stranded by a mid-flush writability flip, but only while data is queued —
208+
// else an empty toggle (e.g. SslHandler preface) would re-livelock (#17256).
209+
} while (flushAgain && ctx.channel().isWritable() && hasPendingData());
198210
} catch (Http2Exception e) {
199211
onError(ctx, true, e);
200212
} catch (Throwable cause) {
@@ -204,6 +216,19 @@ public void flush(ChannelHandlerContext ctx) {
204216
}
205217
}
206218

219+
private boolean hasPendingData() {
220+
final Http2RemoteFlowController flowController = encoder.flowController();
221+
if (flowController instanceof DefaultHttp2RemoteFlowController) {
222+
return ((DefaultHttp2RemoteFlowController) flowController).hasPendingBytes();
223+
}
224+
// Custom controllers have no aggregate counter; scan for the first stream with queued frames.
225+
try {
226+
return connection().forEachActiveStream(stream -> !flowController.hasFlowControlled(stream)) != null;
227+
} catch (Http2Exception e) {
228+
return false;
229+
}
230+
}
231+
207232
private abstract class BaseDecoder {
208233
public abstract void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception;
209234
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { }
@@ -460,10 +485,9 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
460485

461486
@Override
462487
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.
488+
// Reentering the write loop here would over-allocate; flush() guards against reentry (#17256).
465489
try {
466-
if (ctx.channel().isWritable() && !inFlush) {
490+
if (ctx.channel().isWritable()) {
467491
flush(ctx);
468492
}
469493
encoder.flowController().channelWritabilityChanged();
Collapse file

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

Copy file name to clipboard
+76Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2026 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License, version 2.0 (the
5+
* "License"); you may not use this file except in compliance with the License. You may obtain a
6+
* copy of the License at:
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software distributed under the License
11+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12+
* or implied. See the License for the specific language governing permissions and limitations under
13+
* the License.
14+
*/
15+
package io.netty.handler.codec.http2;
16+
17+
import io.netty.buffer.ByteBuf;
18+
import io.netty.buffer.Unpooled;
19+
import io.netty.channel.ChannelHandlerContext;
20+
import io.netty.channel.ChannelPromise;
21+
import io.netty.channel.WriteBufferWaterMark;
22+
import io.netty.channel.embedded.EmbeddedChannel;
23+
import org.junit.jupiter.api.Test;
24+
25+
import java.net.InetSocketAddress;
26+
27+
import static org.junit.jupiter.api.Assertions.assertTrue;
28+
29+
public class Http2ConnectionHandlerFlushTest {
30+
31+
/**
32+
* A large body must fully drain even when the channel briefly becomes unwritable while it is being written
33+
* and then flips back to writable synchronously during {@code flush()}. That writable transition re-enters
34+
* {@link Http2ConnectionHandler#channelWritabilityChanged(ChannelHandlerContext)} while the flush is still in
35+
* progress; the handler must not recurse (which livelocks), but it must still write the frames that were
36+
* queued in the remote flow controller. Dropping the reentrant flush strands the tail of the body forever.
37+
*/
38+
@Test
39+
public void largeWriteDrainsFullyWhenWritabilityTogglesDuringFlush() throws Exception {
40+
// Small watermarks so writing ~1 KiB flips the channel unwritable, and EmbeddedChannel draining the
41+
// outbound buffer during flush() flips it back to writable synchronously (ChannelOutboundBuffer.remove
42+
// fires the event inline), re-entering flush().
43+
EmbeddedChannel channel = new EmbeddedChannel();
44+
channel.config().setWriteBufferWaterMark(new WriteBufferWaterMark(512, 1024));
45+
46+
Http2ConnectionHandler handler = new Http2ConnectionHandlerBuilder()
47+
.server(false)
48+
.frameListener(new Http2FrameAdapter())
49+
.validateHeaders(false)
50+
.gracefulShutdownTimeoutMillis(0)
51+
.build();
52+
53+
channel.connect(new InetSocketAddress(0));
54+
channel.pipeline().addLast(handler);
55+
channel.pipeline().fireChannelActive();
56+
// Discard the connection preface + SETTINGS the handler wrote on activation.
57+
channel.releaseOutbound();
58+
59+
ChannelHandlerContext ctx = channel.pipeline().context(handler);
60+
handler.encoder().writeHeaders(ctx, 3, new DefaultHttp2Headers(), 0, false, ctx.newPromise());
61+
62+
// Larger than the write high-watermark but within the default 65535-byte flow-control window, so only
63+
// channel writability (not flow control) gates the write.
64+
final int size = 60 * 1024;
65+
ByteBuf data = Unpooled.buffer(size).writeZero(size);
66+
ChannelPromise dataPromise = ctx.newPromise();
67+
handler.encoder().writeData(ctx, 3, data, 0, false, dataPromise);
68+
69+
handler.flush(ctx);
70+
71+
assertTrue(dataPromise.isSuccess(),
72+
"DATA write did not complete: frames were stranded after a writability toggle during flush");
73+
74+
channel.finishAndReleaseAll();
75+
}
76+
}
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
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@ public void serverShouldNotSendClientPrefaceStringWhenAddedAfterActive() throws
345345

346346
@Test
347347
public void channelWritabilityChangedShouldNotReenterFlush() throws Exception {
348+
// Models a flush whose ctx.flush() synchronously re-fires channelWritabilityChanged (as SslHandler does
349+
// during connection setup). The handler must not recurse into flush(), and with nothing queued it must
350+
// not loop either — otherwise the event loop livelocks (#17256).
348351
when(channel.isActive()).thenReturn(false);
349352
when(channel.isWritable()).thenReturn(true);
350353
handler = newHandler();

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.