Commit ecfc8d73 authored by Phillip Webb's avatar Phillip Webb

Be defensive when clearing caches for restart

Update `Restarter` to be much more defensive when attempting to clear
caches. We now use `clearCache()` methods whenever possible, and only
fall back to field access when absolutely necessary. In addition field
access now ignore any exceptions.

Fixes gh-12719
parent 4e0afaf4
/* /*
* Copyright 2012-2016 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -341,16 +341,47 @@ public class Restarter { ...@@ -341,16 +341,47 @@ public class Restarter {
private void cleanupKnownCaches() throws Exception { private void cleanupKnownCaches() throws Exception {
// Whilst not strictly necessary it helps to cleanup soft reference caches // Whilst not strictly necessary it helps to cleanup soft reference caches
// early rather than waiting for memory limits to be reached // early rather than waiting for memory limits to be reached
clearResolvableTypeCache();
clearCachedIntrospectionResultsCache();
clearReflectionUtilsCache();
clearAnnotationUtilsCache();
clear("com.sun.naming.internal.ResourceManager", "propertiesCache");
}
private void clearResolvableTypeCache() throws Exception {
try {
ResolvableType.clearCache();
}
catch (Throwable ex) {
clear(ResolvableType.class, "cache"); clear(ResolvableType.class, "cache");
clear("org.springframework.core.SerializableTypeWrapper", "cache"); clear("org.springframework.core.SerializableTypeWrapper", "cache");
}
}
private void clearCachedIntrospectionResultsCache() throws Exception {
clear(CachedIntrospectionResults.class, "acceptedClassLoaders"); clear(CachedIntrospectionResults.class, "acceptedClassLoaders");
clear(CachedIntrospectionResults.class, "strongClassCache"); clear(CachedIntrospectionResults.class, "strongClassCache");
clear(CachedIntrospectionResults.class, "softClassCache"); clear(CachedIntrospectionResults.class, "softClassCache");
}
private void clearReflectionUtilsCache() throws Exception {
try {
ReflectionUtils.clearCache();
}
catch (Throwable ex) {
clear(ReflectionUtils.class, "declaredFieldsCache"); clear(ReflectionUtils.class, "declaredFieldsCache");
clear(ReflectionUtils.class, "declaredMethodsCache"); clear(ReflectionUtils.class, "declaredMethodsCache");
}
}
private void clearAnnotationUtilsCache() throws Exception {
try {
AnnotationUtils.clearCache();
}
catch (Throwable ex) {
clear(AnnotationUtils.class, "findAnnotationCache"); clear(AnnotationUtils.class, "findAnnotationCache");
clear(AnnotationUtils.class, "annotatedInterfaceCache"); clear(AnnotationUtils.class, "annotatedInterfaceCache");
clear("com.sun.naming.internal.ResourceManager", "propertiesCache"); }
} }
private void clear(String className, String fieldName) { private void clear(String className, String fieldName) {
...@@ -363,6 +394,7 @@ public class Restarter { ...@@ -363,6 +394,7 @@ public class Restarter {
} }
private void clear(Class<?> type, String fieldName) throws Exception { private void clear(Class<?> type, String fieldName) throws Exception {
try {
Field field = type.getDeclaredField(fieldName); Field field = type.getDeclaredField(fieldName);
field.setAccessible(true); field.setAccessible(true);
Object instance = field.get(null); Object instance = field.get(null);
...@@ -371,7 +403,8 @@ public class Restarter { ...@@ -371,7 +403,8 @@ public class Restarter {
} }
if (instance instanceof Map) { if (instance instanceof Map) {
Map<?, ?> map = ((Map<?, ?>) instance); Map<?, ?> map = ((Map<?, ?>) instance);
for (Iterator<?> iterator = map.keySet().iterator(); iterator.hasNext();) { for (Iterator<?> iterator = map.keySet().iterator(); iterator
.hasNext();) {
Object value = iterator.next(); Object value = iterator.next();
if (value instanceof Class && ((Class<?>) value) if (value instanceof Class && ((Class<?>) value)
.getClassLoader() instanceof RestartClassLoader) { .getClassLoader() instanceof RestartClassLoader) {
...@@ -381,6 +414,10 @@ public class Restarter { ...@@ -381,6 +414,10 @@ public class Restarter {
} }
} }
} }
catch (Exception ex) {
// Ignore
}
}
/** /**
* Cleanup any soft/weak references by forcing an {@link OutOfMemoryError} error. * Cleanup any soft/weak references by forcing an {@link OutOfMemoryError} error.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment