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 — Port 5.0: SslHandler: Stop wrapping when the transport can not keep up (#18253) by chrisvest · Pull Request #18321 · netty/netty

Conversation

@chrisvest

@chrisvest chrisvest commented Nov 10, 2026

Copy link
Copy Markdown
Member

Motivation:

SslHandler queues the plaintext that is written to it and encrypts all of it in one loop when it is flushed. This loop does not stop when the transport can not keep up, so the encrypted copy of everything that was flushed is allocated at once and kept in the ChannelOutboundBuffer until it is written. When streaming to many slow peers this needs a lot of (direct) memory, see #17304.

Channel.isWritable() can not be used to stop, as the plaintext that is queued in SslHandler counts towards it as well. The channel would stay unwritable because of our own queue, and we would never continue.

Modification:

  • Track the writes of encrypted application data that are not complete yet, oldest first. Stop wrapping once more than the write buffer high water mark of the channel is pending, and continue once not more than the low water mark is pending anymore, like the writability of the channel. Only the oldest pending write gets a listener. The promise that is allocated for each record anyway knows how many bytes it contains, and small writes are tracked together, so no additional objects are allocated per record.
  • Wrap the data for which wrapping was suspended before the outbound side of the SSLEngine is closed by close(), disconnect() and closeOutbound(), as it would be failed otherwise.
  • Like flush(), don't wrap while a delegated task is running. Continue, and close the outbound side if that was requested in the meantime, once the task completed.
  • If the transport fails to write the encrypted data, fail the data that was not wrapped yet instead of encrypting it.
  • Don't suspend before the first message was written with startTls, as flush() would write the data we did not wrap unencrypted.
  • Add SslHandlerWrapBackpressureTest.
  • Add SslHandlerSlowTransportBenchmark, which writes to a transport that can only write a part of the data each time it is writable, like a socket whose send buffer is full. It reports the most encrypted data that was waiting to be written at the same time.

Result:

SslHandler encrypts at most about the write buffer high water mark ahead of what the transport has written.

The bound does not apply when the channel is closed or closeOutbound() is called while data is held back: everything that is queued is encrypted at once before the close_notify, as the SSLEngine would not encrypt it anymore once its outbound side is closed. This is the same as before this change.

This changes the behaviour of SslHandler in some cases:

  • flush() may return while plaintext is still queued in SslHandler. It is written as the transport makes progress, without another flush().
  • On a fast transport a large flush is written in batches of about the high water mark, which means more writes.
  • A transport or handler that never completes the promise of a write stops the encrypted output once about the high water mark is pending.
  • If the channel is closed while data that was held back is still queued, data that was written after the last flush() is written before the close_notify as well.
  • If SslHandler is removed while wrapping is suspended, the data that was not wrapped yet is failed.
  • If the peer sends close_notify while wrapping is suspended, the data that was not wrapped yet is failed, as the outbound side of the SSLEngine is closed then as well (the OpenSSL SSLEngine, and the JDK one for TLSv1.2 and before). Before, it was already encrypted at that point and so still written.
  • If unwrapping fails while the SSLEngine can still wrap, everything that is queued is wrapped at once before the channel is closed, like when the channel is closed while wrapping is suspended. This is the same as before.

Port notes for 5.0:

  • ChannelPromise / ChannelFuture / ChannelFutureListener are replaced by Promise / Future / FutureListener, and EncryptedWritePromise extends DefaultPromise.
  • The water marks are read through ChannelConfig.getWriteBufferWaterMark().high() / low().
  • In the test, handlers can not have their own EventExecutorGroup in 5.0, so testWrapIsSuspendedWhenSslHandlerUsesOtherExecutors only runs the delegated tasks on another executor. The transport is closed from the first handler of the pipeline instead of through Channel.unsafe(), and the plaintext queued in SslHandler is read with SslHandler.pendingOutboundBytes(...).

(cherry picked from commit abe6408)

Sorry, something went wrong.

Uh oh!

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

All reactions
@chrisvest @violetagg
SslHandler: Stop wrapping when the transport can not keep up (netty#1…
41bd1b7
…8253)

Motivation:

SslHandler queues the plaintext that is written to it and encrypts all
of it in one loop when it is flushed. This loop does not stop when the
transport can not keep up, so the encrypted copy of everything that was
flushed is allocated at once and kept in the ChannelOutboundBuffer until
it is written. When streaming to many slow peers this needs a lot of
(direct) memory, see netty#17304.

Channel.isWritable() can not be used to stop, as the plaintext that is
queued in SslHandler counts towards it as well. The channel would stay
unwritable because of our own queue, and we would never continue.

Modification:

- Track the writes of encrypted application data that are not complete
  yet, oldest first. Stop wrapping once more than the write buffer high
  water mark of the channel is pending, and continue once not more than
  the low water mark is pending anymore, like the writability of the
  channel. Only the oldest pending write gets a listener. The promise
  that is allocated for each record anyway knows how many bytes it
  contains, and small writes are tracked together, so no additional
  objects are allocated per record.
- Wrap the data for which wrapping was suspended before the outbound
  side of the SSLEngine is closed by close(), disconnect() and
  closeOutbound(), as it would be failed otherwise.
- Like flush(), don't wrap while a delegated task is running. Continue,
  and close the outbound side if that was requested in the meantime,
  once the task completed.
- If the transport fails to write the encrypted data, fail the data that
  was not wrapped yet instead of encrypting it.
- Don't suspend before the first message was written with startTls, as
  flush() would write the data we did not wrap unencrypted.
- Add SslHandlerWrapBackpressureTest.
- Add SslHandlerSlowTransportBenchmark, which writes to a transport that
  can only write a part of the data each time it is writable, like a
  socket whose send buffer is full. It reports the most encrypted data
  that was waiting to be written at the same time.

Result:

SslHandler encrypts at most about the write buffer high water mark ahead
of what the transport has written.

The bound does not apply when the channel is closed or closeOutbound()
is called while data is held back: everything that is queued is
encrypted at once before the close_notify, as the SSLEngine would not
encrypt it anymore once its outbound side is closed. This is the same
as before this change.

This changes the behaviour of SslHandler in some cases:

- flush() may return while plaintext is still queued in SslHandler. It
  is written as the transport makes progress, without another flush().
- On a fast transport a large flush is written in batches of about the
  high water mark, which means more writes.
- A transport or handler that never completes the promise of a write
  stops the encrypted output once about the high water mark is pending.
- If the channel is closed while data that was held back is still
  queued, data that was written after the last flush() is written
  before the close_notify as well.
- If SslHandler is removed while wrapping is suspended, the data that
  was not wrapped yet is failed.
- If the peer sends close_notify while wrapping is suspended, the data
  that was not wrapped yet is failed, as the outbound side of the
  SSLEngine is closed then as well (the OpenSSL SSLEngine, and the JDK
  one for TLSv1.2 and before). Before, it was already encrypted at that
  point and so still written.
- If unwrapping fails while the SSLEngine can still wrap, everything
  that is queued is wrapped at once before the channel is closed, like
  when the channel is closed while wrapping is suspended. This is the
  same as before.

Port notes for 5.0:

- ChannelPromise / ChannelFuture / ChannelFutureListener are replaced by
  Promise<Void> / Future<Void> / FutureListener<Void>, and
  EncryptedWritePromise extends DefaultPromise<Void>.
- The water marks are read through
  ChannelConfig.getWriteBufferWaterMark().high() / low().
- In the test, handlers can not have their own EventExecutorGroup in
  5.0, so testWrapIsSuspendedWhenSslHandlerUsesOtherExecutors only runs
  the delegated tasks on another executor. The transport is closed from
  the first handler of the pipeline instead of through Channel.unsafe(),
  and the plaintext queued in SslHandler is read with
  SslHandler.pendingOutboundBytes(...).

(cherry picked from commit abe6408)

Co-authored-by: Violeta Georgieva <[email protected]>
@chrisvest chrisvest added this to the 5.0.0.Final milestone Nov 10, 2026
@chrisvest chrisvest mentioned this pull request Nov 10, 2026
SslHandler: Stop wrapping when the transport can not keep up #18253
Merged
@testlens-app

testlens-app Bot commented Nov 10, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown

🚨 All tests passed but jobs failed 🚨

Failed Jobs without Test Failures

❌ Verify native library loading on musl / musl-verify-alpine-x86_64-gcompat
❌ Verify native library loading on musl / musl-verify-alpine-x86_64

🏷️ Commit: 41bd1b7
▶️ Tests: 89330 executed
⚪️ Checks: 20/20 completed

Rerun Controls

Click the checkbox to trigger a rerun:

  • Rerun jobs

Learn more about TestLens at testlens.app/docs.

All reactions

Sorry, something went wrong.

Uh oh!

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

@normanmaurer
normanmaurer merged commit 96d487a into netty:5.0 Nov 10, 2026
19 of 21 checks passed

Uh oh!

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

@chrisvest
chrisvest deleted the 5.0-port-pr-18253 branch November 10, 2026 17:07
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

5.0.0.Final

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

2 participants

@chrisvest @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.