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

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


The following commit(s) were added to refs/heads/master by this push:
     new f7602e324e fix: close RabbitMQ connection when channel close fails 
(#6944)
f7602e324e is described below

commit f7602e324ebce13b38fde63778b0f9cbc7e1699e
Author: BobSong <[email protected]>
AuthorDate: Sun Sep 6 10:31:25 2026 +0800

    fix: close RabbitMQ connection when channel close fails (#6944)
    
    Co-authored-by: aias00 <[email protected]>
---
 .../rabbitmq/client/RabbitmqLogCollectClient.java     | 15 ++++++++++-----
 .../rabbitmq/client/RabbitmqLogCollectClientTest.java | 19 +++++++++++++++++++
 2 files changed, 29 insertions(+), 5 deletions(-)

diff --git 
a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-rabbitmq/src/main/java/org/apache/shenyu/plugin/logging/rabbitmq/client/RabbitmqLogCollectClient.java
 
b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-rabbitmq/src/main/java/org/apache/shenyu/plugin/logging/rabbitmq/client/RabbitmqLogCollectClient.java
index b599be56e2..236022eb75 100644
--- 
a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-rabbitmq/src/main/java/org/apache/shenyu/plugin/logging/rabbitmq/client/RabbitmqLogCollectClient.java
+++ 
b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-rabbitmq/src/main/java/org/apache/shenyu/plugin/logging/rabbitmq/client/RabbitmqLogCollectClient.java
@@ -142,12 +142,17 @@ public class RabbitmqLogCollectClient extends 
AbstractLogConsumeClient<RabbitmqL
             if (Objects.nonNull(channel)) {
                 channel.close();
             }
-            if (Objects.nonNull(connection)) {
-                connection.close();
-            }
-            LOG.info("close RabbitMQ connection success");
         } catch (IOException e) {
-            LOG.error("failed to close RabbitMQ connection", e);
+            LOG.error("failed to close RabbitMQ channel", e);
+        } finally {
+            try {
+                if (Objects.nonNull(connection)) {
+                    connection.close();
+                }
+                LOG.info("close RabbitMQ connection success");
+            } catch (IOException e) {
+                LOG.error("failed to close RabbitMQ connection", e);
+            }
         }
     }
 
diff --git 
a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-rabbitmq/src/test/java/org/apache/shenyu/plugin/logging/rabbitmq/client/RabbitmqLogCollectClientTest.java
 
b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-rabbitmq/src/test/java/org/apache/shenyu/plugin/logging/rabbitmq/client/RabbitmqLogCollectClientTest.java
index f9848a9808..fb22671b51 100644
--- 
a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-rabbitmq/src/test/java/org/apache/shenyu/plugin/logging/rabbitmq/client/RabbitmqLogCollectClientTest.java
+++ 
b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-rabbitmq/src/test/java/org/apache/shenyu/plugin/logging/rabbitmq/client/RabbitmqLogCollectClientTest.java
@@ -17,6 +17,8 @@
 
 package org.apache.shenyu.plugin.logging.rabbitmq.client;
 
+import com.rabbitmq.client.Channel;
+import com.rabbitmq.client.Connection;
 import org.apache.shenyu.common.dto.PluginData;
 import org.apache.shenyu.common.utils.GsonUtils;
 import org.apache.shenyu.plugin.logging.common.entity.ShenyuRequestLog;
@@ -24,7 +26,10 @@ import 
org.apache.shenyu.plugin.logging.rabbitmq.config.RabbitmqLogCollectConfig
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
+import org.mockito.Mockito;
+import org.springframework.test.util.ReflectionTestUtils;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -72,4 +77,18 @@ public class RabbitmqLogCollectClientTest {
         rabbitmqLogCollectClient.close();
     }
 
+    @Test
+    public void testCloseConnectionWhenChannelCloseFails() throws Exception {
+        Channel channel = Mockito.mock(Channel.class);
+        Connection connection = Mockito.mock(Connection.class);
+        Mockito.doThrow(new IOException("channel close 
failed")).when(channel).close();
+        ReflectionTestUtils.setField(rabbitmqLogCollectClient, "channel", 
channel);
+        ReflectionTestUtils.setField(rabbitmqLogCollectClient, "connection", 
connection);
+
+        rabbitmqLogCollectClient.close0();
+
+        Mockito.verify(channel).close();
+        Mockito.verify(connection).close();
+    }
+
 }

Reply via email to