From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Spottedleaf <spottedleaf@spottedleaf.dev>
Date: Thu, 10 Dec 2020 15:18:05 -0800
Subject: [PATCH] Improve abnormal server shutdown process

- When we're trying to kill the main thread from watchdog,
step up the stop() spamming after 15s to really kill the main thread.

- Do not wait for window disposing when disposing of the server
gui. It looks like during sigint shutdown there can be some
deadlock between the server thread and awt shutdown thread here.

diff --git a/src/main/java/net/minecraft/server/DedicatedServer.java b/src/main/java/net/minecraft/server/DedicatedServer.java
index 617ba6fb85c55967360ca270ee8feeff5acbf8e0..2644b190813cc934914aeab78fbd6515d1a37c4a 100644
--- a/src/main/java/net/minecraft/server/DedicatedServer.java
+++ b/src/main/java/net/minecraft/server/DedicatedServer.java
@@ -360,7 +360,7 @@ public class DedicatedServer extends MinecraftServer implements IMinecraftServer
         }
 
         if (this.q != null) {
-            this.q.b();
+            //this.q.b(); // Tuinity - do not wait for AWT, causes deadlock with sigint handler (AWT shutdown will properly clear our resources anyways)
         }
 
         if (this.remoteControlListener != null) {
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index 29e7b4aa6e94566d446221cd28c2f60fdb3fccab..7ac34a8959a797bf2af96f3f402fa65cffe3d666 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -756,10 +756,11 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
         org.spigotmc.WatchdogThread.doStop(); // Paper
         if (!isMainThread()) {
             MinecraftServer.LOGGER.info("Stopping main thread (Ignore any thread death message you see! - DO NOT REPORT THREAD DEATH TO PAPER)");
+            long start = System.nanoTime(); // Tuinity - really try hard to kill the main thread
             while (this.getThread().isAlive()) {
                 this.getThread().stop();
                 try {
-                    Thread.sleep(1);
+                    if ((System.nanoTime() - start) <= (15L * 1000L * 1000L * 1000L)) Thread.sleep(1); // Tuinity - really try hard to kill the main thread - if we're past 15s we're probably in a terrible loop, spam it to really kill it
                 } catch (InterruptedException e) {}
             }
             // We've just obliterated the main thread, this will prevent stop from dying when removing players
