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 6d923e3b2d fix(plugin): release response buffer once (#7110)
6d923e3b2d is described below

commit 6d923e3b2d239918bd32ac033d3a07e7cc2aa103
Author: Liming Deng <[email protected]>
AuthorDate: Wed Sep 23 13:56:44 2026 +0800

    fix(plugin): release response buffer once (#7110)
    
    Co-authored-by: aias00 <[email protected]>
---
 .../plugin/base/support/ResponseDecorator.java     |  2 +-
 .../plugin/base/support/ResponseDecoratorTest.java | 74 ++++++++++++++++++++++
 2 files changed, 75 insertions(+), 1 deletion(-)

diff --git 
a/shenyu-plugin/shenyu-plugin-base/src/main/java/org/apache/shenyu/plugin/base/support/ResponseDecorator.java
 
b/shenyu-plugin/shenyu-plugin-base/src/main/java/org/apache/shenyu/plugin/base/support/ResponseDecorator.java
index ae697c67f0..a532717d23 100644
--- 
a/shenyu-plugin/shenyu-plugin-base/src/main/java/org/apache/shenyu/plugin/base/support/ResponseDecorator.java
+++ 
b/shenyu-plugin/shenyu-plugin-base/src/main/java/org/apache/shenyu/plugin/base/support/ResponseDecorator.java
@@ -55,7 +55,7 @@ public class ResponseDecorator extends 
ServerHttpResponseDecorator {
             DataBufferUtils.release(dataBuffer);
             String bodyString = new String(bytes, StandardCharsets.UTF_8);
             final String convertStr = convert.apply(bodyString);
-            return WebFluxResultUtils.result(this.exchange, 
convertStr).doFinally(signalType -> DataBufferUtils.release(dataBuffer));
+            return WebFluxResultUtils.result(this.exchange, convertStr);
         });
     }
 }
diff --git 
a/shenyu-plugin/shenyu-plugin-base/src/test/java/org/apache/shenyu/plugin/base/support/ResponseDecoratorTest.java
 
b/shenyu-plugin/shenyu-plugin-base/src/test/java/org/apache/shenyu/plugin/base/support/ResponseDecoratorTest.java
new file mode 100644
index 0000000000..e502aa22ae
--- /dev/null
+++ 
b/shenyu-plugin/shenyu-plugin-base/src/test/java/org/apache/shenyu/plugin/base/support/ResponseDecoratorTest.java
@@ -0,0 +1,74 @@
+/*
+ * 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.shenyu.plugin.base.support;
+
+import org.apache.shenyu.plugin.api.result.DefaultShenyuResult;
+import org.apache.shenyu.plugin.api.result.ShenyuResult;
+import org.apache.shenyu.plugin.api.utils.SpringBeanUtils;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.core.io.buffer.DataBufferFactory;
+import org.springframework.core.io.buffer.PooledDataBuffer;
+import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
+import org.springframework.mock.web.server.MockServerWebExchange;
+import reactor.core.publisher.Mono;
+import reactor.test.StepVerifier;
+
+import java.nio.charset.StandardCharsets;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * Test cases for ResponseDecorator.
+ */
+public final class ResponseDecoratorTest {
+
+    @BeforeEach
+    public void setUp() {
+        ConfigurableApplicationContext context = 
mock(ConfigurableApplicationContext.class);
+        when(context.getBean(ShenyuResult.class)).thenReturn(new 
DefaultShenyuResult());
+        SpringBeanUtils.getInstance().setApplicationContext(context);
+    }
+
+    @Test
+    public void testJoinedBufferIsReleasedOnce() {
+        byte[] content = "body".getBytes(StandardCharsets.UTF_8);
+        PooledDataBuffer buffer = mock(PooledDataBuffer.class);
+        DataBufferFactory bufferFactory = mock(DataBufferFactory.class);
+        when(buffer.factory()).thenReturn(bufferFactory);
+        when(bufferFactory.join(any())).thenReturn(buffer);
+        when(buffer.readableByteCount()).thenReturn(content.length);
+        when(buffer.read(any(byte[].class))).thenAnswer(invocation -> {
+            System.arraycopy(content, 0, invocation.getArgument(0), 0, 
content.length);
+            return buffer;
+        });
+        when(buffer.isAllocated()).thenReturn(true);
+        when(buffer.release()).thenReturn(true);
+        MockServerWebExchange exchange = 
MockServerWebExchange.from(MockServerHttpRequest.get("/").build());
+
+        StepVerifier.create(new ResponseDecorator(exchange, 
String::toUpperCase).writeWith(Mono.just(buffer)))
+                .verifyComplete();
+
+        verify(buffer, times(1)).release();
+    }
+}

Reply via email to