This is an automated email from the ASF dual-hosted git repository.
earthchen pushed a commit to branch 3.2
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.2 by this push:
new 68e148080e Fix memory leak when OOM (#15207)
68e148080e is described below
commit 68e148080e5143fd29b58c98de7b91b1a7d5d9f3
Author: Albumen Kevin <[email protected]>
AuthorDate: Wed Mar 5 16:49:09 2025 +0800
Fix memory leak when OOM (#15207)
---
.../org/apache/dubbo/remoting/transport/netty4/NettyChannel.java | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git
a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyChannel.java
b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyChannel.java
index 8041c72bd8..3a03e2c519 100644
---
a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyChannel.java
+++
b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyChannel.java
@@ -44,6 +44,7 @@ import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.handler.codec.EncoderException;
+import io.netty.util.ReferenceCountUtil;
import static
org.apache.dubbo.common.constants.CommonConstants.DEFAULT_ENCODE_IN_IO_THREAD;
import static
org.apache.dubbo.common.constants.CommonConstants.DEFAULT_TIMEOUT;
@@ -185,10 +186,11 @@ final class NettyChannel extends AbstractChannel {
boolean success = true;
int timeout = 0;
+ ByteBuf buf = null;
try {
Object outputMessage = message;
if (!encodeInIOThread) {
- ByteBuf buf = channel.alloc().buffer();
+ buf = channel.alloc().buffer();
ChannelBuffer buffer = new NettyBackedChannelBuffer(buf);
codec.encode(this, buffer, message);
outputMessage = buf;
@@ -224,6 +226,10 @@ final class NettyChannel extends AbstractChannel {
}
} catch (Throwable e) {
removeChannelIfDisconnected(channel);
+ if (buf != null) {
+ // Release the ByteBuf if an exception occurs
+ ReferenceCountUtil.safeRelease(buf);
+ }
throw new RemotingException(
this,
"Failed to send message " +
PayloadDropper.getRequestWithoutData(message) + " to "