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 — SslHandler: bound TLS wrapping by outbound backpressure by copley · Pull Request #17391 · netty/netty

Conversation

@copley

@copley copley commented Sep 6, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown

A large amount of plaintext reaches SslHandler. SslHandler can turn it into direct TLS buffers faster than a slow transport can drain them. The fix bounds that conversion using downstream transport capacity.

Motivation

This change addresses the direct-memory increase reported in #17304.

The behavior became visible after #13711 changed ChunkedWriteHandler. Previously, ordinary writes could remain queued there until flush(). After that change, ordinary writes can pass immediately to downstream handlers.

For an HTTPS connection, this means a large amount of application data can reach SslHandler before it is flushed to the transport.

SslHandler may then:

accept the plaintext data,
encrypt it,
allocate TLS output buffers,
queue those encrypted buffers for the transport.

If the peer is slow, the transport cannot drain those TLS buffers quickly enough. With many slow connections, a large plaintext backlog can therefore be converted into a similarly large amount of pooled direct TLS memory.

The important point is that this is not a traditional memory leak. The memory is eventually released, but significantly more direct memory can be retained while slow clients are catching up.

Why Channel.isWritable() is not enough

A simple fix might appear to be:

if (!ctx.channel().isWritable()) {
stopWrapping();
}

However, that can deadlock.

The plaintext waiting inside SslHandler already contributes to the channel's pending outbound-byte count. A sufficiently large plaintext backlog can therefore make the channel unwritable before that plaintext has been encrypted.

If SslHandler then refuses to consume plaintext until the channel becomes writable again, the bytes responsible for making the channel unwritable never leave the plaintext queue.

Instead, the backpressure decision needs to distinguish between:

plaintext still owned by SslHandler, and
TLS output already queued downstream toward the transport.

This change therefore calculates the available wrapping budget from the channel write-buffer high watermark after subtracting the plaintext bytes still waiting inside SslHandler.

Modifications

This change:

Keeps unflushed plaintext separate from plaintext that has crossed an explicit flush() boundary.
Tracks the number of plaintext bytes currently owned by SslHandler.
Limits how much plaintext may be converted into TLS output based on the remaining downstream write-buffer capacity.
Stops additional TLS wrapping when that capacity is exhausted.
Automatically resumes wrapping after previously generated TLS output has drained far enough through the transport.
Preserves the existing TLS alert and control-frame behavior.
Prevents writes made after a flush from being pulled into an earlier flushed batch.
Adds SslHandlerBackpressureTest to reproduce the slow-consumer case.

The regression test specifically verifies that a single flush() does not immediately convert the entire plaintext backlog into retained TLS buffers.

Result

With this change, SslHandler no longer eagerly converts an arbitrarily large flushed plaintext backlog into TLS output while the transport is backpressured.

Instead, encryption progresses in bounded amounts:

flushed plaintext
|
v
SslHandler
|
| bounded TLS wrapping
v
TLS output
|
v
slow transport

As the transport drains TLS output, SslHandler automatically continues processing the already-flushed plaintext.

At the same time, writes made after that flush remain behind the next explicit flush boundary.

This preserves normal write/flush semantics while preventing slow peers from causing an unnecessarily large buildup of pooled TLS buffers.

Validation

The new regression test fails on unmodified Netty 4.1 at the expected bounded-TLS-output assertion and passes with this change.

Additional validation:

Focused SslHandlerBackpressureTest: pass
JDK TLS 1.2 handshake-failure compatibility: pass
JDK TLS 1.3 handshake-failure compatibility: pass
Incorrect TLS record handling: pass
SslHandlerTest: pass
SslHandlerCoalescingBufferQueueTest: pass
Static BoringSSL regression coverage: pass
Native OpenSSL/BoringSSL compatibility sentinels: pass
Complete handler module suite:
15,635 tests
0 failures
0 errors
714 skipped

The final validation was performed against netty/netty:4.1 HEAD 1df2a41.

Fixes #17304

Sorry, something went wrong.

Uh oh!

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

All reactions
@copley
SslHandler: bound TLS wrapping by outbound backpressure
021cdd4
Motivation:

After ChunkedWriteHandler stopped queueing ordinary writes before flush,
SslHandler can eagerly convert a large plaintext backlog into pooled TLS
buffers. With many slow peers this can cause a large increase in direct
memory usage even though ChannelOutboundBuffer writability accounting is
otherwise active.

Modifications:

- Preserve explicit flush boundaries between queued plaintext writes.
- Bound plaintext-to-TLS conversion using downstream pending bytes and the
  channel write-buffer high watermark.
- Resume wrapping only after previously queued TLS output drains.
- Preserve TLS alert/control-frame wrapping semantics.
- Add a regression test that reproduces the direct-memory/backpressure
  failure and verifies bounded TLS output.

Result:

SslHandler no longer converts an entire flushed plaintext backlog into TLS
output while the downstream transport is backpressured, while existing TLS
behavior remains compatible.

Relates to netty#17304
@franz1981

franz1981 commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

@yawkat I have some deja-vu...

All reactions

Sorry, something went wrong.

Uh oh!

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

@normanmaurer

normanmaurer commented Sep 7, 2026

Copy link
Copy Markdown
Member

Honestly this seems a lot more complicated then it should be... So just to clarify what we try to archive here. We basically try to only "wrap" as much as there is writable bytes left and only resume once the channel becomes writable again, correct ?

All reactions

Sorry, something went wrong.

Uh oh!

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

@copley

copley commented Sep 7, 2026

Copy link
Copy Markdown
Author

Yes, bounded wrapping is the goal. The qualification is the resume condition: SslHandler’s queued plaintext contributes to channel unwritability, so downstream TLS output can drain while the channel remains unwritable. Waiting solely for a writable transition would then stall.

That explains the separate downstream-capacity calculation, but doesn’t establish that the current queue and resume machinery is necessary. I’m revisiting that complexity and strengthening the regression coverage around this distinction before proposing a smaller implementation.

All reactions

Sorry, something went wrong.

Uh oh!

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

@normanmaurer

normanmaurer commented Sep 14, 2026

Copy link
Copy Markdown
Member

I think I have a different idea... let me open a PR later today

All reactions

Sorry, something went wrong.

Uh oh!

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

@normanmaurer

normanmaurer commented Sep 16, 2026

Copy link
Copy Markdown
Member

Sorry got side-tracked.... still working on it

copley reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@copley

copley commented Sep 17, 2026

Copy link
Copy Markdown
Author

I'm keen to help out, I have a huge AI harness set up on my system, plus access to infrastructure.

All reactions

Sorry, something went wrong.

Uh oh!

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

@normanmaurer

normanmaurer commented Sep 17, 2026

Copy link
Copy Markdown
Member

@copley honestly I am hesitant to accept a PR which is generated by AI without someone really debugging it carefully. SslHandler is a complex beast.

All reactions

Sorry, something went wrong.

Uh oh!

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

@normanmaurer

normanmaurer commented Sep 17, 2026

Copy link
Copy Markdown
Member

@copley PTAL #17525

All reactions

Sorry, something went wrong.

Uh oh!

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

@normanmaurer

normanmaurer commented Nov 3, 2026

Copy link
Copy Markdown
Member

Let me close this.. I think I have a better solution

All reactions

Sorry, something went wrong.

Uh oh!

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

@normanmaurer normanmaurer closed this Nov 3, 2026
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

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

3 participants

@copley @franz1981 @normanmaurer
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.