Memory leak with PathCache.cache growing due the map was not synchronized

closes #19096
This commit is contained in:
mposolda 2023-05-23 09:00:13 +02:00 committed by Pedro Igor
parent 827943571e
commit 1f5d3223ae

View file

@ -16,6 +16,7 @@
*/ */
package org.keycloak.adapters.authorization; package org.keycloak.adapters.authorization;
import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
@ -53,12 +54,12 @@ public class PathCache {
*/ */
PathCache(final int maxEntries, long maxAge, PathCache(final int maxEntries, long maxAge,
Map<String, PathConfig> paths) { Map<String, PathConfig> paths) {
cache = new LinkedHashMap<String, CacheEntry>(16, DEFAULT_LOAD_FACTOR, true) { cache = Collections.synchronizedMap(new LinkedHashMap<String, CacheEntry>(16, DEFAULT_LOAD_FACTOR, true) {
@Override @Override
protected boolean removeEldestEntry(Map.Entry eldest) { protected boolean removeEldestEntry(Map.Entry eldest) {
return cache.size() > maxEntries; return cache.size() > maxEntries;
} }
}; });
this.maxAge = maxAge; this.maxAge = maxAge;
this.enabled = ! (maxAge < -1 || (maxAge > -1 && maxAge <= 0)); this.enabled = ! (maxAge < -1 || (maxAge > -1 && maxAge <= 0));
this.paths = paths; this.paths = paths;