GeezerVison is showing github.com — SslHandler: Stop wrapping when the transport can not keep up by normanmaurer · Pull Request #18253 · netty/netty
Conversation
normanmaurer
commented
Nov 3, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
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. With the default water marks of 32KB
and 64KB, the encrypted data that is waiting to be written for a 1MB
write goes down from 1050432 to 65652 bytes (-94%), which is the memory
that is needed for it.
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.
SslHandlerSlowTransportBenchmark, TLSv1.2, AES-128-GCM, write size /
bytes the transport writes each time ("all" is everything written so
far), max pending bytes is the most encrypted data that was waiting to
be written at the same time:
time (us/op) max pending bytes
before after before after
JDK 16KB/64KB 5.3 ± 0.3 5.3 ± 0.2 16413 16413
JDK 16KB/all 5.3 ± 0.2 5.3 ± 0.1 16413 16413
JDK 1MB/64KB 356.5 ± 4.2 358.4 ± 6.6 1050432 65652
JDK 1MB/all 358.5 ± 5.8 358.3 ± 4.4 1050432 65652
OPENSSL 16KB/64KB 3.9 ± 0.1 3.9 ± 0.1 16413 16413
OPENSSL 16KB/all 3.9 ± 0.1 3.8 ± 0.2 16413 16413
OPENSSL 1MB/64KB 253.1 ± 3.9 257.7 ± 1.2 1050432 65652
OPENSSL 1MB/all 254.4 ± 2.3 258.4 ± 2.8 1050432 65652
The allocation rate (-prof gc) is the same for 16KB writes and about 1KB
lower per 1MB write (JDK 104743 -> 103554 B/op, OpenSSL 30057 -> 29102
B/op).
SslHandlerEchoBenchmark, TLSv1.2, AES-128-GCM, direct buffers, message
size x number of writes (ops/s):
before after
JDK 1KB x 1 566473 ± 13375 565161 ± 18471
JDK 1KB x 10 119876 ± 825 120350 ± 346
JDK 4KB x 1 267206 ± 2172 267043 ± 3587
JDK 4KB x 10 33032 ± 267 33205 ± 189
OPENSSL 1KB x 1 689871 ± 25770 703325 ± 6357
OPENSSL 1KB x 10 168617 ± 3507 166535 ± 1092
OPENSSL 4KB x 1 375844 ± 5663 377976 ± 2637
OPENSSL 4KB x 10 46235 ± 461 46362 ± 136
The numbers were taken in-process (-f 0, 5 warmup and 5 measurement
iterations of 1s) with JDK 25.0.1 on an Apple M5 Max.
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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. With the default water marks of 32KB
and 64KB, the encrypted data that is waiting to be written for a 1MB
write goes down from 1050432 to 65652 bytes (-94%), which is the memory
that is needed for it.
SslHandlerSlowTransportBenchmark, TLSv1.2, AES-128-GCM, write size /
bytes the transport writes each time ("all" is everything written so
far), max pending bytes is the most encrypted data that was waiting to
be written at the same time:
time (us/op) max pending bytes
before after before after
JDK 16KB/64KB 5.3 ± 0.3 5.3 ± 0.2 16413 16413
JDK 16KB/all 5.3 ± 0.2 5.3 ± 0.1 16413 16413
JDK 1MB/64KB 356.5 ± 4.2 358.4 ± 6.6 1050432 65652
JDK 1MB/all 358.5 ± 5.8 358.3 ± 4.4 1050432 65652
OPENSSL 16KB/64KB 3.9 ± 0.1 3.9 ± 0.1 16413 16413
OPENSSL 16KB/all 3.9 ± 0.1 3.8 ± 0.2 16413 16413
OPENSSL 1MB/64KB 253.1 ± 3.9 257.7 ± 1.2 1050432 65652
OPENSSL 1MB/all 254.4 ± 2.3 258.4 ± 2.8 1050432 65652
The allocation rate (-prof gc) is the same for 16KB writes and about 1KB
lower per 1MB write (JDK 104743 -> 103554 B/op, OpenSSL 30057 -> 29102
B/op).
SslHandlerEchoBenchmark, TLSv1.2, AES-128-GCM, direct buffers, message
size x number of writes (ops/s):
before after
JDK 1KB x 1 566473 ± 13375 565161 ± 18471
JDK 1KB x 10 119876 ± 825 120350 ± 346
JDK 4KB x 1 267206 ± 2172 267043 ± 3587
JDK 4KB x 10 33032 ± 267 33205 ± 189
OPENSSL 1KB x 1 689871 ± 25770 703325 ± 6357
OPENSSL 1KB x 10 168617 ± 3507 166535 ± 1092
OPENSSL 4KB x 1 375844 ± 5663 377976 ± 2637
OPENSSL 4KB x 10 46235 ± 461 46362 ± 136
The numbers were taken in-process (-f 0, 5 warmup and 5 measurement
iterations of 1s) with JDK 25.0.1 on an Apple M5 Max.
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 wrapping is suspended, 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.
Motivation:
A review of the change that stops wrapping when the transport can not
keep up found two cases in which SslHandler does not do what it should
while wrapping is suspended:
- When the peer sends a record that can not be decrypted, the JDK
SSLEngine produces the alert on the next wrap(...).
handleUnwrapThrowable(...) calls wrapAndFlush(...) for this, but as
more than the high water mark was still pending, wrapping was
suspended again right away. The alert was only produced once the
channel was closed already.
- A delegated task that is run directly can call closeOutbound(), for
example from a TrustManager. If wrapping is suspended, closing the
outbound side waits for the task to complete, but nothing continued
it once a task that was run directly completed. No close_notify was
written, and close() only completed once the close_notify flush
timeout fired.
Modification:
- Set STATE_OUTBOUND_CLOSED before calling wrapAndFlush(...) in
handleUnwrapThrowable(...), so wrapping is not suspended. It is set
right after anyway.
- Once a delegated task that was run directly completed, continue what
had to wait for it later on the EventExecutor.
- Add tests for both cases, and make the existing tests check what they
claim and not depend on the exception messages of the JDK.
- Fail SslHandlerSlowTransportBenchmark if not all the data was written.
Result:
The alert is written before the channel is closed, and the close_notify
is written after the data even if closeOutbound() is called from a
delegated task that is run directly.
The behaviour of SslHandler also changes in two cases that were not
listed before:
- 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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Motivation:
While the SSLEngine is handshaking, for example during a renegotiation,
wrap(...) only wraps one record at a time. If wrapping resumed during a
renegotiation, STATE_WRAP_SUSPENDED was cleared while most of the data
that was held back was still queued. Closing the channel or calling
closeOutbound() then closed the outbound side of the SSLEngine without
wrapping this data first, so it was failed with SslClosedEngineException
instead of written.
Modification:
- Add STATE_WRAP_PENDING, which is set when wrapping is suspended and
only cleared once all the queued data was wrapped. As long as it is
set, wrap the queued data before closing the outbound side.
- Add the test that was suggested in the review.
Result:
Data that was held back is written before the close_notify, even if
wrapping resumed during a renegotiation.
As before, 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.
Co-authored-by: Violeta Georgieva <[email protected]>
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
testlens-app
Bot
commented
Nov 4, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
✅ All tests passed ✅
🏷️ Commit: 1a2d597
▶️ Tests: 152161 executed
⚪️ Checks: 28/28 completed
Learn more about TestLens at testlens.app/docs.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
|
Could not create auto-port PR.
Got conflicts when cherry-picking onto 5.0.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
normanmaurer
pushed a commit
that referenced
this pull request
Nov 10, 2026
#18253) (#18321)
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<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]>
hyperxpro
pushed a commit
to AsyncHttpClient/async-http-client
that referenced
this pull request
Nov 21, 2026
## Summary
- Upgrades Netty from 4.2.20.Final to
[4.2.21.Final](https://github.com/netty/netty/releases/tag/netty-4.2.21.Final).
It mainly reverts a build mistake in 4.2.20: Netty's native libraries
for x86_64 and aarch64 required glibc 2.33. AHC declares the native
epoll and io_uring jars at its Netty version, and when they do not load,
it falls back to NIO, silently unless `useNativeTransport` is set. On
4.2.20 that would happen on systems with an older glibc, such as RHEL 8
or Ubuntu 20.04. AHC 3.0.14 used Netty 4.2.18, so no AHC release has
4.2.20.
- 4.2.21 also fixes `ZstdDecoder` and `ZstdDecompressor` at a
non-positive `io.netty.compression.defaultMaxForwardBytes`
(netty/netty#18289). This PR drops AHC's workarounds for that bug, so
zstd now waits for demand at any value of the property, like br and
snappy. A zero or negative value of the property now needs Netty 4.2.21
or later.
## Change
- **Netty 4.2.21.Final.**
- **Drop the `ZstdDecoder` replacement from #2375.** At a non-positive
value of the property, `Http1ContentDecompressor` replaced the
`ZstdDecoder` that `HttpContentDecompressor` creates with one whose
buffers hold `MAX_PART_SIZE`, because Netty's decoder handed on an empty
body at 0 and failed below it.
- **Drop the zstd guard from #2362.** At such a value, zstd went to
`HttpContentDecompressor` instead of `PullDecompression`, because
Netty's `ZstdDecompressor` handed out nothing at 0 while reporting more
output. It now takes the pull path there too:
- it waits for demand;
- parts are at most 64 KiB;
- the size limit applies;
- a truncated stream fails with `Truncated zstd stream` instead of
ending quietly.
- **Docs:** the README and the Javadoc of
`ResponseBodyControl.suspend()` and of `Http1ContentDecompressor` no
longer make an exception for zstd. The README and `suspend()` say
instead that a zero or negative value of the property needs Netty 4.2.21
or later. With Netty 4.2.17 to 4.2.20, zstd responses then fail at a
negative value, and at 0 they never end and keep the event loop busy, as
Netty's `ZstdDecompressor` hands out empty buffers while it reports more
output. `MAX_FORWARD_BYTES` is private, since only
`Http1ContentDecompressor` uses it now.
- **Test:** `Http1ContentDecompressorForwardThresholdTest` runs with the
property at 0 and -1 in a fresh JVM. It now also checks zstd while
suspending after every part (one part per resume, none larger than
`MAX_PART_SIZE`) and a truncated zstd body (it fails, and nothing is
left allocated).
## Notes
- With the property at its default (64 KiB), nothing changes for zstd.
- Other changes in 4.2.21 that touch AHC's paths:
- Netty now also rejects control characters such as NUL and ESC, and
DEL, in a response's reason phrase (netty/netty#18331). CR and LF were
already rejected; HTAB is still allowed.
- `SslHandler` changes: netty/netty#18253, #18323, #18363 and #18406.
AHC's tests pass without changes.
## AI disclosure
Claude Code on behalf of Matthias Kurz. The commit includes
`Co-Authored-By` per `AGENTS.md`.
## Test plan
- [x] `./mvnw -B -ntp -Dgpg.skip=true clean verify` on JDK 11: 2,098
tests, 0 failures, 0 errors, 11 skipped; Revapi passed. No test-skipping
flags were used.
- [x] With the README and Javadoc change from review (`5fd35bad8`), the
same `clean verify`: 2,098 tests, 0 failures, 0 errors, 22 skipped;
Revapi passed. Docker was not running, so the 11 Testcontainers proxy
tests skipped themselves; they ran and passed in the runs above, on the
same code.
- [x] The version change alone, without the other changes, passes the
same `clean verify`: 2,098 tests, 0 failures.
- [x] The extended test fails where it should:
- with the guard put back, at 0 and -1: "the suspended zstd body did not
wait for demand after its first part";
- without the workarounds on Netty 4.2.20: at 0, the check hangs until
its 60 s timeout; at -1, it fails with `IllegalArgumentException:
initialCapacity : -1`.
- [x] The `Http1ContentDecompressor*Test`,
`Http2ContentDecompressorTest`, `Http1Decompression*Test` and
`ResponseBodyControlTest` classes: 142 tests, 0 failures.
- [x] Merged with #2394 (`ef18b0b68`): no conflicts; the same classes,
203 tests, 0 failures; `clean verify` on JDK 11, 2,159 tests, 0
failures, 0 errors, 11 skipped; Revapi passed.
---------
Co-authored-by: Claude Opus 5.5 <[email protected]>
Reviewers
yawkat
Awaiting requested review from yawkat
franz1981
Awaiting requested review from franz1981
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|