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 — Netty HTTP/2 client livelock during close() — reentrant flush via channelWritabilityChanged, second variant not covered by #17266 · Issue #17276 · netty/netty

Netty HTTP/2 client livelock during close() — reentrant flush via channelWritabilityChanged, second variant not covered by #17266 #17276

New issue
Copy link
New issue
Copy link
Closed
Closed
Netty HTTP/2 client livelock during close() — reentrant flush via channelWritabilityChanged, second variant not covered by #17266#17276
Copy link
Milestone
4.2.18.Final

Description

@vitolibrarius
vitolibrarius
opened on Aug 12, 2026
Issue body actions

Netty HTTP/2 client livelock during close() — reentrant flush via channelWritabilityChanged, second variant not covered by #17266

Summary: after applying the fix from #17266 (which guards Http2ConnectionHandler.flush() against reentering itself while already on the stack), a second, closely related livelock remained: Http2ConnectionHandler.close()'s own cascade can synchronously trigger channelWritabilityChanged() → flush() from outside any active flush() frame, so the inFlush guard added in #17266 never fires. This permanently pins one IO event-loop thread at ~100% CPU, identical in symptom to the original #17256 report, but via close() instead of the initial pipeline-setup path.

Versions:

  • Netty: 4.2.16.Final (patched locally with HTTP/2: Prevent reentrant flush on writability change #17266's fix backported; confirmed the class is otherwise byte-identical to 4.2.18.Final-SNAPSHOT as of this testing)
  • ServiceTalk: 0.42.64
  • OS: macOS (kqueue-based Netty transport observed in traces; mechanism is generic to the epoll/kqueue Netty transports, not macOS-specific)

Confirmed stack signature (captured via jcmd <pid> Thread.print, thread servicetalk-global-io-executor-1-6, ~100% CPU for the life of the thread once triggered):

io.netty.channel.ChannelOutboundBuffer.incrementPendingOutboundBytes
io.netty.channel.DefaultChannelPipeline.incrementPendingOutboundBytes
io.netty.channel.PendingBytesTracker$DefaultChannelPipelinePendingBytesTracker.incrementPendingOutboundBytes
io.netty.channel.AbstractCoalescingBufferQueue.incrementReadableBytes
io.netty.channel.AbstractCoalescingBufferQueue.add
io.netty.channel.AbstractCoalescingBufferQueue.add
io.netty.handler.ssl.SslHandler.wrapAndFlush
io.netty.handler.ssl.SslHandler.flush
io.netty.channel.AbstractChannelHandlerContext.flush
io.netty.handler.codec.http2.Http2ConnectionHandler.flush                         <- reentered here
io.netty.handler.codec.http2.Http2ConnectionHandler.channelWritabilityChanged     <- inFlush is false here
io.netty.channel.AbstractChannelHandlerContext.fireChannelWritabilityChanged
io.servicetalk.transport.netty.internal.ConnectionObserverInitializer$ConnectionObserverHandler.channelWritabilityChanged
io.netty.channel.AbstractChannelHandlerContext.fireChannelWritabilityChanged
io.netty.channel.DefaultChannelPipeline$HeadContext.channelWritabilityChanged
io.netty.channel.DefaultChannelPipeline.fireChannelWritabilityChanged
io.netty.channel.ChannelOutboundBuffer.fireChannelWritabilityChanged
io.netty.channel.ChannelOutboundBuffer.setWritable
io.netty.channel.ChannelOutboundBuffer.decrementPendingOutboundBytes
io.netty.channel.ChannelOutboundBuffer.remove
io.netty.channel.ChannelOutboundBuffer.removeBytes
io.netty.channel.kqueue.AbstractKQueueChannel.doWriteBytes
io.netty.channel.kqueue.AbstractKQueueStreamChannel.writeBytes
io.netty.channel.kqueue.AbstractKQueueStreamChannel.doWriteSingle
io.netty.channel.kqueue.AbstractKQueueStreamChannel.doWrite
io.netty.channel.AbstractChannel$AbstractUnsafe.flush0
io.netty.channel.kqueue.AbstractKQueueChannel$AbstractKQueueUnsafe.flush0
io.netty.channel.AbstractChannel$AbstractUnsafe.flush
io.netty.channel.DefaultChannelPipeline$HeadContext.flush
io.netty.channel.AbstractChannelHandlerContext.flush
io.servicetalk.transport.netty.internal.ConnectionObserverInitializer$ConnectionObserverHandler.flush
io.netty.channel.AbstractChannelHandlerContext.flush
io.netty.handler.ssl.SslHandler.forceFlush
io.netty.handler.ssl.SslHandler.wrapAndFlush
io.netty.handler.ssl.SslHandler.flush
io.netty.handler.ssl.SslHandler.flush
io.netty.handler.ssl.SslHandler.closeOutboundAndChannel
io.netty.handler.ssl.SslHandler.close
io.netty.channel.AbstractChannelHandlerContext.close
io.netty.handler.codec.http2.Http2ConnectionHandler.close                         <- outer frame: close(), not flush()
io.netty.channel.AbstractChannelHandlerContext.close
io.netty.channel.AbstractChannelHandlerContext.lambda$close$6
io.netty.util.concurrent.AbstractEventExecutor.runTask
io.netty.util.concurrent.AbstractEventExecutor.safeExecute
io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks
io.netty.channel.SingleThreadIoEventLoop.run

Trigger point: Http2ConnectionHandler.close(ChannelHandlerContext, ChannelPromise), specifically the decoupleCloseAndGoAway branch, which does nothing but ctx.close(promise). That call cascades into SslHandler.close() → closeOutboundAndChannel(), which flushes any final outbound data (e.g. the TLS close_notify) before actually closing. That flush performs a real socket write; the write completing drops ChannelOutboundBuffer's pending-bytes count, which calls setWritable(), which fires channelWritabilityChanged() back up the pipeline — reaching Http2ConnectionHandler.channelWritabilityChanged() while Http2ConnectionHandler.close() is the active frame, not Http2ConnectionHandler.flush(). The inFlush guard added in #17266 is therefore false at that point, the guard's condition passes, and flush() is called again — which wraps and queues more bytes into the very buffer the outer doWrite() loop (further down the same call chain, still on the stack) is actively draining. Each iteration of that loop finds newly-queued data, writes it, completes, and fires channelWritabilityChanged() again — a self-sustaining cycle, bounded in call-stack depth (it's a loop inside doWrite(), not unbounded recursion) but unbounded in time: it only stops once the connection actually finishes closing, which in the observed capture did not happen — the affected thread stayed pinned at ~100% CPU indefinitely.

What's been ruled out:

  • Not the same reentrancy HTTP/2: Prevent reentrant flush on writability change #17266 already fixed — that guard specifically requires flush() to already be on the stack when channelWritabilityChanged() fires. Here the active outer frame is close().
  • Not tied to a specific auth/application layer — the mechanism is entirely within Netty core (ChannelOutboundBuffer, AbstractKQueueStreamChannel/AbstractNioByteChannel, Http2ConnectionHandler) and io.netty.handler.ssl.SslHandler; the one non-Netty frame (ConnectionObserverInitializer$ConnectionObserverHandler) is a transparent pass-through observer handler in ServiceTalk that doesn't alter control flow.
  • Reproduced deterministically in isolation with a Mockito-based unit test (below) that models AbstractKQueueStreamChannel.doWrite()'s drain loop firing sequential, non-nested channelWritabilityChanged() calls during close() — no live network needed to demonstrate the mechanism.

Proposed fix

Same technique as #17266 (a boolean reentrancy flag), applied to close()'s cascade instead of flush()'s:

--- a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2ConnectionHandler.java
+++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2ConnectionHandler.java
@@ -80,6 +80,15 @@
     private BaseDecoder byteDecoder;
     private long gracefulShutdownTimeoutMillis;
     private boolean inFlush;
+    // Guards against a second reentrancy variant on top of the one fixed for #17256: close()'s
+    // own cascade (e.g. SslHandler.closeOutboundAndChannel -> flush -> a write completing ->
+    // ChannelOutboundBuffer.setWritable -> fireChannelWritabilityChanged) can synchronously
+    // re-enter channelWritabilityChanged() from OUTSIDE any active flush() frame, so inFlush
+    // (only true while flush() itself is on the stack) never catches it. Suppressing the
+    // reentrant flush() for the duration of our own close(ctx, promise) call is safe because the
+    // decoupleCloseAndGoAway branch does nothing but forward ctx.close(promise) - there is no
+    // HTTP/2-level state left to flush once that call has been made.
+    private boolean inClose;
 
     protected Http2ConnectionHandler(Http2ConnectionDecoder decoder, Http2ConnectionEncoder encoder,
                                      Http2Settings initialSettings) {
@@ -463,7 +472,7 @@
         // Writability is expected to change while we are writing. We cannot allow this event to trigger reentering
         // the allocation and write loop. Reentering the event loop will lead to over or illegal allocation.
         try {
-            if (ctx.channel().isWritable() && !inFlush) {
+            if (ctx.channel().isWritable() && !inFlush && !inClose) {
                 flush(ctx);
             }
             encoder.flowController().channelWritabilityChanged();
@@ -502,7 +511,12 @@
     @Override
     public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
         if (decoupleCloseAndGoAway) {
-            ctx.close(promise);
+            inClose = true;
+            try {
+                ctx.close(promise);
+            } finally {
+                inClose = false;
+            }
             return;
         }
         promise = promise.unvoid();

Only the decoupleCloseAndGoAway branch is guarded above, since it's a single unconditional ctx.close(promise) call and matches the exact trigger observed. The graceful-shutdown branch below it (used when decoupleCloseAndGoAway is false) has the same theoretical exposure — it also ends up calling ctx.close(...) down the line — but I haven't reproduced or fixed that branch specifically; happy to extend the fix there too if maintainers want it covered in the same PR.

Regression test

Added to Http2ConnectionHandlerTest, alongside the existing channelWritabilityChangedShouldNotReenterFlush:

@Test
public void closeShouldNotAllowChannelWritabilityChangedToReenterFlush() throws Exception {
    when(channel.isWritable()).thenReturn(true);
    handler = new Http2ConnectionHandlerBuilder().codec(decoder, encoder)
            .decoupleCloseAndGoAway(true).build();
    handler.handlerAdded(ctx);
    clearInvocations(ctx);

    doAnswer(new Answer<ChannelFuture>() {
        @Override
        public ChannelFuture answer(InvocationOnMock invocation) throws Throwable {
            // Simulate close()'s own cascade (e.g. SslHandler flushing a close_notify)
            // completing a write and firing channelWritabilityChanged synchronously, from
            // OUTSIDE any Http2ConnectionHandler.flush() frame - exactly what
            // AbstractKQueueStreamChannel's write-drain loop does when a write it just
            // performed flips writability.
            handler.channelWritabilityChanged(ctx);
            return future;
        }
    }).when(ctx).close(any(ChannelPromise.class));

    handler.close(ctx, promise);

    verify(ctx, never()).flush();
}

(Needs import static org.mockito.Mockito.clearInvocations; added to the file's imports.)

Confirmed this test fails against the unpatched class (flush() invoked once, from PrefaceDecoder.sendPrefaceIfNeeded's unrelated path aside — the failure is specifically the reentrant call from the simulated close() cascade) and passes once the inClose guard above is applied. All 45 pre-existing tests in the file continue to pass unchanged.

Impact: identical to #17256 — once triggered, the affected IO thread is permanently lost for the life of the process (CPU-bound execution, not a blocked I/O wait, so nothing recovers it short of process termination). Any application using ServiceTalk (or any other Netty consumer) that closes HTTP/2 connections through the decoupleCloseAndGoAway path is exposed to this over time as more connections cycle through close.

Activity

  1. normanmaurer commented on Aug 13, 2026

    @normanmaurer
    normanmaurer
    on Aug 13, 2026
    Member
    More actions

    @bryce-anderson FYI...

  2. normanmaurer commented on Sep 16, 2026

    @normanmaurer
    normanmaurer
    on Sep 16, 2026
    Member
    More actions

    This was fixed by #17279 already. We are adding the regression test in #17497

  3. normanmaurer
    closed this as completedon Sep 16, 2026
  4. normanmaurer
    added this to the 4.2.18.Final milestone on Sep 16, 2026
  5. normanmaurer
    added 3 commits that reference this issue on Sep 16, 2026

    Add regression test for close() cascade reentering flush() (#17497)

    9b96442

    Auto-port 5.0: Add regression test for close() cascade reentering flu…

    9472345

    Auto-port 4.1: Add regression test for close() cascade reentering flu…

    707791d
  6. lltopk
    added a commit that references this issue on Oct 24, 2026

    Auto-port 4.1: Add regression test for close() cascade reentering flu…

    8985e55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      • 4.2.18.Final

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      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.