Skip to content

Port 5.0: SslHandler: Stop wrapping when the transport can not keep up (#18253) - #18321

Merged
normanmaurer merged 1 commit into
netty:5.0from
chrisvest:5.0-port-pr-18253
Nov 10, 2026
Merged

normanmaurer merged 1 commit into
netty:5.0from
chrisvest:5.0-port-pr-18253

Conversation

@chrisvest

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)

…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]>
@testlens-app

testlens-app Bot commented Nov 10, 2026 •

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.

@normanmaurer
normanmaurer merged commit 96d487a into netty:5.0 Nov 10, 2026
19 of 21 checks passed
@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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants