Force exiting server on shutdown (#415)

This commit is contained in:
IzzelAliz 2021-12-14 14:33:33 +08:00
parent aba187d46e
commit 04859a24c4
No known key found for this signature in database
GPG Key ID: EE50E123A11D8338
3 changed files with 44 additions and 1 deletions

View File

@ -0,0 +1,18 @@
package io.izzel.arclight.common.mixin.bukkit;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.bukkit.craftbukkit.v.scheduler.CraftScheduler;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import java.util.concurrent.ThreadFactory;
@Mixin(value = CraftScheduler.class, remap = false)
public class CraftSchedulerMixin {
@Redirect(method = "<init>", at = @At(value = "INVOKE", target = "Lcom/google/common/util/concurrent/ThreadFactoryBuilder;build()Ljava/util/concurrent/ThreadFactory;"))
private ThreadFactory arclight$setDaemon(ThreadFactoryBuilder instance) {
return instance.setDaemon(true).build();
}
}

View File

@ -1,6 +1,7 @@
package io.izzel.arclight.common.mixin.core.server.dedicated;
import io.izzel.arclight.common.mixin.core.server.MinecraftServerMixin;
import io.izzel.arclight.common.mod.ArclightMod;
import io.izzel.arclight.common.mod.server.BukkitRegistry;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
@ -25,6 +26,8 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Mixin(DedicatedServer.class)
public abstract class DedicatedServerMixin extends MinecraftServerMixin {
@ -88,7 +91,28 @@ public abstract class DedicatedServerMixin extends MinecraftServerMixin {
} catch (IOException e) {
e.printStackTrace();
}
new Thread(() -> System.exit(0), "Exit Thread").start();
Thread exitThread = new Thread(this::arclight$exit, "Exit Thread");
exitThread.setDaemon(true);
exitThread.start();
}
private void arclight$exit() {
try {
Thread.sleep(5000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
List<String> threads = new ArrayList<>();
for (Thread thread : Thread.getAllStackTraces().keySet()) {
if (!thread.isDaemon() && !thread.getName().equals("DestroyJavaVM")) {
threads.add(thread.getName());
}
}
if (!threads.isEmpty()) {
ArclightMod.LOGGER.debug("Threads {} not shutting down", String.join(", ", threads));
ArclightMod.LOGGER.warn("{} threads not shutting down correctly, force exiting", threads.size());
}
System.exit(0);
}
/**

View File

@ -29,6 +29,7 @@
"CraftMetaItemMixin",
"CraftPlayerMixin",
"CraftRegionAccessorMixin",
"CraftSchedulerMixin",
"CraftServerMixin",
"CraftVillagerMixin",
"CraftWorldMixin",