From c94cfa4291612e3395d269eaea2f439b5d9ae0da Mon Sep 17 00:00:00 2001
From: Jason Penilla <11360596+jpenilla@users.noreply.github.com>
Date: Fri, 4 Mar 2022 01:26:22 -0700
Subject: [PATCH] Fix deadlock on watchdog crash (#7535)

---
 .../Fix-deadlock-on-watchdog-crash.patch      | 21 +++++++++++++++++++
 1 file changed, 21 insertions(+)
 create mode 100644 patches/server/Fix-deadlock-on-watchdog-crash.patch

diff --git a/patches/server/Fix-deadlock-on-watchdog-crash.patch b/patches/server/Fix-deadlock-on-watchdog-crash.patch
new file mode 100644
index 0000000000..d439715e9b
--- /dev/null
+++ b/patches/server/Fix-deadlock-on-watchdog-crash.patch
@@ -0,0 +1,21 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: Jason Penilla <11360596+jpenilla@users.noreply.github.com>
+Date: Fri, 4 Mar 2022 00:12:49 -0700
+Subject: [PATCH] Fix deadlock on watchdog crash
+
+The watchdog thread sets MinecraftServer#forceTicks to true which will
+cause waitUntilNextTick to hang when the task queue is drained.
+
+diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
+index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
+--- a/src/main/java/net/minecraft/server/MinecraftServer.java
++++ b/src/main/java/net/minecraft/server/MinecraftServer.java
+@@ -0,0 +0,0 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
+                 }, false);
+             }
+ 
+-            this.waitUntilNextTick();
++            this.managedBlock(() -> !this.canSleepForTickNoOversleep() || this.getPendingTasksCount() == 0); // Paper - based on waitUntilNextTick
+         }
+ 
+         this.saveAllChunks(false, true, false);