From 67a25de20218fa3fc2f1c407ae07b83350f4dc7a 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 2290e2ef85..c11939ccae 100644 --- a/src/main/java/ac/grim/grimac/manager/CheckManager.java +++ b/src/main/java/ac/grim/grimac/manager/CheckManager.java @@ -47,8 +47,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; @@ -336,7 +339,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(); @@ -349,7 +357,5 @@ private void init() { } } } - - inited = true; } }