Skip to content

Ensure AbstractCoalescingBufferQueue does not end up in inconsistent … - #14220

Merged
normanmaurer merged 3 commits into
4.1from
queue_consistent_state
Aug 13, 2024
Merged

normanmaurer merged 3 commits into
4.1from
queue_consistent_state

Conversation

@normanmaurer

Copy link
Copy Markdown
Member

…state on error

Motivation:

The code in AbstractCoalescingBufferQueue.remove(...) could lead to inconsistent internal state when for example an exception is thrown because of reference counting problems. Because of this is was possible that while the queue was empty it still showed readableBytes() > 0. This could then lead to various issues internally within netty where we depend on that the queue will eventually returns readableBytes() == 0.

Modifications:

  • Ensure we always decrement readable bytes, even in the cause of an error
  • Also correctly remove the listener if an error happens during handling the buffer.

Result:

Fixes #11959

…state on error

Motivation:

The code in AbstractCoalescingBufferQueue.remove(...) could lead to inconsistent internal state when for example an exception is thrown because of reference counting problems. Because of this is was possible that while the queue was empty it still showed readableBytes() > 0. This could then lead to various issues internally within netty where we depend on that the queue will eventually returns readableBytes() == 0.

Modifications:

- Ensure we always decrement readable bytes, even in the cause of an error
- Also correctly remove the listener if an error happens during handling the buffer.

Result:

Fixes #11959

@chrisvest chrisvest left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good catch

@normanmaurer
normanmaurer merged commit eaf788e into 4.1 Aug 13, 2024
@normanmaurer
normanmaurer deleted the queue_consistent_state branch August 13, 2024 04:11
normanmaurer added a commit that referenced this pull request Aug 13, 2024
#14220)

…state on error

Motivation:

The code in AbstractCoalescingBufferQueue.remove(...) could lead to
inconsistent internal state when for example an exception is thrown
because of reference counting problems. Because of this is was possible
that while the queue was empty it still showed readableBytes() > 0. This
could then lead to various issues internally within netty where we
depend on that the queue will eventually returns readableBytes() == 0.

Modifications:

- Ensure we always decrement readable bytes, even in the cause of an
error
- Also correctly remove the listener if an error happens during handling
the buffer.

Result:

Fixes #11959
normanmaurer added a commit that referenced this pull request Aug 13, 2024
#14220)

…state on error

Motivation:

The code in AbstractCoalescingBufferQueue.remove(...) could lead to
inconsistent internal state when for example an exception is thrown
because of reference counting problems. Because of this is was possible
that while the queue was empty it still showed readableBytes() > 0. This
could then lead to various issues internally within netty where we
depend on that the queue will eventually returns readableBytes() == 0.

Modifications:

- Ensure we always decrement readable bytes, even in the cause of an
error
- Also correctly remove the listener if an error happens during handling
the buffer.

Result:

Fixes #11959
normanmaurer added a commit that referenced this pull request Jun 25, 2026
…s, and fail stuck HTTP/2 streams instead of spinning empty DATA frames (#16947)

**Motivation:**

On a proxy doing HTTP/2 egress we OOMed when the remote flow controller
spun writing empty DATA frames (~134M) into an already-unwritable
channel. This is the OOM from #11959, still reachable on `4.2.12.Final`
despite #14220 - that fix only closed the exception-in-`remove` route
and left two gaps:

* `CoalescingBufferQueue.remove(...)` can leave `readableBytes > 0` with
an empty deque -> it decrements by a buffer's *live* readable bytes, and
its empty-queue early return asserts rather than reconciles
* Also `writeAllocatedBytes` re-writes a frame that makes no progress
without ever checking channel writability.

Full investigation and heap dump in #16946.

**Modification:**

* `AbstractCoalescingBufferQueue` - add a `reconcileReadableBytes()`
(called at the `remove(...)` early return and after the drain loop) so
an empty queue always reports 0 readable bytes.
* `DefaultHttp2RemoteFlowController.writeAllocatedBytes` - if the head
frame is given a positive budget but is neither removed nor shrinks
across two consecutive iterations, fail the stream via cancel path
instead.
* Tests cover the queue desync, the stuck-frame spin (failing the
stream), and a single no-progress pass (tolerated).

**Result:**

The queue can't report bytes it can't produce, and the flow controller
can't emit empty DATA frames unboundedly - a wedged stream fails cleanly
instead of OOMing the connection. No public API change.

Fixes #16946

---------

Co-authored-by: Norman Maurer <[email protected]>
normanmaurer added a commit that referenced this pull request Jun 26, 2026
…s when it drains, and fail stuck HTTP/2 streams instead of spinning empty DATA frames (#16997)

Auto-port of #16947 to 4.1
Cherry-picked commit: a5d4f28

---
**Motivation:**

On a proxy doing HTTP/2 egress we OOMed when the remote flow controller
spun writing empty DATA frames (~134M) into an already-unwritable
channel. This is the OOM from #11959, still reachable on `4.2.12.Final`
despite #14220 - that fix only closed the exception-in-`remove` route
and left two gaps:

* `CoalescingBufferQueue.remove(...)` can leave `readableBytes > 0` with
an empty deque -> it decrements by a buffer's *live* readable bytes, and
its empty-queue early return asserts rather than reconciles
* Also `writeAllocatedBytes` re-writes a frame that makes no progress
without ever checking channel writability.

Full investigation and heap dump in #16946.

**Modification:**

* `AbstractCoalescingBufferQueue` - add a `reconcileReadableBytes()`
(called at the `remove(...)` early return and after the drain loop) so
an empty queue always reports 0 readable bytes.
* `DefaultHttp2RemoteFlowController.writeAllocatedBytes` - if the head
frame is given a positive budget but is neither removed nor shrinks
across two consecutive iterations, fail the stream via cancel path
instead.
* Tests cover the queue desync, the stuck-frame spin (failing the
stream), and a single no-progress pass (tolerated).

**Result:**

The queue can't report bytes it can't produce, and the flow controller
can't emit empty DATA frames unboundedly - a wedged stream fails cleanly
instead of OOMing the connection. No public API change.

Fixes #16946

Co-authored-by: Gavin Bunney <[email protected]>
Co-authored-by: Norman Maurer <[email protected]>
chrisvest added a commit that referenced this pull request Jun 30, 2026
#17018)

…s, and fail stuck HTTP/2 streams instead of spinning empty DATA frames
(#16947)

**Motivation:**

On a proxy doing HTTP/2 egress we OOMed when the remote flow controller
spun writing empty DATA frames (~134M) into an already-unwritable
channel. This is the OOM from #11959, still reachable on `4.2.12.Final`
despite #14220 - that fix only closed the exception-in-`remove` route
and left two gaps:

* `CoalescingBufferQueue.remove(...)` can leave `readableBytes > 0` with
an empty deque -> it decrements by a buffer's *live* readable bytes, and
its empty-queue early return asserts rather than reconciles
* Also `writeAllocatedBytes` re-writes a frame that makes no progress
without ever checking channel writability.

Full investigation and heap dump in #16946.

**Modification:**

* `AbstractCoalescingBufferQueue` - add a `reconcileReadableBytes()`
(called at the `remove(...)` early return and after the drain loop) so
an empty queue always reports 0 readable bytes.
* `DefaultHttp2RemoteFlowController.writeAllocatedBytes` - if the head
frame is given a positive budget but is neither removed nor shrinks
across two consecutive iterations, fail the stream via cancel path
instead.
* Tests cover the queue desync, the stuck-frame spin (failing the
stream), and a single no-progress pass (tolerated).

**Result:**

The queue can't report bytes it can't produce, and the flow controller
can't emit empty DATA frames unboundedly - a wedged stream fails cleanly
instead of OOMing the connection. No public API change.

Fixes #16946

---------

Co-authored-by: Norman Maurer <[email protected]>

(cherry picked from commit a5d4f28)

Co-authored-by: Gavin Bunney <[email protected]>
lltopk pushed a commit to lltopk/netty that referenced this pull request Oct 24, 2026
…s when it drains, and fail stuck HTTP/2 streams instead of spinning empty DATA frames (netty#16997)

Auto-port of netty#16947 to 4.1
Cherry-picked commit: a5d4f28

---
**Motivation:**

On a proxy doing HTTP/2 egress we OOMed when the remote flow controller
spun writing empty DATA frames (~134M) into an already-unwritable
channel. This is the OOM from netty#11959, still reachable on `4.2.12.Final`
despite netty#14220 - that fix only closed the exception-in-`remove` route
and left two gaps:

* `CoalescingBufferQueue.remove(...)` can leave `readableBytes > 0` with
an empty deque -> it decrements by a buffer's *live* readable bytes, and
its empty-queue early return asserts rather than reconciles
* Also `writeAllocatedBytes` re-writes a frame that makes no progress
without ever checking channel writability.

Full investigation and heap dump in netty#16946.

**Modification:**

* `AbstractCoalescingBufferQueue` - add a `reconcileReadableBytes()`
(called at the `remove(...)` early return and after the drain loop) so
an empty queue always reports 0 readable bytes.
* `DefaultHttp2RemoteFlowController.writeAllocatedBytes` - if the head
frame is given a positive budget but is neither removed nor shrinks
across two consecutive iterations, fail the stream via cancel path
instead.
* Tests cover the queue desync, the stuck-frame spin (failing the
stream), and a single no-progress pass (tolerated).

**Result:**

The queue can't report bytes it can't produce, and the flow controller
can't emit empty DATA frames unboundedly - a wedged stream fails cleanly
instead of OOMing the connection. No public API change.

Fixes netty#16946

Co-authored-by: Gavin Bunney <[email protected]>
Co-authored-by: Norman Maurer <[email protected]>
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.

DefaultHttp2RemoteFlowController: writeAllocatedBytes & Endless loop

4 participants