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 5dfa9ec791 fix(common): avoid recursive discard rejection (#7101)
5dfa9ec791 is described below

commit 5dfa9ec791fa485f674ff055c33c596b25a894bc
Author: Liming Deng <[email protected]>
AuthorDate: Thu Oct 1 06:29:44 2026 +0800

    fix(common): avoid recursive discard rejection (#7101)
---
 .../shenyu/common/concurrent/DiscardOldestPolicy.java      |  5 +++++
 .../common/concurrent/MemorySafeLinkedBlockingQueue.java   |  4 ++++
 .../concurrent/MemorySafeLinkedBlockingQueueTest.java      | 14 ++++++++++++++
 3 files changed, 23 insertions(+)

diff --git 
a/shenyu-common/src/main/java/org/apache/shenyu/common/concurrent/DiscardOldestPolicy.java
 
b/shenyu-common/src/main/java/org/apache/shenyu/common/concurrent/DiscardOldestPolicy.java
index 44a8fc0bb4..5def757d6b 100644
--- 
a/shenyu-common/src/main/java/org/apache/shenyu/common/concurrent/DiscardOldestPolicy.java
+++ 
b/shenyu-common/src/main/java/org/apache/shenyu/common/concurrent/DiscardOldestPolicy.java
@@ -25,8 +25,13 @@ import java.util.Queue;
 public class DiscardOldestPolicy<E> implements Rejector<E> {
 
     @Override
+    @SuppressWarnings("unchecked")
     public void reject(final E e, final Queue<E> queue) {
         queue.poll();
+        if (queue instanceof MemorySafeLinkedBlockingQueue) {
+            ((MemorySafeLinkedBlockingQueue<E>) 
queue).offerWithoutMemoryCheck(e);
+            return;
+        }
         queue.offer(e);
     }
 }
diff --git 
a/shenyu-common/src/main/java/org/apache/shenyu/common/concurrent/MemorySafeLinkedBlockingQueue.java
 
b/shenyu-common/src/main/java/org/apache/shenyu/common/concurrent/MemorySafeLinkedBlockingQueue.java
index 6dd9a0bae2..6dd4c6e799 100644
--- 
a/shenyu-common/src/main/java/org/apache/shenyu/common/concurrent/MemorySafeLinkedBlockingQueue.java
+++ 
b/shenyu-common/src/main/java/org/apache/shenyu/common/concurrent/MemorySafeLinkedBlockingQueue.java
@@ -111,4 +111,8 @@ public class MemorySafeLinkedBlockingQueue<E> extends 
LinkedBlockingQueue<E> {
         }
         return super.offer(e);
     }
+
+    boolean offerWithoutMemoryCheck(final E e) {
+        return super.offer(e);
+    }
 }
diff --git 
a/shenyu-common/src/test/java/org/apache/shenyu/common/concurrent/MemorySafeLinkedBlockingQueueTest.java
 
b/shenyu-common/src/test/java/org/apache/shenyu/common/concurrent/MemorySafeLinkedBlockingQueueTest.java
index 60b356ea89..afbfa63c32 100644
--- 
a/shenyu-common/src/test/java/org/apache/shenyu/common/concurrent/MemorySafeLinkedBlockingQueueTest.java
+++ 
b/shenyu-common/src/test/java/org/apache/shenyu/common/concurrent/MemorySafeLinkedBlockingQueueTest.java
@@ -19,6 +19,7 @@ package org.apache.shenyu.common.concurrent;
 
 import org.junit.jupiter.api.Test;
 
+import java.util.Collections;
 import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
 
@@ -49,6 +50,19 @@ public class MemorySafeLinkedBlockingQueueTest {
         }));
     }
 
+    @Test
+    public void testDiscardOldestDoesNotReenterMemoryCheck() {
+        Runnable oldest = () -> { };
+        Runnable replacement = () -> { };
+        MemorySafeLinkedBlockingQueue<Runnable> queue =
+                new 
MemorySafeLinkedBlockingQueue<>(Collections.singletonList(oldest), 
Integer.MAX_VALUE);
+        queue.setRejector(new DiscardOldestPolicy<>());
+
+        assertThat(queue.offer(replacement), is(false));
+        assertThat(queue.size(), is(1));
+        assertThat(queue.peek(), is(replacement));
+    }
+
     @Test
     public void testMutableConfigurationIsVolatile() throws 
NoSuchFieldException {
         Field maxFreeMemory = 
MemorySafeLinkedBlockingQueue.class.getDeclaredField("maxFreeMemory");

Reply via email to