From 2b5016442b74f238d70ae872e09d0d4949ecae2a Mon Sep 17 00:00:00 2001 From: Cristian Milani <111324740+crimilo@users.noreply.github.com> Date: Thu, 21 Nov 2024 00:26:33 +0100 Subject: [PATCH] Fix race condition and exception when registering check permissions during rapid consecutive player joins on server start --- src/main/java/ac/grim/grimac/manager/CheckManager.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/ac/grim/grimac/manager/CheckManager.java b/src/main/java/ac/grim/grimac/manager/CheckManager.java index 5e0519f879..51a8f36d0e 100644 --- a/src/main/java/ac/grim/grimac/manager/CheckManager.java +++ b/src/main/java/ac/grim/grimac/manager/CheckManager.java @@ -48,8 +48,11 @@ import org.bukkit.permissions.Permission; import org.bukkit.permissions.PermissionDefault; +import java.util.concurrent.atomic.AtomicBoolean; + public class CheckManager { private static boolean inited; + private static final AtomicBoolean initedAtomic = new AtomicBoolean(false); ClassToInstanceMap packetChecks; ClassToInstanceMap positionCheck; @@ -364,7 +367,12 @@ public T getPostPredictionCheck(Class check) } private void init() { + // Fast non-thread safe check if (inited) return; + // Slow thread safe check + if (!initedAtomic.compareAndSet(false, true)) return; + inited = true; + for (AbstractCheck check : allChecks.values()) { if (check.getCheckName() != null) { String permissionName = "grim.exempt." + check.getCheckName().toLowerCase(); @@ -377,7 +385,5 @@ private void init() { } } } - - inited = true; } }