This is an automated email from the ASF dual-hosted git repository.

szetszwo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ratis.git


The following commit(s) were added to refs/heads/master by this push:
     new 97b76ba3d RATIS-2416. Fix memory leak in NettyClientReplies for stale 
replies. (#1357)
97b76ba3d is described below

commit 97b76ba3d1196ad0859e084d6d92720c85e009f7
Author: slfan1989 <[email protected]>
AuthorDate: Tue Feb 24 10:09:41 2026 +0800

    RATIS-2416. Fix memory leak in NettyClientReplies for stale replies. (#1357)
---
 .../ratis/netty/client/NettyClientReplies.java     |  6 ++-
 .../ratis/netty/client/NettyClientStreamRpc.java   |  2 +-
 .../ratis/netty/client/TestNettyClientReplies.java | 45 ++++++++++++++++++++++
 3 files changed, 51 insertions(+), 2 deletions(-)

diff --git 
a/ratis-netty/src/main/java/org/apache/ratis/netty/client/NettyClientReplies.java
 
b/ratis-netty/src/main/java/org/apache/ratis/netty/client/NettyClientReplies.java
index 4c49b1d16..695177262 100644
--- 
a/ratis-netty/src/main/java/org/apache/ratis/netty/client/NettyClientReplies.java
+++ 
b/ratis-netty/src/main/java/org/apache/ratis/netty/client/NettyClientReplies.java
@@ -40,11 +40,15 @@ public class NettyClientReplies {
 
   private final ConcurrentMap<ClientInvocationId, ReplyMap> replies = new 
ConcurrentHashMap<>();
 
-  ReplyMap getReplyMap(ClientInvocationId clientInvocationId) {
+  ReplyMap getOrCreateReplyMap(ClientInvocationId clientInvocationId) {
     final MemoizedSupplier<ReplyMap> q = MemoizedSupplier.valueOf(() -> new 
ReplyMap(clientInvocationId));
     return replies.computeIfAbsent(clientInvocationId, key -> q.get());
   }
 
+  ReplyMap getReplyMap(ClientInvocationId clientInvocationId) {
+    return replies.get(clientInvocationId);
+  }
+
   class ReplyMap {
     private final ClientInvocationId clientInvocationId;
     private final Map<RequestEntry, ReplyEntry> map = new 
ConcurrentHashMap<>();
diff --git 
a/ratis-netty/src/main/java/org/apache/ratis/netty/client/NettyClientStreamRpc.java
 
b/ratis-netty/src/main/java/org/apache/ratis/netty/client/NettyClientStreamRpc.java
index 54ad8acf6..4970d244a 100644
--- 
a/ratis-netty/src/main/java/org/apache/ratis/netty/client/NettyClientStreamRpc.java
+++ 
b/ratis-netty/src/main/java/org/apache/ratis/netty/client/NettyClientStreamRpc.java
@@ -473,7 +473,7 @@ public class NettyClientStreamRpc implements 
DataStreamClientRpc {
     ClientInvocationId clientInvocationId = 
ClientInvocationId.valueOf(request.getClientId(), request.getStreamId());
     final boolean isClose = 
request.getWriteOptionList().contains(StandardWriteOption.CLOSE);
 
-    final NettyClientReplies.ReplyMap replyMap = 
replies.getReplyMap(clientInvocationId);
+    final NettyClientReplies.ReplyMap replyMap = 
replies.getOrCreateReplyMap(clientInvocationId);
     final ChannelFuture channelFuture;
     final Channel channel;
     final NettyClientReplies.RequestEntry requestEntry = new 
NettyClientReplies.RequestEntry(request);
diff --git 
a/ratis-test/src/test/java/org/apache/ratis/netty/client/TestNettyClientReplies.java
 
b/ratis-test/src/test/java/org/apache/ratis/netty/client/TestNettyClientReplies.java
new file mode 100644
index 000000000..5e22761e2
--- /dev/null
+++ 
b/ratis-test/src/test/java/org/apache/ratis/netty/client/TestNettyClientReplies.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ratis.netty.client;
+
+import org.apache.ratis.protocol.ClientId;
+import org.apache.ratis.protocol.ClientInvocationId;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+public class TestNettyClientReplies {
+  @Test
+  public void testGetReplyMapDoesNotCreate() {
+    final NettyClientReplies replies = new NettyClientReplies();
+    final ClientInvocationId clientInvocationId =
+        ClientInvocationId.valueOf(ClientId.randomId(), 1L);
+
+    assertNull(replies.getReplyMap(clientInvocationId));
+
+    final NettyClientReplies.ReplyMap created = 
replies.getOrCreateReplyMap(clientInvocationId);
+    assertNotNull(created);
+    assertSame(created, replies.getReplyMap(clientInvocationId));
+
+    final ClientInvocationId other =
+        ClientInvocationId.valueOf(ClientId.randomId(), 2L);
+    assertNull(replies.getReplyMap(other));
+  }
+}

Reply via email to