From f819fd4d5e6c78a590cb4af6dc821f6070571e6d Mon Sep 17 00:00:00 2001 From: William Date: Fri, 19 Jul 2024 17:33:04 +0100 Subject: [PATCH] fix: Fabric thread exhaustion/deadlock causing crashes --- .../william278/husksync/util/FabricTask.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/fabric/src/main/java/net/william278/husksync/util/FabricTask.java b/fabric/src/main/java/net/william278/husksync/util/FabricTask.java index 73556db9..ae13cd84 100644 --- a/fabric/src/main/java/net/william278/husksync/util/FabricTask.java +++ b/fabric/src/main/java/net/william278/husksync/util/FabricTask.java @@ -19,18 +19,21 @@ package net.william278.husksync.util; +import com.google.common.util.concurrent.ThreadFactoryBuilder; import net.william278.husksync.FabricHuskSync; import net.william278.husksync.HuskSync; import net.william278.husksync.data.UserDataHolder; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.*; public interface FabricTask extends Task { + ScheduledExecutorService ASYNC_EXEC = Executors.newScheduledThreadPool(4, + new ThreadFactoryBuilder() + .setDaemon(true) + .setNameFormat("HuskSync-ThreadPool") + .build()); class Sync extends Task.Sync implements FabricTask { @@ -46,7 +49,7 @@ public interface FabricTask extends Task { @Override public void run() { if (!cancelled) { - Executors.newSingleThreadScheduledExecutor().schedule( + ASYNC_EXEC.schedule( () -> ((FabricHuskSync) getPlugin()).getMinecraftServer().executeSync(runnable), delayTicks * 50, TimeUnit.MILLISECONDS @@ -73,7 +76,7 @@ public interface FabricTask extends Task { @Override public void run() { if (!cancelled) { - this.task = CompletableFuture.runAsync(runnable, ((FabricHuskSync) getPlugin()).getMinecraftServer()); + this.task = CompletableFuture.runAsync(runnable, ASYNC_EXEC); } } } @@ -97,7 +100,7 @@ public interface FabricTask extends Task { @Override public void run() { if (!cancelled) { - this.task = Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate( + this.task = ASYNC_EXEC.scheduleAtFixedRate( runnable, 0, repeatingTicks * 50, @@ -129,7 +132,7 @@ public interface FabricTask extends Task { @Override default void cancelTasks() { - // Do nothing + ASYNC_EXEC.shutdownNow(); } }