I investigated this and the behavior change does appear to come from #13711, specifically commit ce0366bf9f.
Before Netty 4.1.105, ChunkedWriteHandler queued every outbound write as a PendingWrite. Those messages were only passed to the next outbound handlers when the application flushed.
After #13711, a regular message bypasses the queue when there is no queued ChunkedInput:
if (!queueIsEmpty() || msg instanceof ChunkedInput) {
allocateQueue();
queue.add(new PendingWrite(msg, promise));
} else {
ctx.write(msg, promise);
}
This means the change is not limited to lazily allocating the queue. It also changes when non-chunked messages are propagated through the rest of the outbound pipeline.
For example, consider an application doing something equivalent to:
ctx.write(response);
ctx.write(content1);
ctx.write(content2);
// flush happens later
With 4.1.104 and earlier, these messages remain in ChunkedWriteHandler until the later flush.
With 4.1.105 and later, if no ChunkedInput is already queued, the messages immediately reach downstream handlers. In an HTTPS pipeline, that can cause the HTTP encoder and SslHandler to encode and encrypt the data immediately. As a result, lightweight pending Java objects are replaced by pooled direct buffers before the application calls flush().
For a single connection this may not be noticeable. With many concurrent slow clients, however, those encrypted direct buffers can remain pending for a significant amount of time. That explains why pooled direct-memory usage increases sharply but eventually stabilizes. This is probably not a leak, but it is a substantial change in where and in what form data is buffered.
The reproducer confirms the exact version boundary:
4.1.104.Final: passes
4.1.105.Final: fails
- Later 4.1 releases continue to show the new behavior
It also demonstrates that about 2 MiB of data written without a flush can result in approximately 2.2 MiB of pooled direct memory being allocated before the flush.
There is an important semantic detail here; in Netty, write() is generally allowed to propagate through handlers before flush(). A flush controls when pending writes are submitted toward the transport; it does not necessarily promise that encoders or SslHandler will defer all processing. From that perspective, the new pass-through behavior may be considered valid.
However, #13711 was described as a queue-allocation optimization. It appears to have unintentionally changed the buffering and backpressure behavior of ChunkedWriteHandler, with a potentially large memory impact for HTTPS servers serving slow clients. The old queue was effectively acting as a pre-encoding buffer, while the new behavior can move that buffering into pooled TLS buffers.
I think there are a few possible ways to address this:
-
Restore the previous behavior and queue all writes until flush. This is the safest option for compatibility, although it gives up part of the allocation optimization from Lazily allocate ChunkedWriteHandler#queue #13711.
-
Preserve the lazy allocation but allocate the queue on the first write and continue queueing until the corresponding flush. After the queue has been drained, it could potentially be released or reused. This would retain lazy initialization without allowing messages in the same write batch to bypass the handler.
-
Make the pass-through behavior configurable. This could preserve the current optimization for clients that only install ChunkedWriteHandler defensively, while allowing servers that depend on pre-encoding buffering to select the old behavior.
A possible implementation of the second option would be conceptually similar to:
@Override
public void write(
ChannelHandlerContext ctx,
Object msg,
ChannelPromise promise) throws Exception {
allocateQueue();
queue.add(new PendingWrite(msg, promise));
}
The queue would still be allocated lazily, but the first actual write would no longer bypass it. That preserves the allocation benefit for handlers that never receive a write, which was the original motivation for #13711, while restoring the pre-4.1.105 write/flush boundary.
Until there is an upstream resolution, reverting ce0366bf9f or using a small compatibility handler that queues outbound messages until flush should restore the previous memory behavior. Adjusting channel write-buffer watermarks may reduce the amount of pending data in some pipelines, but it may not fully solve this case because the direct allocation can occur while the write is being processed by SslHandler, before the later flush.
So overall, the @cfolgere’s analysis looks correct. This is not a continuously growing memory leak, but it is a real buffering regression introduced in 4.1.105, and its effect becomes especially significant with TLS and many slow consumers.
cc @franz1981 @slandelle