Skip to content

fix: release late response ByteBuf when callback executor is shut down (#16415) - #16418

Open
jamespud wants to merge 3 commits into
apache:3.3from
jamespud:feature/fix-16415-triple-bytebuf-leak
Open

fix: release late response ByteBuf when callback executor is shut down (#16415)#16418
jamespud wants to merge 3 commits into
apache:3.3from
jamespud:feature/fix-16415-triple-bytebuf-leak

Conversation

@jamespud

@jamespud jamespud commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

What is the purpose of the change

Fixes #16415.

When a sync Triple call times out, AsyncRpcResult#get(timeout) shuts down the per-call ThreadlessExecutor in a finally block. Any DATA frame that arrives afterwards is handed to AbstractTripleClientStream.ClientTransportListener.onData(), which submits the decode task via SerializingExecutor. Because the underlying executor is already shut down, SerializingExecutor silently drops the submitted task, so doOnData() never runs and the DATA frame's ByteBuf (wrapped in ByteBufInputStream(data, true) only inside doOnData) is never released — a Netty ByteBuf leak that grows on every timed-out call with in-flight response data.

Root cause

For a sync Triple call the callback executor is a per-call ThreadlessExecutor. On timeout, AsyncRpcResult#get(timeout) shuts it down in a finally block. Late DATA frames are then submitted to a SerializingExecutor wrapping that shut-down executor; SerializingExecutor silently drops them (a behavior from #15122), so doOnData() never executes and the ByteBuf is never released.

Brief changelog

  1. AbstractStream (dubbo-rpc-triple): retain the raw callback executor and expose isCallbackExecutorShutdown().
  2. AbstractTripleClientStream (dubbo-rpc-triple): in onData, when the callback executor has been shut down, release the DATA frame's ByteBuf immediately instead of submitting a task that would be silently dropped; log at WARN since dropping late data after a timeout is expected.

Verifying the change

  • TripleClientStreamTest#testOnDataReleaseByteBufAfterCallbackExecutorShutdown (new): a DATA-frame ByteBuf received after the callback executor is shut down is released (refCnt == 0). This test fails before the fix (refCnt == 1).

Fixes #16415

@codecov-commenter

codecov-commenter commented Aug 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 62.50000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 60.88%. Comparing base (3a30432) to head (f0b5ef4).

Files with missing lines Patch % Lines
.../dubbo/rpc/protocol/tri/stream/AbstractStream.java 33.33% 1 Missing and 1 partial ⚠️
...rotocol/tri/stream/AbstractTripleClientStream.java 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##                3.3   #16418   +/-   ##
=========================================
  Coverage     60.87%   60.88%           
- Complexity    11766    11768    +2     
=========================================
  Files          1953     1953           
  Lines         89273    89280    +7     
  Branches      13473    13474    +1     
=========================================
+ Hits          54346    54357   +11     
+ Misses        29333    29330    -3     
+ Partials       5594     5593    -1     
Flag Coverage Δ
integration-tests-java21 32.13% <12.50%> (-0.01%) ⬇️
integration-tests-java8 32.21% <12.50%> (-0.01%) ⬇️
samples-tests-java21 32.11% <12.50%> (-0.09%) ⬇️
samples-tests-java8 29.81% <12.50%> (+0.01%) ⬆️
unit-tests-java11 59.13% <62.50%> (+0.02%) ⬆️
unit-tests-java17 ?
unit-tests-java21 58.60% <62.50%> (-0.01%) ⬇️
unit-tests-java25 58.57% <62.50%> (+<0.01%) ⬆️
unit-tests-java8 59.16% <62.50%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

apache#16415)

When a sync Triple call times out, AsyncRpcResult#get(timeout) shuts down the per-call ThreadlessExecutor in a finally block. Late DATA frames that arrive afterwards are submitted via SerializingExecutor, which silently drops tasks submitted to a shut-down executor, so doOnData never runs and the DATA frame's ByteBuf is never released - a Netty ByteBuf leak that grows on every timed-out call with in-flight response data.

Check the callback executor's shutdown state in onData and release the ByteBuf immediately when it is shut down, instead of relying on the submission being rejected (SerializingExecutor swallows the rejection on shutdown).

Verification:
- TripleClientStreamTest#testOnDataReleaseByteBufAfterCallbackExecutorShutdown (new): a DATA-frame ByteBuf received after the callback executor is shut down is released (refCnt == 0). This test fails before the fix (refCnt == 1).

Fixes apache#16415
@jamespud
jamespud force-pushed the feature/fix-16415-triple-bytebuf-leak branch from 627725e to f3afd83 Compare August 11, 2026 05:57
@zrlw
zrlw requested a lite review from Copilot August 12, 2026 02:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a Netty ByteBuf leak in the Triple client stream when late DATA frames arrive after a sync call times out and the per-call callback executor has already been shut down, causing the decode task submission to be dropped.

Changes:

  • Track the original (raw) callback executor in AbstractStream and expose isCallbackExecutorShutdown().
  • In AbstractTripleClientStream.onData(), immediately release late DATA ByteBufs when the callback executor is shut down (instead of submitting a task that will be dropped).
  • Add a unit test to assert late DATA ByteBufs are released after callback-executor shutdown.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/stream/AbstractStream.java Retains the raw callback executor and provides shutdown-state detection for leak prevention.
dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/stream/AbstractTripleClientStream.java Releases late response ByteBufs when tasks would be dropped due to executor shutdown/rejection; adjusts logging.
dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/stream/TripleClientStreamTest.java Adds a regression test ensuring late DATA ByteBuf is released after callback executor shutdown.
Suppressed comments (1)

dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/stream/AbstractTripleClientStream.java:585

  • The WARN message in the executor-rejection path says "Drop late response data", but this catch block can also be triggered by thread pool saturation (as noted in the comment). The log message should not imply the data is necessarily late; otherwise it can mislead debugging/alert triage.
                // Tasks will be rejected when the thread pool is closed or full,
                // ByteBuf needs to be released to avoid out of heap memory leakage.
                // For example, ThreadLessExecutor will be shutdown when request timeout {@link AsyncRpcResult}
                ReferenceCountUtil.release(data);
                LOGGER.warn(PROTOCOL_FAILED_RESPONSE, "", "", "Drop late response data, executor rejected the task", t);
            }

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +150 to +155
Http2StreamChannel http2StreamChannel = mock(Http2StreamChannel.class);
when(http2StreamChannel.isActive()).thenReturn(true);
when(http2StreamChannel.newSucceededFuture()).thenReturn(channel.newSucceededFuture());
when(http2StreamChannel.eventLoop()).thenReturn(new NioEventLoopGroup().next());
when(http2StreamChannel.newPromise()).thenReturn(channel.newPromise());
when(http2StreamChannel.parent()).thenReturn(channel);
Comment on lines +569 to +575
// The callback executor (e.g. ThreadlessExecutor) has been shut down, e.g.
// after the request timed out {@link AsyncRpcResult}. SerializingExecutor would
// silently drop the submitted task so doOnData would never run; release the
// ByteBuf now to avoid out of heap memory leakage.
ReferenceCountUtil.release(data);
LOGGER.warn(PROTOCOL_FAILED_RESPONSE, "", "", "Drop late response data, callback executor is shutdown");
return;
Address Copilot review feedback on apache#16418:
- Rephrase onData comments and WARN messages so they do not imply the response data is always late (the rejection can also come from thread pool saturation) and drop the inaccurate 'out of heap memory' wording (ByteBufs can be off-heap).
- Reuse the EmbeddedChannel event loop in TripleClientStreamTest instead of creating a new NioEventLoopGroup, which started threads that were never shut down.
@jamespud

Copy link
Copy Markdown
Contributor Author

@zrlw Hi, the "Unit Test" jobs are failing only on the pre-existing flaky SpringBootConfigPropsTest (expected: but was: , test-isolation issue in dubbo-config-spring; it passes locally and is unrelated to this PR). Could you re-run the failed unit-test jobs? Thanks!

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.

[Bug] Dubbo 3.3.6 trip protocol netty memory leak

3 participants