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 — Pooled direct memory increases due to change in ChunkedWriteHandler (Netty 4.1.135) · Issue #17304 · netty/netty

Pooled direct memory increases due to change in ChunkedWriteHandler (Netty 4.1.135) #17304

New issue
Copy link
New issue
Copy link
Open
Open
Pooled direct memory increases due to change in ChunkedWriteHandler (Netty 4.1.135)#17304
Copy link

Description

@cfolgere
cfolgere
opened on Aug 19, 2026
Issue body actions

Hello,

We noticed a problem in our Java web server when we upgraded Netty (previously we were using 4.1.91.Final). We track direct buffer usage and saw a huge spike when we upgraded to 4.1.135.Final. It's not a leak per se since it doesn't continuously increase, but it's still enough to cause problems for us. We tracked this down to #13711.

The theory for why this is a problem for us is because we stream data (via HTTPS) to many clients which are unpredictable in speed. Previously, the writes were queued via a new PendingWrite in the ChunkedWriteHandler, but now they are available immediately to downstream handlers (maybe SslHandler) which queue up direct buffers. We also confirmed this commit as the suspect by reverting just that commit and deploying. Below is an image from our test environment showing direct memory metrics before/after this change.

I have a reproducer here -> https://github.com/cfolgere/netty-chunked-write-issue You can run it on any Netty version and see a pass/fail.

Is this intended, and is there any fix or workaround?

Application details:
Netty - 4.1.135.Final
Netty TC native - 4.1.74.Final
OpenSSL - 3.5.x
Vert.x - 4.4.1

Image

Activity

  1. franz1981 commented on Aug 19, 2026

    @franz1981
    franz1981
    on Aug 19, 2026
    Contributor
    More actions

    @slandelle ?

  2. arnabnandy7 commented on Aug 20, 2026

    @arnabnandy7
    arnabnandy7
    on Aug 20, 2026
    Contributor
    More actions

    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:

    1. 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.

    2. 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.

    3. 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

    Reacted by Francesco Nigro
  3. franz1981 commented on Aug 24, 2026

    @franz1981
    franz1981
    on Aug 24, 2026
    Contributor
    More actions

    @vietj @cescoffier I think this can hit Vertx and Quarkus too ^^

    Reacted by Clement Escoffier
  4. slandelle commented on Aug 25, 2026

    @slandelle
    slandelle
    on Aug 25, 2026
    Contributor
    More actions

    TBH, I don't think the solution to this problem is to revert:

    1. as pointed out, there's indeed no semantic change
    2. I don't think it's ChunkedWriteHandler 's responsibility to deal with anything else than ChunkedInput
    3. as pointed out, on the client side, it's frequent to add it defensively as you never know beforehand if the channel will ultimately have to deal with large file uploads, in particular when using connection pooling

    It looks to me that the previous behavior was a lucky yet unexpected interaction with SslHandler.
    If SslHandler needs back pressure to protect against high direct memory usage caused by slow peers, maybe it's SslHandler itself's responsibility.

    Reacted by Arnab Nandy
  5. marktrinh commented on Sep 2, 2026

    @marktrinh
    marktrinh
    on Sep 2, 2026
    More actions

    I see the conversation here dropped off. Do we know the path forward? this is affecting us too.

  6. franz1981 commented on Sep 2, 2026

    @franz1981
    franz1981
    on Sep 2, 2026
    Contributor
    More actions

    us too

    It means straight on Netty users? Vertx? Miconaut? Spring? Quarkus?
    Just to better understand 🙏

  7. marktrinh commented on Sep 2, 2026

    @marktrinh
    marktrinh
    on Sep 2, 2026
    More actions

    we're on vertx, not netty only

  8. franz1981 commented on Sep 2, 2026

    @franz1981
    franz1981
    on Sep 2, 2026
    Contributor
    More actions

    There's a sibling Vertx issue too?
    @vietj any thoughts?

  9. arnabnandy7 commented on Sep 2, 2026

    @arnabnandy7
    arnabnandy7
    on Sep 2, 2026
    Contributor
    More actions

    It looks to me that the previous behavior was a lucky yet unexpected interaction with SslHandler. If SslHandler needs back pressure to protect against high direct memory usage caused by slow peers, maybe it's SslHandler itself's responsibility.

    @slandelle agreed that restoring the old ChunkedWriteHandler behavior may not be the right architectural fix. The remaining issue is that valid unflushed writes can be eagerly transformed by SslHandler into pooled direct buffers without an effective bound for slow peers. The reproducer provides a deterministic example of that memory amplification.

    Should we track this as an SslHandler backpressure issue, or is Vert.x expected to prevent this amount of unflushed data from reaching the pipeline?

  10. vietj commented on Sep 3, 2026

    @vietj
    vietj
    on Sep 3, 2026
    Contributor
    More actions

    I think the SSL handler should be improved and queue input until it is really consumable as direct memory, it should not rely on another handler to work better.

  11. vietj commented on Sep 3, 2026

    @vietj
    vietj
    on Sep 3, 2026
    Contributor
    More actions

    vertx performs back pressure and for this it relies on netty writability signals, so it will behave like any netty application would, the application however, however perhaps the application using vertx is ignoring the writability signal relay from vertx

  12. arnabnandy7 commented on Sep 3, 2026

    @arnabnandy7
    arnabnandy7
    on Sep 3, 2026
    Contributor
    More actions

    @vietj I agree that relying on ChunkedWriteHandler to provide this buffering is accidental and that reverting #13711 is probably not the right fix.

    One detail may be important for locating the problem: SslHandler already queues application data in pendingUnencryptedWrites until flush(); it does not normally encrypt each write() immediately. However, those queued messages may already be pooled direct ByteBufs produced by an upstream encoder, and the bytes retained inside SslHandler do not appear to contribute to the channel outbound-buffer watermarks.

    Consequently, an application can respect Vert.x/Netty writability signals and still enqueue substantially more than the configured high watermark before flush(), because the channel remains writable while the data is held inside SslHandler.

    The existing reproducer proves the ChunkedWriteHandler propagation change, but it uses a synthetic direct-buffering handler rather than TLS. I think the next useful reproducer should use a real pipeline:

    ChunkedWriteHandler -> HttpResponseEncoder -> SslHandler -> transport
    

    It should write without flushing, use low write-buffer watermarks, stop whenever the channel becomes unwritable, and verify whether pooled direct memory can nevertheless grow well beyond the high watermark.

    If that reproduces the production behavior, would you agree that the fix should make SslHandler’s pending plaintext bytes participate in writability accounting, rather than merely queueing them differently?

  13. copley
    added a commit that references this issue on Sep 6, 2026

    SslHandler: bound TLS wrapping by outbound backpressure

    021cdd4
  14. chrisvest
    added a commit that references this issue on Nov 7, 2026

    SslHandler: Stop wrapping when the transport can not keep up (#18253)

    abe6408
  15. normanmaurer
    added a commit that references this issue on Nov 10, 2026

    Port 5.0: SslHandler: Stop wrapping when the transport can not keep up (

    96d487a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      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.