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 75dd02ff37 fix: guard shutdown hook reflection failures (#6786) (#6926)
75dd02ff37 is described below

commit 75dd02ff370cc4f6599f1b2a73cf6b66ce36e678
Author: BobSong <[email protected]>
AuthorDate: Mon Aug 17 19:33:02 2026 +0800

    fix: guard shutdown hook reflection failures (#6786) (#6926)
    
    Co-authored-by: aias00 <[email protected]>
---
 .../core/shutdown/ShenyuClientShutdownHook.java    | 11 ++++--
 .../shutdown/ShenyuClientShutdownHookTest.java     | 43 ++++++++++++++++++++++
 2 files changed, 51 insertions(+), 3 deletions(-)

diff --git 
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/shutdown/ShenyuClientShutdownHook.java
 
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/shutdown/ShenyuClientShutdownHook.java
index 04720181e8..2f0d121314 100644
--- 
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/shutdown/ShenyuClientShutdownHook.java
+++ 
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/shutdown/ShenyuClientShutdownHook.java
@@ -88,12 +88,17 @@ public class ShenyuClientShutdownHook {
                 Field field = 
clazz.getDeclaredField(props.getProperty("applicationShutdownHooksFieldName", 
"hooks"));
                 field.setAccessible(true);
                 hooks = (IdentityHashMap<Thread, Thread>) field.get(clazz);
-            } catch (ClassNotFoundException | NoSuchFieldException | 
IllegalAccessException ex) {
-                LOG.error(ex.getMessage(), ex);
+            } catch (ClassNotFoundException | NoSuchFieldException | 
IllegalAccessException | RuntimeException ex) {
+                LOG.warn("Failed to access application shutdown hooks, skip 
delaying other hooks.", ex);
+                return;
+            }
+            if (Objects.isNull(hooks)) {
+                LOG.warn("Application shutdown hooks are null, skip delaying 
other hooks.");
+                return;
             }
             long s = System.currentTimeMillis();
             while (System.currentTimeMillis() - s < delayOtherHooksExecTime) {
-                for (Iterator<Thread> iterator = 
Objects.requireNonNull(hooks).keySet().iterator(); iterator.hasNext();) {
+                for (Iterator<Thread> iterator = hooks.keySet().iterator(); 
iterator.hasNext();) {
                     Thread hook = iterator.next();
                     if 
(hook.getName().equals(ShutdownHookManager.getHookName())) {
                         continue;
diff --git 
a/shenyu-client/shenyu-client-core/src/test/java/org/apache/shenyu/client/core/shutdown/ShenyuClientShutdownHookTest.java
 
b/shenyu-client/shenyu-client-core/src/test/java/org/apache/shenyu/client/core/shutdown/ShenyuClientShutdownHookTest.java
new file mode 100644
index 0000000000..94297b68cb
--- /dev/null
+++ 
b/shenyu-client/shenyu-client-core/src/test/java/org/apache/shenyu/client/core/shutdown/ShenyuClientShutdownHookTest.java
@@ -0,0 +1,43 @@
+/*
+ * 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.client.core.shutdown;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.test.util.ReflectionTestUtils;
+
+import java.lang.reflect.Constructor;
+import java.util.Properties;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+
+public final class ShenyuClientShutdownHookTest {
+
+    @Test
+    void shouldSkipDelayWhenApplicationShutdownHooksCannotBeAccessed() throws 
Exception {
+        Properties properties = new Properties();
+        properties.setProperty("applicationShutdownHooksClassName", 
"org.apache.shenyu.InvalidApplicationShutdownHooks");
+        ReflectionTestUtils.setField(ShenyuClientShutdownHook.class, "props", 
properties);
+
+        Class<?> threadClass = 
Class.forName(ShenyuClientShutdownHook.class.getName() + 
"$TakeoverOtherHooksThread");
+        Constructor<?> constructor = threadClass.getDeclaredConstructor();
+        constructor.setAccessible(true);
+        Thread takeoverOtherHooksThread = (Thread) constructor.newInstance();
+
+        assertDoesNotThrow(takeoverOtherHooksThread::run);
+    }
+}

Reply via email to