gnodet commented on code in PR #1918: URL: https://github.com/apache/maven-mvnd/pull/1918#discussion_r4792884363
########## daemon/src/main/java/org/mvndaemon/mvnd/cache/invalidating/RealmDisposer.java: ########## @@ -0,0 +1,85 @@ +/* + * 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.mvndaemon.mvnd.cache.invalidating; + +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Singleton; + +import java.lang.reflect.Field; + +import org.codehaus.plexus.classworlds.realm.ClassRealm; +import org.codehaus.plexus.classworlds.realm.NoSuchRealmException; +import org.eclipse.sisu.bean.BeanManager; +import org.eclipse.sisu.bean.LifecycleManager; +import org.eclipse.sisu.plexus.PlexusLifecycleManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Disposes the class realms evicted from the mvnd caches. + * <p> + * Besides disposing the realm in its {@code ClassWorld}, this also removes the classes of the realm from the JSR250 + * lifecycle cache of Sisu ({@link LifecycleManager}). That cache uses strong {@code Class} keys and Sisu does not flush + * it when a realm is disposed (eclipse-sisu/sisu-project#281), so every evicted realm would otherwise stay reachable + * for the lifetime of the daemon. This can be removed once mvnd uses a Sisu version that flushes the cache itself. + */ +@Named +@Singleton +public class RealmDisposer { + + private static final Logger LOG = LoggerFactory.getLogger(RealmDisposer.class); + + private final LifecycleManager lifecycleManager; + + @Inject + public RealmDisposer(BeanManager beanManager) { + this.lifecycleManager = findLifecycleManager(beanManager); + } + + public void dispose(ClassRealm realm) { Review Comment: **Suggestion: Add null check for defensive programming** Consider adding a null check at the start of this method: ```java public void dispose(ClassRealm realm) { if (realm == null) { return; } // ... rest of method } ``` While current callers appear to handle null appropriately, this would make the API safer and prevent potential NPEs if called from new code paths in the future. ########## daemon/src/main/java/org/mvndaemon/mvnd/cache/invalidating/RealmDisposer.java: ########## @@ -0,0 +1,85 @@ +/* + * 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.mvndaemon.mvnd.cache.invalidating; + +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Singleton; + +import java.lang.reflect.Field; + +import org.codehaus.plexus.classworlds.realm.ClassRealm; +import org.codehaus.plexus.classworlds.realm.NoSuchRealmException; +import org.eclipse.sisu.bean.BeanManager; +import org.eclipse.sisu.bean.LifecycleManager; +import org.eclipse.sisu.plexus.PlexusLifecycleManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Disposes the class realms evicted from the mvnd caches. + * <p> + * Besides disposing the realm in its {@code ClassWorld}, this also removes the classes of the realm from the JSR250 + * lifecycle cache of Sisu ({@link LifecycleManager}). That cache uses strong {@code Class} keys and Sisu does not flush + * it when a realm is disposed (eclipse-sisu/sisu-project#281), so every evicted realm would otherwise stay reachable + * for the lifetime of the daemon. This can be removed once mvnd uses a Sisu version that flushes the cache itself. + */ +@Named +@Singleton +public class RealmDisposer { + + private static final Logger LOG = LoggerFactory.getLogger(RealmDisposer.class); + + private final LifecycleManager lifecycleManager; + + @Inject + public RealmDisposer(BeanManager beanManager) { + this.lifecycleManager = findLifecycleManager(beanManager); + } + + public void dispose(ClassRealm realm) { + try { + realm.getWorld().disposeRealm(realm.getId()); + } catch (NoSuchRealmException e) { + // ignore + } + if (lifecycleManager != null) { + lifecycleManager.flushCacheFor(clazz -> clazz.getClassLoader() == realm); + } + } + + private static LifecycleManager findLifecycleManager(BeanManager beanManager) { + if (beanManager instanceof LifecycleManager) { + return (LifecycleManager) beanManager; + } + if (beanManager instanceof PlexusLifecycleManager) { + try { + Field delegate = PlexusLifecycleManager.class.getDeclaredField("delegate"); + delegate.setAccessible(true); + Object jsr250Lifecycle = delegate.get(beanManager); + if (jsr250Lifecycle instanceof LifecycleManager) { + return (LifecycleManager) jsr250Lifecycle; + } + } catch (ReflectiveOperationException | RuntimeException e) { + LOG.debug("Could not access the JSR250 lifecycle manager of Sisu", e); Review Comment: **Suggestion: Consider using WARN level instead of DEBUG** When the LifecycleManager cannot be accessed via reflection, the memory leak fix won't work. This is a significant degradation in functionality, so WARN level would be more appropriate: ```java LOG.warn("Could not access the JSR250 lifecycle manager of Sisu; realm classes may not be garbage collected", e); ``` This would make it more visible in logs if the workaround is not functioning. ########## daemon/src/main/java/org/mvndaemon/mvnd/cache/invalidating/RealmDisposer.java: ########## @@ -0,0 +1,85 @@ +/* + * 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.mvndaemon.mvnd.cache.invalidating; + +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Singleton; + +import java.lang.reflect.Field; + +import org.codehaus.plexus.classworlds.realm.ClassRealm; +import org.codehaus.plexus.classworlds.realm.NoSuchRealmException; +import org.eclipse.sisu.bean.BeanManager; +import org.eclipse.sisu.bean.LifecycleManager; +import org.eclipse.sisu.plexus.PlexusLifecycleManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Disposes the class realms evicted from the mvnd caches. + * <p> + * Besides disposing the realm in its {@code ClassWorld}, this also removes the classes of the realm from the JSR250 + * lifecycle cache of Sisu ({@link LifecycleManager}). That cache uses strong {@code Class} keys and Sisu does not flush + * it when a realm is disposed (eclipse-sisu/sisu-project#281), so every evicted realm would otherwise stay reachable + * for the lifetime of the daemon. This can be removed once mvnd uses a Sisu version that flushes the cache itself. + */ +@Named +@Singleton +public class RealmDisposer { + + private static final Logger LOG = LoggerFactory.getLogger(RealmDisposer.class); + + private final LifecycleManager lifecycleManager; + + @Inject + public RealmDisposer(BeanManager beanManager) { + this.lifecycleManager = findLifecycleManager(beanManager); + } + + public void dispose(ClassRealm realm) { + try { + realm.getWorld().disposeRealm(realm.getId()); + } catch (NoSuchRealmException e) { + // ignore Review Comment: **Minor: Consider debug logging the exception** For troubleshooting purposes, it might be helpful to log this exception at DEBUG level: ```java } catch (NoSuchRealmException e) { LOG.debug("Realm {} already disposed", realm.getId(), e); } ``` This is a minor point since this exception is expected in some cases. -- 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]
