Skip to content

Commit 68e1480

Browse files
authored
Fix memory leak when OOM (#15207)
1 parent 29472c0 commit 68e1480

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

  • dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4

dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyChannel.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import io.netty.channel.ChannelFuture;
4545
import io.netty.channel.ChannelFutureListener;
4646
import io.netty.handler.codec.EncoderException;
47+
import io.netty.util.ReferenceCountUtil;
4748

4849
import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_ENCODE_IN_IO_THREAD;
4950
import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_TIMEOUT;
@@ -185,10 +186,11 @@ public void send(Object message, boolean sent) throws RemotingException {
185186

186187
boolean success = true;
187188
int timeout = 0;
189+
ByteBuf buf = null;
188190
try {
189191
Object outputMessage = message;
190192
if (!encodeInIOThread) {
191-
ByteBuf buf = channel.alloc().buffer();
193+
buf = channel.alloc().buffer();
192194
ChannelBuffer buffer = new NettyBackedChannelBuffer(buf);
193195
codec.encode(this, buffer, message);
194196
outputMessage = buf;
@@ -224,6 +226,10 @@ public void operationComplete(ChannelFuture future) throws Exception {
224226
}
225227
} catch (Throwable e) {
226228
removeChannelIfDisconnected(channel);
229+
if (buf != null) {
230+
// Release the ByteBuf if an exception occurs
231+
ReferenceCountUtil.safeRelease(buf);
232+
}
227233
throw new RemotingException(
228234
this,
229235
"Failed to send message " + PayloadDropper.getRequestWithoutData(message) + " to "

0 commit comments

Comments
 (0)