From 0bf0c1b1a52530ef73d426ddd2fbe96315406207 Mon Sep 17 00:00:00 2001 From: Aaron <51387595+AzureAaron@users.noreply.github.com> Date: Thu, 5 Jun 2025 15:22:59 -0400 Subject: Improve scheduler thread safety to prevent crashes (#1306) Note: The code can't be put in another method otherwise the stack walker probably wont work correctly. --- .../de/hysky/skyblocker/utils/scheduler/Scheduler.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/main/java') diff --git a/src/main/java/de/hysky/skyblocker/utils/scheduler/Scheduler.java b/src/main/java/de/hysky/skyblocker/utils/scheduler/Scheduler.java index cca4da09..bc8abfbe 100644 --- a/src/main/java/de/hysky/skyblocker/utils/scheduler/Scheduler.java +++ b/src/main/java/de/hysky/skyblocker/utils/scheduler/Scheduler.java @@ -1,5 +1,6 @@ package de.hysky.skyblocker.utils.scheduler; +import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.brigadier.Command; import com.mojang.brigadier.context.CommandContext; import it.unimi.dsi.fastutil.ints.AbstractInt2ObjectMap; @@ -12,6 +13,7 @@ import net.minecraft.util.profiler.Profilers; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.lang.StackWalker.Option; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; @@ -54,6 +56,13 @@ public class Scheduler { * @param multithreaded whether to run the task on the schedulers dedicated thread pool */ public void schedule(Runnable task, int delay, boolean multithreaded) { + if (!RenderSystem.isOnRenderThread() && MinecraftClient.getInstance() != null) { + LOGGER.warn("[Skyblocker Scheduler] Called the scheduler from the {} class on the {} thread. This will be unsupported in the future.", StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE).getCallerClass().getName(), Thread.currentThread().getName()); + MinecraftClient.getInstance().send(() -> schedule(task, delay, multithreaded)); + + return; + } + if (delay >= 0) { addTask(multithreaded ? new ScheduledTask(task, true) : task, currentTick + delay); } else { @@ -69,6 +78,13 @@ public class Scheduler { * @param multithreaded whether to run the task on the schedulers dedicated thread pool */ public void scheduleCyclic(Runnable task, int period, boolean multithreaded) { + if (!RenderSystem.isOnRenderThread() && MinecraftClient.getInstance() != null) { + LOGGER.warn("[Skyblocker Scheduler] Called the scheduler from the {} class on the {} thread. This will be unsupported in the future.", StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE).getCallerClass().getName(), Thread.currentThread().getName()); + MinecraftClient.getInstance().send(() -> scheduleCyclic(task, period, multithreaded)); + + return; + } + if (period > 0) { addTask(new ScheduledTask(task, period, true, multithreaded), currentTick); } else { -- cgit