Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-63766] Work around JDK-8231454 #420

Merged
merged 2 commits into from
May 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import hudson.model.Item;
import hudson.model.TaskListener;
import hudson.util.FormValidation;
import io.jenkins.lib.versionnumber.JavaSpecificationVersion;

import java.beans.Introspector;
import java.io.Serializable;
Expand Down Expand Up @@ -206,6 +207,7 @@ private static void cleanUpClass(Class<?> clazz, Set<ClassLoader> encounteredLoa
if (encounteredClasses.add(clazz)) {
LOGGER.log(Level.FINER, "found {0}", clazz.getName());
Introspector.flushFromCaches(clazz);
cleanUpClassInfoCache(clazz);
cleanUpGlobalClassSet(clazz);
cleanUpClassHelperCache(clazz);
cleanUpObjectStreamClassCaches(clazz);
Expand Down Expand Up @@ -266,6 +268,45 @@ private static void cleanUpGlobalClassValue(@NonNull ClassLoader loader) throws
}
}

private static void cleanUpClassInfoCache(Class<?> clazz) {
JavaSpecificationVersion current = JavaSpecificationVersion.forCurrentJVM();
if (current.isNewerThan(new JavaSpecificationVersion("1.8"))
&& current.isOlderThan(new JavaSpecificationVersion("16"))) {
try {
// TODO Work around JDK-8231454.
Class<?> classInfoC = Class.forName("com.sun.beans.introspect.ClassInfo");
Field cacheF = classInfoC.getDeclaredField("CACHE");
try {
cacheF.setAccessible(true);
} catch (RuntimeException e) { // TODO Java 9+ InaccessibleObjectException
/*
* Not running with "--add-opens java.desktop/com.sun.beans.introspect=ALL-UNNAMED".
* Until core adds this to its --add-opens configuration, and until that core
* change is widely adopted, avoid unnecessary log spam and return early.
*/
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.log(Level.FINER, "Failed to clean up " + clazz.getName() + " from ClassInfo#CACHE. A metaspace leak may have occurred.", e);
}
return;
}
Object cache = cacheF.get(null);
Class<?> cacheC = Class.forName("com.sun.beans.util.Cache");
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.log(Level.FINER, "Cleaning up " + clazz.getName() + " from ClassInfo#CACHE.");
}
Method removeM = cacheC.getMethod("remove", Object.class);
removeM.invoke(cache, clazz);
} catch (ReflectiveOperationException e) {
/*
* Should never happen, but if it does, ensure the failure is isolated to this
* method and does not prevent other cleanup logic from executing.
*/
LOGGER.log(Level.WARNING, "Failed to clean up " + clazz.getName() + " from ClassInfo#CACHE. A metaspace leak may have occurred.", e);
}
}
}


private static void cleanUpGlobalClassSet(@NonNull Class<?> clazz) throws Exception {
Class<?> classInfoC = Class.forName("org.codehaus.groovy.reflection.ClassInfo"); // or just ClassInfo.class, but unclear whether this will always be there
Field globalClassSetF = classInfoC.getDeclaredField("globalClassSet");
Expand Down Expand Up @@ -313,15 +354,18 @@ private static void cleanUpObjectStreamClassCaches(@NonNull Class<?> clazz) thro
for (String cacheFName : new String[] {"localDescs", "reflectors"}) {
Field cacheF = cachesC.getDeclaredField(cacheFName);
cacheF.setAccessible(true);
ConcurrentMap<Reference<Class<?>>, ?> cache = (ConcurrentMap) cacheF.get(null);
Iterator<? extends Map.Entry<Reference<Class<?>>, ?>> iterator = cache.entrySet().iterator();
while (iterator.hasNext()) {
if (iterator.next().getKey().get() == clazz) {
iterator.remove();
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.log(Level.FINER, "cleaning up {0} from ObjectStreamClass.Caches.{1}", new Object[] {clazz.getName(), cacheFName});
Object cache = cacheF.get(null);
if (cache instanceof ConcurrentMap) {
// Prior to JDK-8277072
Iterator<? extends Map.Entry<Reference<Class<?>>, ?>> iterator = ((ConcurrentMap) cache).entrySet().iterator();
while (iterator.hasNext()) {
if (iterator.next().getKey().get() == clazz) {
iterator.remove();
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.log(Level.FINER, "cleaning up {0} from ObjectStreamClass.Caches.{1}", new Object[]{clazz.getName(), cacheFName});
}
break;
}
break;
}
}
}
Expand Down