Skip to content

Commit 82cf9a3

Browse files
authored
Fix issue #1111 by not creating methodType on each invocation (to work around a possible concurrency bug in JDK) (#1114)
1 parent b1462dd commit 82cf9a3

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

caffeine/src/main/java/com/github/benmanes/caffeine/cache/LocalCacheFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ interface LocalCacheFactory {
3232
MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
3333
MethodType FACTORY = MethodType.methodType(
3434
void.class, Caffeine.class, AsyncCacheLoader.class, boolean.class);
35+
MethodType FACTORY_CALL = FACTORY.changeReturnType(BoundedLocalCache.class);
3536
ConcurrentMap<String, LocalCacheFactory> FACTORIES = new ConcurrentHashMap<>();
3637

3738
/** Returns a cache optimized for this configuration. */
@@ -118,10 +119,9 @@ final class MethodHandleBasedFactory implements LocalCacheFactory {
118119
final MethodHandle methodHandle;
119120

120121
MethodHandleBasedFactory(Class<?> clazz) throws NoSuchMethodException, IllegalAccessException {
121-
var constructor = LOOKUP.findConstructor(clazz, FACTORY);
122-
this.methodHandle = constructor.asType(
123-
constructor.type().changeReturnType(BoundedLocalCache.class));
122+
this.methodHandle = LOOKUP.findConstructor(clazz, FACTORY).asType(FACTORY_CALL);
124123
}
124+
125125
@SuppressWarnings("unchecked")
126126
@Override public <K, V> BoundedLocalCache<K, V> newInstance(Caffeine<K, V> builder,
127127
@Nullable AsyncCacheLoader<? super K, V> cacheLoader, boolean async) throws Throwable {

caffeine/src/main/java/com/github/benmanes/caffeine/cache/NodeFactory.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ static NodeFactory<Object, Object> newFactory(String className) {
148148
try {
149149
var clazz = LOOKUP.findClass(Node.class.getPackageName() + "." + className);
150150
var constructor = LOOKUP.findConstructor(clazz, FACTORY);
151-
return (NodeFactory<Object, Object>) constructor
152-
.asType(constructor.type().changeReturnType(NodeFactory.class)).invokeExact();
151+
return (NodeFactory<Object, Object>) constructor.invoke();
153152
} catch (RuntimeException | Error e) {
154153
throw e;
155154
} catch (Throwable t) {

0 commit comments

Comments
 (0)