From 9cdaa5d8f0dca7968fc4a9e5a7fd6bb08fe317b1 Mon Sep 17 00:00:00 2001 From: Antti Koponen Date: Sat, 10 Oct 2026 02:18:41 +0300 Subject: [PATCH 1/2] Fix memory leaks in CacheHandler due to iteration failures --- .../net/coreprotect/thread/CacheHandler.java | 50 +++++++------------ 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/src/main/java/net/coreprotect/thread/CacheHandler.java b/src/main/java/net/coreprotect/thread/CacheHandler.java index 872aa89b7..eca7eac56 100755 --- a/src/main/java/net/coreprotect/thread/CacheHandler.java +++ b/src/main/java/net/coreprotect/thread/CacheHandler.java @@ -1,7 +1,5 @@ package net.coreprotect.thread; -import java.util.Collections; -import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; @@ -15,18 +13,18 @@ public class CacheHandler implements Runnable { - public static Map lookupCache = Collections.synchronizedMap(new HashMap<>()); - public static Map breakCache = Collections.synchronizedMap(new HashMap<>()); - public static Map interactCache = Collections.synchronizedMap(new HashMap<>()); - public static Map entityCache = Collections.synchronizedMap(new HashMap<>()); - public static ConcurrentHashMap pistonCache = new ConcurrentHashMap<>(16, 0.75f, 2); - public static ConcurrentHashMap spreadCache = new ConcurrentHashMap<>(16, 0.75f, 2); - public static ConcurrentHashMap containerDuplicateCache = new ConcurrentHashMap<>(16, 0.75f, 2); - public static ConcurrentHashMap flowDuplicateCache = new ConcurrentHashMap<>(16, 0.75f, 2); - public static ConcurrentHashMap bonemealDuplicateCache = new ConcurrentHashMap<>(16, 0.75f, 2); - public static ConcurrentHashMap entityKillDuplicateCache = new ConcurrentHashMap<>(16, 0.75f, 2); - public static ConcurrentHashMap redstoneCache = new ConcurrentHashMap<>(16, 0.75f, 2); - public static ConcurrentHashMap fallingBlockSpawnCache = new ConcurrentHashMap<>(16, 0.75f, 2); + public static Map lookupCache = new ConcurrentHashMap<>(); + public static Map breakCache = new ConcurrentHashMap<>(); + public static Map interactCache = new ConcurrentHashMap<>(); + public static Map entityCache = new ConcurrentHashMap<>(); + public static Map pistonCache = new ConcurrentHashMap<>(); + public static Map spreadCache = new ConcurrentHashMap<>(); + public static Map containerDuplicateCache = new ConcurrentHashMap<>(); + public static Map flowDuplicateCache = new ConcurrentHashMap<>(); + public static Map bonemealDuplicateCache = new ConcurrentHashMap<>(); + public static Map entityKillDuplicateCache = new ConcurrentHashMap<>(); + public static Map redstoneCache = new ConcurrentHashMap<>(); + public static Map fallingBlockSpawnCache = new ConcurrentHashMap<>(); private static volatile Thread thread; @@ -64,7 +62,7 @@ public static String locationKey(World world, int x, int y, int z) { return world.getUID() + ":" + x + ":" + y + ":" + z; } - public static boolean shouldSuppressRepeat(ConcurrentHashMap cache, String cacheId, int threshold, int resetWindowSeconds) { + public static boolean shouldSuppressRepeat(Map cache, String cacheId, int threshold, int resetWindowSeconds) { if (cache == null || cacheId == null || cacheId.isEmpty() || threshold <= 0) { return false; } @@ -156,25 +154,15 @@ public void run() { } int timestamp = (int) (System.currentTimeMillis() / 1000L) - scanTime; - Iterator iterator = cache.entrySet().iterator(); - while (iterator.hasNext()) { + cache.values().removeIf(value -> { try { - Map.Entry entry = iterator.next(); - Object[] data = (Object[]) entry.getValue(); + Object[] data = (Object[]) value; int time = (data[0] instanceof Long) ? (int) ((long) data[0] / 1000L) : (int) data[0]; - - if (time < timestamp) { - try { - iterator.remove(); - } - catch (Exception e) { - } - } - } - catch (Exception e) { - break; + return time < timestamp; + } catch (Exception e) { + return true; // malformed entry } - } + }); } } catch (InterruptedException e) { From 2b994eb8726c2338ee3577aac8cea62164044801 Mon Sep 17 00:00:00 2001 From: Antti Koponen Date: Wed, 14 Oct 2026 09:20:36 +0300 Subject: [PATCH 2/2] Revert type cleanup --- .../net/coreprotect/thread/CacheHandler.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/coreprotect/thread/CacheHandler.java b/src/main/java/net/coreprotect/thread/CacheHandler.java index eca7eac56..c5e6611b0 100755 --- a/src/main/java/net/coreprotect/thread/CacheHandler.java +++ b/src/main/java/net/coreprotect/thread/CacheHandler.java @@ -17,14 +17,14 @@ public class CacheHandler implements Runnable { public static Map breakCache = new ConcurrentHashMap<>(); public static Map interactCache = new ConcurrentHashMap<>(); public static Map entityCache = new ConcurrentHashMap<>(); - public static Map pistonCache = new ConcurrentHashMap<>(); - public static Map spreadCache = new ConcurrentHashMap<>(); - public static Map containerDuplicateCache = new ConcurrentHashMap<>(); - public static Map flowDuplicateCache = new ConcurrentHashMap<>(); - public static Map bonemealDuplicateCache = new ConcurrentHashMap<>(); - public static Map entityKillDuplicateCache = new ConcurrentHashMap<>(); - public static Map redstoneCache = new ConcurrentHashMap<>(); - public static Map fallingBlockSpawnCache = new ConcurrentHashMap<>(); + public static ConcurrentHashMap pistonCache = new ConcurrentHashMap<>(); + public static ConcurrentHashMap spreadCache = new ConcurrentHashMap<>(); + public static ConcurrentHashMap containerDuplicateCache = new ConcurrentHashMap<>(); + public static ConcurrentHashMap flowDuplicateCache = new ConcurrentHashMap<>(); + public static ConcurrentHashMap bonemealDuplicateCache = new ConcurrentHashMap<>(); + public static ConcurrentHashMap entityKillDuplicateCache = new ConcurrentHashMap<>(); + public static ConcurrentHashMap redstoneCache = new ConcurrentHashMap<>(); + public static ConcurrentHashMap fallingBlockSpawnCache = new ConcurrentHashMap<>(); private static volatile Thread thread; @@ -62,7 +62,7 @@ public static String locationKey(World world, int x, int y, int z) { return world.getUID() + ":" + x + ":" + y + ":" + z; } - public static boolean shouldSuppressRepeat(Map cache, String cacheId, int threshold, int resetWindowSeconds) { + public static boolean shouldSuppressRepeat(ConcurrentHashMap cache, String cacheId, int threshold, int resetWindowSeconds) { if (cache == null || cacheId == null || cacheId.isEmpty() || threshold <= 0) { return false; }