Copilot commented on code in PR #7093:
URL: https://github.com/apache/shenyu/pull/7093#discussion_r4032817441


##########
shenyu-web/src/main/java/org/apache/shenyu/web/loader/ShenyuPluginClassLoader.java:
##########
@@ -194,6 +199,11 @@ private <T> boolean isLoadedByOtherClassLoader(final T 
inst) {
         return !inst.getClass().getClassLoader().equals(this);
     }
 
+    private String getPluginBeanName(final String className) {
+        String pluginKey = 
Optional.ofNullable(pluginJar.getAbsolutePath()).orElse(pluginJar.getJarKey());
+        return pluginKey + "#" + className;

Review Comment:
   The name identifies the plugin jar, but not this loader generation. Two 
loaders replacing the same jar therefore use the same name; if the newer loader 
registers before the older loader finishes `close()`, the older loader still 
has that name in `registeredBeanNames` and destroys the newer bean. This is the 
reload race described by #6730, and the holder's contains/remove/put sequence 
is not atomic. Include a per-loader generation in the name, or destroy only 
after verifying the current bean/definition is owned by this classloader.



##########
shenyu-web/src/test/java/org/apache/shenyu/web/loader/ShenyuPluginClassLoaderBeanIsolationTest.java:
##########
@@ -0,0 +1,75 @@
+/*
+ * 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.web.loader;
+
+import org.apache.shenyu.plugin.api.utils.SpringBeanUtils;
+import org.junit.jupiter.api.Test;
+import org.springframework.context.support.GenericApplicationContext;
+import org.springframework.stereotype.Component;
+
+import java.util.Collections;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class ShenyuPluginClassLoaderBeanIsolationTest {
+
+    @Test
+    void closingOnePluginShouldNotDestroyAnotherPluginsBean() {
+        GenericApplicationContext context = new GenericApplicationContext();
+        context.refresh();
+        SpringBeanUtils.getInstance().setApplicationContext(context);
+        ShenyuPluginClassLoader firstLoader = createLoader("plugin-one", 
FirstPlugin.SharedComponent.class);
+        ShenyuPluginClassLoader secondLoader = createLoader("plugin-two", 
SecondPlugin.SharedComponent.class);

Review Comment:
   This does not reproduce the reported legacy-name collision. `getBeanName` 
strips only the package, so these nested classes had distinct old names 
containing `$FirstPlugin$` and `$SecondPlugin$`; parent-first delegation also 
means both classes are loaded by the test classloader rather than the two 
`ShenyuPluginClassLoader` instances. Use fixture classes in different packages 
with the same simple name (and actual class bytes loaded by each plugin loader) 
so the test fails specifically when one loader can destroy the other's bean.



##########
shenyu-web/src/main/java/org/apache/shenyu/web/loader/ShenyuPluginClassLoader.java:
##########
@@ -140,23 +143,24 @@ protected Class<?> findClass(final String name) throws 
ClassNotFoundException {
 
     @Override
     public void close() {
-        Set<String> clazzNames = pluginJar.getClazzMap().keySet();
-        for (String clazzName : clazzNames) {
-            SpringBeanUtils.getInstance().destroyBean(clazzName);
+        for (String beanName : registeredBeanNames) {
+            SpringBeanUtils.getInstance().destroyBeanByName(beanName);
         }
+        registeredBeanNames.clear();
     }
 
     private <T> T getOrCreateSpringBean(final String className) throws 
ClassNotFoundException, IllegalAccessException, InstantiationException {
-        if (SpringBeanUtils.getInstance().existBean(className)) {
-            T inst = 
SpringBeanUtils.getInstance().getBeanByClassName(className);
+        String beanName = getPluginBeanName(className);
+        if (SpringBeanUtils.getInstance().existBeanByName(beanName)) {
+            T inst = SpringBeanUtils.getInstance().getBeanByName(beanName);
             // if the class is loaded by other classloader, then reload it
             if (!isLoadedByOtherClassLoader(inst)) {

Review Comment:
   `getBeanByName` explicitly returns `null` when bean creation raises a 
`BeansException`, but this branch immediately passes that value to 
`isLoadedByOtherClassLoader`, which dereferences it. A previously registered 
bean definition whose construction fails (or a concurrent registration) 
therefore turns into an uncaught NPE and aborts loading the remaining plugin 
classes. Guard the class-loader check with a non-null test.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to