fix: Fabric thread exhaustion/deadlock causing crashes

feat/data-edit-commands
William 4 months ago
parent e7659255fe
commit f819fd4d5e
No known key found for this signature in database

@ -19,18 +19,21 @@
package net.william278.husksync.util; package net.william278.husksync.util;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import net.william278.husksync.FabricHuskSync; import net.william278.husksync.FabricHuskSync;
import net.william278.husksync.HuskSync; import net.william278.husksync.HuskSync;
import net.william278.husksync.data.UserDataHolder; import net.william278.husksync.data.UserDataHolder;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public interface FabricTask extends Task { 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 { class Sync extends Task.Sync implements FabricTask {
@ -46,7 +49,7 @@ public interface FabricTask extends Task {
@Override @Override
public void run() { public void run() {
if (!cancelled) { if (!cancelled) {
Executors.newSingleThreadScheduledExecutor().schedule( ASYNC_EXEC.schedule(
() -> ((FabricHuskSync) getPlugin()).getMinecraftServer().executeSync(runnable), () -> ((FabricHuskSync) getPlugin()).getMinecraftServer().executeSync(runnable),
delayTicks * 50, delayTicks * 50,
TimeUnit.MILLISECONDS TimeUnit.MILLISECONDS
@ -73,7 +76,7 @@ public interface FabricTask extends Task {
@Override @Override
public void run() { public void run() {
if (!cancelled) { 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 @Override
public void run() { public void run() {
if (!cancelled) { if (!cancelled) {
this.task = Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate( this.task = ASYNC_EXEC.scheduleAtFixedRate(
runnable, runnable,
0, 0,
repeatingTicks * 50, repeatingTicks * 50,
@ -129,7 +132,7 @@ public interface FabricTask extends Task {
@Override @Override
default void cancelTasks() { default void cancelTasks() {
// Do nothing ASYNC_EXEC.shutdownNow();
} }
} }

Loading…
Cancel
Save