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 — Auto-port 4.1: `HttpObjectEncoder` / `DefaultHttp2FrameWriter`: fix buffer leak when a `Throwable` is thrown during header encoding by netty-project-bot · Pull Request #17178 · netty/netty

Conversation

@netty-project-bot

@netty-project-bot netty-project-bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Auto-port of #17089 to 4.1
Cherry-picked commit: 10e24f9


Motivation

Fixes #17088. Same class of bug as the SslHandler leak fixed in #17059: a header buffer is allocated, then a Throwable (typically OutOfMemoryError) is thrown before the buffer is handed off, leaving it unreleased. #6729 reported the exact symptom on HttpObjectEncoder.encodeHeaders back in 2017 but couldn't be reproduced on demand and was closed; the root cause was never fixed.

Affected paths:

  • HTTP/1 — encodeInitHttpMessage() and encodeFullHttpMessage() in HttpObjectEncoder: buf from ctx.alloc().buffer(...) leaks if encodeHeaders() throws before it is added to out.
  • HTTP/2 — writeHeadersInternal(), writePushPromise() and writeContinuationFrames() in DefaultHttp2FrameWriter: the retained fragment / frame-header buffer leaks if a later allocation throws after the fragment is sliced off the header block.

With pooled direct buffers this leaks off-heap memory the GC cannot reclaim, so repeated OOME on these paths ends in OutOfDirectMemoryError / process death.

Modification

  • HttpObjectEncoder: guard the header buffer with a success/handed-off flag and release it in a finally unless ownership was transferred. In encodeFullHttpMessage() the flag is set immediately before encodeByteBufHttpContent() so the chunked path — where buf is already added to out before encodeChunkedHttpContent() can throw — does not double-release.
  • DefaultHttp2FrameWriter: hoist fragment to method scope, null it right after ctx.write(fragment, ...), and release it in finally if non-null. writeContinuationFrames() additionally guards the reused frame-header buffer with a per-fragment flag.
  • Add tests to both modules using a tracking allocator that injects an OutOfMemoryError on a targeted allocation, asserting every tracked buffer reaches refCnt() == 0 after the failure.

Result

No buffer leak when a Throwable is thrown mid header encoding. The normal path is unchanged.

Measured with the tracking allocator (each OOME on these paths leaks exactly one header buffer, so the leak grows linearly with the number of affected requests):

path leak / request before after
HttpObjectEncoder init / full 256 B refCnt == 1 0
Http2 writeHeaders / writePushPromise 256 B refCnt == 1 0
Http2 writeContinuationFrames (large headers) 64 KiB refCnt == 1 0

At scale on the 256 B paths that is ~244 MiB leaked per 1M affected requests; on the CONTINUATION path (large headers) ~61 GiB per 1M. After the fix the leak is 0 regardless of request count.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@HwangRock @netty-project-bot
HttpObjectEncoder / DefaultHttp2FrameWriter: fix buffer leak when…
ff9e082
… a `Throwable` is thrown during header encoding (#17089)

### Motivation

Fixes #17088. Same class of bug as the `SslHandler` leak fixed in
#17059: a header buffer is allocated, then a `Throwable` (typically
`OutOfMemoryError`) is thrown before the buffer is handed off, leaving
it unreleased. #6729 reported the exact symptom on
`HttpObjectEncoder.encodeHeaders` back in 2017 but couldn't be
reproduced on demand and was closed; the root cause was never fixed.

Affected paths:
- HTTP/1 — `encodeInitHttpMessage()` and `encodeFullHttpMessage()` in
`HttpObjectEncoder`: `buf` from `ctx.alloc().buffer(...)` leaks if
`encodeHeaders()` throws before it is added to `out`.
- HTTP/2 — `writeHeadersInternal()`, `writePushPromise()` and
`writeContinuationFrames()` in `DefaultHttp2FrameWriter`: the retained
`fragment` / frame-header buffer leaks if a later allocation throws
after the fragment is sliced off the header block.

With pooled direct buffers this leaks off-heap memory the GC cannot
reclaim, so repeated OOME on these paths ends in
`OutOfDirectMemoryError` / process death.

### Modification

- `HttpObjectEncoder`: guard the header buffer with a success/handed-off
flag and release it in a `finally` unless ownership was transferred. In
`encodeFullHttpMessage()` the flag is set immediately before
`encodeByteBufHttpContent()` so the chunked path — where `buf` is
already added to `out` before `encodeChunkedHttpContent()` can throw —
does not double-release.
- `DefaultHttp2FrameWriter`: hoist `fragment` to method scope, null it
right after `ctx.write(fragment, ...)`, and release it in `finally` if
non-null. `writeContinuationFrames()` additionally guards the reused
frame-header buffer with a per-fragment flag.
- Add tests to both modules using a tracking allocator that injects an
`OutOfMemoryError` on a targeted allocation, asserting every tracked
buffer reaches `refCnt() == 0` after the failure.

### Result

No buffer leak when a `Throwable` is thrown mid header encoding. The
normal path is unchanged.

Measured with the tracking allocator (each OOME on these paths leaks
exactly one header buffer, so the leak grows linearly with the number of
affected requests):

| path | leak / request | before | after |
|---|---|---|---|
| `HttpObjectEncoder` init / full | 256 B | `refCnt == 1` | `0` |
| Http2 `writeHeaders` / `writePushPromise` | 256 B | `refCnt == 1` |
`0` |
| Http2 `writeContinuationFrames` (large headers) | 64 KiB | `refCnt ==
1` | `0` |

At scale on the 256 B paths that is ~244 MiB leaked per 1M affected
requests; on the CONTINUATION path (large headers) ~61 GiB per 1M. After
the fix the leak is `0` regardless of request count.

(cherry picked from commit 10e24f9)
@netty-project-bot netty-project-bot mentioned this pull request Jul 31, 2026
HttpObjectEncoder / DefaultHttp2FrameWriter: fix buffer leak when a Throwable is thrown during header encoding #17089
Merged
@normanmaurer
Fix compile error
858bece
@normanmaurer normanmaurer added this to the 4.1.137.Final milestone Aug 3, 2026
@normanmaurer
normanmaurer merged commit 25f7e5e into 4.1 Aug 3, 2026
31 of 33 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@normanmaurer
normanmaurer deleted the auto-port-pr-17089-to-4.1 branch August 3, 2026 07:45
@lhotari lhotari mentioned this pull request Aug 10, 2026
[fix][sec][branch-4.2] Upgrade Netty to 4.1.137 to address several CVEs and bugs apache/pulsar#26301
Merged
2 of 11 tasks
@coderabbitai coderabbitai Bot mentioned this pull request Sep 18, 2026
fix(deps): close the amqp-client credential-leak advisory left open on dev OpenResilienceInitiative/ORISO-UserService#1190
Merged
lltopk pushed a commit to lltopk/netty that referenced this pull request Oct 24, 2026
@netty-project-bot @HwangRock
@normanmaurer @lltopk
Auto-port 4.1: HttpObjectEncoder / DefaultHttp2FrameWriter: fix b…
2becb07
…uffer leak when a `Throwable` is thrown during header encoding (netty#17178)

Auto-port of netty#17089 to 4.1
Cherry-picked commit: 10e24f9

---
### Motivation

Fixes netty#17088. Same class of bug as the `SslHandler` leak fixed in
netty#17059: a header buffer is allocated, then a `Throwable` (typically
`OutOfMemoryError`) is thrown before the buffer is handed off, leaving
it unreleased. netty#6729 reported the exact symptom on
`HttpObjectEncoder.encodeHeaders` back in 2017 but couldn't be
reproduced on demand and was closed; the root cause was never fixed.

Affected paths:
- HTTP/1 — `encodeInitHttpMessage()` and `encodeFullHttpMessage()` in
`HttpObjectEncoder`: `buf` from `ctx.alloc().buffer(...)` leaks if
`encodeHeaders()` throws before it is added to `out`.
- HTTP/2 — `writeHeadersInternal()`, `writePushPromise()` and
`writeContinuationFrames()` in `DefaultHttp2FrameWriter`: the retained
`fragment` / frame-header buffer leaks if a later allocation throws
after the fragment is sliced off the header block.

With pooled direct buffers this leaks off-heap memory the GC cannot
reclaim, so repeated OOME on these paths ends in
`OutOfDirectMemoryError` / process death.

### Modification

- `HttpObjectEncoder`: guard the header buffer with a success/handed-off
flag and release it in a `finally` unless ownership was transferred. In
`encodeFullHttpMessage()` the flag is set immediately before
`encodeByteBufHttpContent()` so the chunked path — where `buf` is
already added to `out` before `encodeChunkedHttpContent()` can throw —
does not double-release.
- `DefaultHttp2FrameWriter`: hoist `fragment` to method scope, null it
right after `ctx.write(fragment, ...)`, and release it in `finally` if
non-null. `writeContinuationFrames()` additionally guards the reused
frame-header buffer with a per-fragment flag.
- Add tests to both modules using a tracking allocator that injects an
`OutOfMemoryError` on a targeted allocation, asserting every tracked
buffer reaches `refCnt() == 0` after the failure.

### Result

No buffer leak when a `Throwable` is thrown mid header encoding. The
normal path is unchanged.

Measured with the tracking allocator (each OOME on these paths leaks
exactly one header buffer, so the leak grows linearly with the number of
affected requests):

| path | leak / request | before | after |
|---|---|---|---|
| `HttpObjectEncoder` init / full | 256 B | `refCnt == 1` | `0` |
| Http2 `writeHeaders` / `writePushPromise` | 256 B | `refCnt == 1` |
`0` |
| Http2 `writeContinuationFrames` (large headers) | 64 KiB | `refCnt ==
1` | `0` |

At scale on the 256 B paths that is ~244 MiB leaked per 1M affected
requests; on the CONTINUATION path (large headers) ~61 GiB per 1M. After
the fix the leak is `0` regardless of request count.

---------

Co-authored-by: HwangRock <[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

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

4.1.137.Final

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@netty-project-bot @normanmaurer @HwangRock
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.