Skip to content

Commit 569917b

Browse files
authored
Fix a bug in ConstructorCache when classes are GC'ed but not removed from cache (#6186)
* Fix a bug in ConstructorCache when classes are GC'ed but not removed from cache * Added a changelog * Address PR feedback
1 parent eac6daf commit 569917b

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "bugfix",
3+
"category": "AWS SDK for Java V2",
4+
"contributor": "",
5+
"description": "Fix a bug in ConstructorCache when classes are GC'ed but not removed from cache"
6+
}

core/checksums/src/main/java/software/amazon/awssdk/checksums/internal/ConstructorCache.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,17 @@ private Optional<Class<?>> getClass(String className) {
6363
return Optional.empty();
6464
}
6565
});
66-
return classRef.map(WeakReference::get);
66+
67+
// if the WeakReference to the class has been garbage collected, remove it from the cache and try again
68+
if (classRef.isPresent()) {
69+
Class<?> clazz = classRef.get().get();
70+
if (clazz != null) {
71+
return Optional.of(clazz);
72+
}
73+
classesByClassLoader.remove(classLoader);
74+
return getClass(className);
75+
}
76+
return Optional.empty();
6777
}
6878

6979
/**

0 commit comments

Comments
 (0)