Skip to content

Fix potential CoalescingBufferQueue desync by tracking each body reader index separately - #2146

Merged
gavinbunney merged 1 commit into
masterfrom
gavin/retry-refcnt
Jun 15, 2026
Merged

gavinbunney merged 1 commit into
masterfrom
gavin/retry-refcnt

Conversation

@gavinbunney

@gavinbunney gavinbunney commented Jun 13, 2026 •

Copy link
Copy Markdown
Collaborator

When an origin request is retried, ProxyEndpoint.writeBufferedBodyContent replays the same buffered ByteBuf chunks to each attempt with chunk.retain(). A plain retain() shares a single reader index across every attempt, so if one attempt drains the body it advances the reader index another still-in-flight attempt's netty CoalescingBufferQueue is pointing at.

That queue can then be left reporting readableBytes() > 0 with nothing left to produce, and DefaultHttp2RemoteFlowController.writeAllocatedBytes spins emitting empty 9-byte DATA frames (frame.size() never reaches 0) into the channel outbound buffer until the heap is exhausted - netty #11959. We saw exactly this signature in a production heap dump: ~134M empty frames queued on a single channel.

This switches the buffered-body replay to use chunk.retainedDuplicate(), giving each attempt its own reader/writer index over the same memory (shared refCnt), so one attempt draining its copy can no longer move another attempt's index.

Added a new CoalescingBufferQueueReaderIndexDesyncTest to verify the mechanism against the real netty classes: the shared-index drain desyncs a real CoalescingBufferQueue, and a frame stuck in that state spins a real DefaultHttp2RemoteFlowController out to 200k+ empty frames - run on a separate-thread timeout with a frame cap so the spin is observable without hanging the suite.

Raised netty/netty/issues/16946 and fix in netty/netty/pull/16947 to help prevent unbounded frames.

@AlexanderEllis AlexanderEllis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super subtle — great catch and fix!

}

@Test
void bufferedBodyReplayGivesEachOriginAttemptIndependentReaderIndex() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good tests!

@gavinbunney
gavinbunney merged commit 7aaaf96 into master Jun 15, 2026
4 checks passed
@gavinbunney
gavinbunney deleted the gavin/retry-refcnt branch June 15, 2026 20:42
gavinbunney added a commit that referenced this pull request Jul 7, 2026
https://netty.io/news/2026/07/06/4-2-16-Final.html

Includes netty fix for #2146 OOM, so updated
`CoalescingBufferQueueReaderIndexDesyncTest` tests to match the new
behavior in the desynced queue mode.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants