From 1369d6e7957861aa3c3c2c535b1d32355087ab9b Mon Sep 17 00:00:00 2001 From: LeonxLJX <51880185+LeonxLJX@users.noreply.github.com> Date: Thu, 12 Nov 2026 10:05:56 +0800 Subject: [PATCH 1/4] fix(trip): prevent netty memory leak by ensuring ByteBuf release in doOnData When deframer.decode() throws an exception, the ByteBufInputStream was never closed, leaking the underlying ByteBuf. This is especially likely during RPC timeout scenarios where malformed or unexpected data may arrive. Use try-with-resources to guarantee ByteBufInputStream.close() is called, which releases the ByteBuf (the second constructor argument releaseOnClose=true). Also add an explicit catch block to release data as a safety net. --- .../tri/stream/AbstractTripleClientStream.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/stream/AbstractTripleClientStream.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/stream/AbstractTripleClientStream.java index b36d761ec755..46b32e07335e 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/stream/AbstractTripleClientStream.java +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/stream/AbstractTripleClientStream.java @@ -590,9 +590,16 @@ private void doOnData(ByteBuf data, boolean endStream) { handleH2TransportError(TriRpcStatus.INTERNAL.withDescription("headers not received before payload")); return; } - // Use ByteBufInputStream to adapt ByteBuf to InputStream - // The second parameter 'true' means release ByteBuf after reading - deframer.decode(new ByteBufInputStream(data, true)); + // Use try-with-resources to ensure ByteBufInputStream is always closed, + // which releases the underlying ByteBuf, even if deframer.decode() throws. + // The second parameter 'true' means release ByteBuf when the stream is closed. + try (ByteBufInputStream inputStream = new ByteBufInputStream(data, true)) { + deframer.decode(inputStream); + } catch (Throwable t) { + // Ensure data is released if deframer.decode() throws before InputStream is fully consumed + ReferenceCountUtil.release(data); + LOGGER.error(PROTOCOL_FAILED_RESPONSE, "", "", "Failed to decode triple response data", t); + } } @Override From 82ae96dae73dfeec2b7b61c41220a25a1d5da71b Mon Sep 17 00:00:00 2001 From: LeonxLJX <51880185+LeonxLJX@users.noreply.github.com> Date: Sat, 14 Nov 2026 18:21:45 +0800 Subject: [PATCH 2/4] fix(triple): avoid premature ByteBuf release causing deserialize failure The original try-with-resources fix in PR #16587 closed the ByteBufInputStream immediately after deframer.decode() returned, which prematurely released the underlying Netty ByteBuf. LengthFieldStreamingDecoder only partially consumes the InputStream (accumulating it in CompositeInputStream 'accumulate' for subsequent frames), so closing it here invalidates data the decoder still needs. This caused TriPojoClientTest.greetLong to fail with INTERNAL: Deserialize response failed because Hessian2 deserialization reads from the already-released ByteBuf. The fix restores the original non-try-with-resources form so the stream is only released later via accumulate.close() when deframer.close() is called (on EOF or trailers). The catch block now properly closes deframer on DecodeException so accumulate.close() releases all remaining streams - avoiding the Netty memory leak that the original PR was trying to fix. Fixes the Samples Test failure on Java 8 and prevents ByteBuf double-release. --- .../stream/AbstractTripleClientStream.java | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/stream/AbstractTripleClientStream.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/stream/AbstractTripleClientStream.java index 46b32e07335e..24fac8b6f2a7 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/stream/AbstractTripleClientStream.java +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/stream/AbstractTripleClientStream.java @@ -590,15 +590,26 @@ private void doOnData(ByteBuf data, boolean endStream) { handleH2TransportError(TriRpcStatus.INTERNAL.withDescription("headers not received before payload")); return; } - // Use try-with-resources to ensure ByteBufInputStream is always closed, - // which releases the underlying ByteBuf, even if deframer.decode() throws. + // Use ByteBufInputStream to adapt ByteBuf to InputStream. // The second parameter 'true' means release ByteBuf when the stream is closed. - try (ByteBufInputStream inputStream = new ByteBufInputStream(data, true)) { - deframer.decode(inputStream); + // NOTE: We must NOT close this stream here because deframer holds a reference + // to it via accumulate.addInputStream(). The stream will be closed later (releasing + // the underlying ByteBuf) when accumulate.close() is called via deframer.close(). + // Closing it here would prematurely invalidate data that deframer still needs + // for subsequent deserialization, causing "Deserialize response failed". + try { + deframer.decode(new ByteBufInputStream(data, true)); } catch (Throwable t) { - // Ensure data is released if deframer.decode() throws before InputStream is fully consumed - ReferenceCountUtil.release(data); + // If deframer.decode() throws (e.g. DecodeException from invalid payload), + // the ByteBufInputStream has already been added to accumulate. We must close + // the deframer so that accumulate.close() releases all accumulated streams, + // including this one, avoiding a Netty ByteBuf memory leak. LOGGER.error(PROTOCOL_FAILED_RESPONSE, "", "", "Failed to decode triple response data", t); + try { + deframer.close(); + } catch (Exception ignore) { + // deframer.close() may throw if already closing; safe to ignore + } } } From e7bf1d6f09d496160377a920334ae1848c9a5c55 Mon Sep 17 00:00:00 2001 From: Stella <51880185+LeonxLJX@users.noreply.github.com> Date: Sun, 29 Nov 2026 23:31:12 +0800 Subject: [PATCH 3/4] fix(trip): use onStreamClosed() for immediate cleanup in decode-failure path --- .../stream/AbstractTripleClientStream.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/stream/AbstractTripleClientStream.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/stream/AbstractTripleClientStream.java index 24fac8b6f2a7..91429662f25f 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/stream/AbstractTripleClientStream.java +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/stream/AbstractTripleClientStream.java @@ -600,16 +600,22 @@ private void doOnData(ByteBuf data, boolean endStream) { try { deframer.decode(new ByteBufInputStream(data, true)); } catch (Throwable t) { - // If deframer.decode() throws (e.g. DecodeException from invalid payload), - // the ByteBufInputStream has already been added to accumulate. We must close - // the deframer so that accumulate.close() releases all accumulated streams, - // including this one, avoiding a Netty ByteBuf memory leak. - LOGGER.error(PROTOCOL_FAILED_RESPONSE, "", "", "Failed to decode triple response data", t); + // decode() threw — e.g. DecodeException from invalid gRPC length field. + // We must immediately close the accumulate to release the ByteBuf that was + // added via accumulate.addInputStream() before decode() failed. + // deframer.close() takes a deferred (stalled-aware) path which never executes + // in the decode-failure case because state stays HEADER; onStreamClosed() + // calls accumulate.close() directly and now, releasing the ByteBuf. try { - deframer.close(); + deframer.onStreamClosed(); } catch (Exception ignore) { - // deframer.close() may throw if already closing; safe to ignore + // accumulate.close() may throw; safe to ignore — ByteBuf is released } + // Route through handleH2TransportError so the caller is notified instead + // of silently waiting until request timeout. + handleH2TransportError(TriRpcStatus.INTERNAL + .withDescription("Failed to decode triple response data: " + t.getMessage()) + .withCause(t)); } } From 82208149ba6a95bff5c29c35f76fc200de35b962 Mon Sep 17 00:00:00 2001 From: Stella <51880185+LeonxLJX@users.noreply.github.com> Date: Sun, 29 Nov 2026 23:32:19 +0800 Subject: [PATCH 4/4] test(trip): add regression test for decode-failure ByteBuf leak in doOnData --- .../tri/stream/TripleClientStreamTest.java | 63 ++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/stream/TripleClientStreamTest.java b/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/stream/TripleClientStreamTest.java index 623a14974517..9c5d4d282268 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/stream/TripleClientStreamTest.java +++ b/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/stream/TripleClientStreamTest.java @@ -131,4 +131,65 @@ void progress() { transportListener.onData(buf, false); Assertions.assertEquals(1, listener.message.length); } -} + + @Test + public void testDecodeFailureReleasesByteBufAndNotifiesCaller() { + // Regression test for #16587: + // When deframer.decode() throws (e.g. negative gRPC length field), + // the ByteBuf must be released and the error must propagate to the caller. + // Previously deframer.close() took a deferred path and never ran in this case, + // leaking the ByteBuf. Now deframer.onStreamClosed() closes accumulate directly. + + TripleWriteQueue writeQueue = mock(TripleWriteQueue.class); + when(writeQueue.enqueueFuture(any(QueuedCommand.class), any(Executor.class))) + .thenReturn(new CompletableFuture<>()); + when(writeQueue.enqueue(any(QueuedCommand.class))) + .thenReturn(new CompletableFuture<>()); + + TripleClientStreamListener listener = new TripleClientStreamListener(); + StreamObserver streamObserver = new StreamObserver<>() { + @Override public void onNext(Object data) {} + @Override public void onError(Throwable t) {} + @Override public void onCompleted() {} + @Override public void setCompletableFuture(CompletableFuture completableFuture) {} + }; + ClientStream stream = new Http2TripleClientStream( + ApplicationModel.defaultModel(), + Runnable::run, + writeQueue, + listener, + streamObserver, + new SocketStub(), + new Metadata(), + new Metadata(), + Compressor.NONE); + stream.initStream(); + + H2TransportListener transportListener = stream.createTransportListener(); + + // Send valid headers first + DefaultHttp2Headers headers = new DefaultHttp2Headers(); + headers.scheme(HttpScheme.HTTP.name()).status(HttpResponseStatus.OK.codeAsText()); + headers.set(TripleHeaderEnum.STATUS_KEY.getKey(), TriRpcStatus.OK.code.code + ""); + headers.set(HttpHeaderNames.CONTENT_TYPE.getKey(), MediaType.APPLICATION_GRPC_PROTO.getName()); + transportListener.onHeader(headers, false); + assertTrue(listener.started); + + stream.request(2); + + // DATA frame with NEGATIVE gRPC length field (0x80000000 = -2147483648) + // This makes LengthFieldStreamingDecoder.processHeader() throw RpcException + byte[] corruptData = new byte[] {0, (byte)0x80, 0, 0, 1}; + final ByteBuf buf = Unpooled.wrappedBuffer(corruptData); + assertEquals(1, buf.refCnt()); + + transportListener.onData(buf, false); + + // ByteBuf MUST be released (refCnt == 0) after decode failure + assertEquals(0, buf.refCnt(), "ByteBuf should be released after decode failure"); + + // Caller MUST be notified via listener.onComplete (not silently timeout) + assertTrue(listener.completed, "listener.onComplete should be called on decode failure"); + assertNotNull(listener.status, "listener.status should be set on decode failure"); + } +} \ No newline at end of file