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 8af03fc9bb fix (admin) : shut down scaling monitor scheduler on 
context close. (#7799)
8af03fc9bb is described below

commit 8af03fc9bb6bc1cdd2a1077ac5274aedffa91d4b
Author: Mingyu Yang <[email protected]>
AuthorDate: Mon Oct 19 14:30:44 2026 +0800

    fix (admin) : shut down scaling monitor scheduler on context close. (#7799)
---
 .../scale/scaler/dynamic/TaskSchedulerManager.java |  4 +-
 .../scaler/dynamic/TaskSchedulerManagerTest.java   | 77 ++++++++++++++++++++++
 2 files changed, 80 insertions(+), 1 deletion(-)

diff --git 
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/scale/scaler/dynamic/TaskSchedulerManager.java
 
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/scale/scaler/dynamic/TaskSchedulerManager.java
index ab21d3a698..1beca803fd 100644
--- 
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/scale/scaler/dynamic/TaskSchedulerManager.java
+++ 
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/scale/scaler/dynamic/TaskSchedulerManager.java
@@ -17,6 +17,7 @@
 
 package org.apache.shenyu.admin.scale.scaler.dynamic;
 
+import jakarta.annotation.PreDestroy;
 import org.apache.shenyu.admin.scale.config.ScaleProperties;
 import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
 import org.springframework.stereotype.Component;
@@ -75,8 +76,9 @@ public class TaskSchedulerManager {
 
 
     /**
-     * shutdown.
+     * Release monitor tasks and the scheduler when the application context 
closes.
      */
+    @PreDestroy
     public void shutdown() {
         taskMap.values().forEach(future -> future.cancel(true));
         scheduler.shutdown();
diff --git 
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/scale/scaler/dynamic/TaskSchedulerManagerTest.java
 
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/scale/scaler/dynamic/TaskSchedulerManagerTest.java
new file mode 100644
index 0000000000..73800d4c60
--- /dev/null
+++ 
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/scale/scaler/dynamic/TaskSchedulerManagerTest.java
@@ -0,0 +1,77 @@
+/*
+ * 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.admin.scale.scaler.dynamic;
+
+import org.apache.shenyu.admin.scale.config.ScaleProperties;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+import 
org.springframework.context.annotation.AnnotationConfigApplicationContext;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
+import org.springframework.test.util.ReflectionTestUtils;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tests scheduler cleanup through the Spring component lifecycle.
+ */
+class TaskSchedulerManagerTest {
+
+    private AnnotationConfigApplicationContext context;
+
+    private TaskSchedulerManager manager;
+
+    @BeforeEach
+    void setUp() {
+        context = new 
AnnotationConfigApplicationContext(ScaleProperties.class, 
TaskSchedulerManager.class);
+        manager = context.getBean(TaskSchedulerManager.class);
+    }
+
+    @AfterEach
+    void tearDown() {
+        context.close();
+        // Release threads even if the lifecycle regression assertion fails.
+        manager.shutdown();
+    }
+
+    @ParameterizedTest
+    @ValueSource(booleans = {false, true})
+    void testContextCloseShutsDownScheduler(final boolean 
cancelTaskBeforeClose) throws InterruptedException {
+        final ThreadPoolTaskScheduler scheduler = (ThreadPoolTaskScheduler) 
ReflectionTestUtils.getField(manager, "scheduler");
+        final ScheduledThreadPoolExecutor executor = 
scheduler.getScheduledThreadPoolExecutor();
+        final CountDownLatch taskStarted = new CountDownLatch(1);
+        manager.registerMonitorTask("monitor", taskStarted::countDown, 10);
+        assertTrue(taskStarted.await(5, TimeUnit.SECONDS), "The monitor task 
should start before closing the context");
+
+        if (cancelTaskBeforeClose) {
+            manager.cancelMonitorTask("monitor");
+        }
+        assertFalse(executor.isShutdown());
+
+        context.close();
+
+        assertTrue(executor.isShutdown(), "Closing the context should shut 
down the monitor scheduler");
+        assertTrue(executor.awaitTermination(5, TimeUnit.SECONDS), "Monitor 
threads should terminate after context close");
+    }
+}

Reply via email to