From dfd19063f972b3542721e8a5328eb2342f9559d2 Mon Sep 17 00:00:00 2001 From: Aikar Date: Fri, 27 May 2016 22:28:23 -0400 Subject: [PATCH] Ensure chunks never load async Force operation to main thread if it occurs async --- Spigot-Server-Patches/0004-MC-Utils.patch | 39 ++++++++++++++-- ...-Ensure-Chunks-never-ever-load-async.patch | 46 +++++++++++++++++++ 2 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 Spigot-Server-Patches/0160-Ensure-Chunks-never-ever-load-async.patch diff --git a/Spigot-Server-Patches/0004-MC-Utils.patch b/Spigot-Server-Patches/0004-MC-Utils.patch index af58e328b..70251e249 100644 --- a/Spigot-Server-Patches/0004-MC-Utils.patch +++ b/Spigot-Server-Patches/0004-MC-Utils.patch @@ -1,4 +1,4 @@ -From e4c4535575c0397de1e8a388e09428dd60947d13 Mon Sep 17 00:00:00 2001 +From 9e9204318a93e93baf42cab944f182a7d8c38d78 Mon Sep 17 00:00:00 2001 From: Aikar Date: Mon, 28 Mar 2016 20:55:47 -0400 Subject: [PATCH] MC Utils @@ -7,18 +7,22 @@ Collection of utils to help reduce NMS diff diff --git a/src/main/java/net/minecraft/server/MCUtil.java b/src/main/java/net/minecraft/server/MCUtil.java new file mode 100644 -index 0000000..bcc8ee7 +index 0000000..f059bc2 --- /dev/null +++ b/src/main/java/net/minecraft/server/MCUtil.java -@@ -0,0 +1,154 @@ +@@ -0,0 +1,185 @@ +package net.minecraft.server; + +import com.google.common.util.concurrent.ThreadFactoryBuilder; +import org.bukkit.Location; ++import org.bukkit.craftbukkit.util.Waitable; ++import org.spigotmc.AsyncCatcher; + +import javax.annotation.Nullable; ++import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; ++import java.util.function.Supplier; +import java.util.regex.Pattern; + +public final class MCUtil { @@ -38,6 +42,33 @@ index 0000000..bcc8ee7 + } + + /** ++ * Ensures the target code is running on the main thread ++ * @param reason ++ * @param run ++ * @param ++ * @return ++ */ ++ public static T ensureMain(String reason, Supplier run) { ++ if (AsyncCatcher.enabled && Thread.currentThread() != MinecraftServer.getServer().primaryThread) { ++ new IllegalStateException( "Asynchronous " + reason + "! Blocking thread until it returns ").printStackTrace(); ++ Waitable wait = new Waitable() { ++ @Override ++ protected T evaluate() { ++ return run.get(); ++ } ++ }; ++ MinecraftServer.getServer().processQueue.add(wait); ++ try { ++ return wait.get(); ++ } catch (InterruptedException | ExecutionException e) { ++ e.printStackTrace(); ++ } ++ return null; ++ } ++ return run.get(); ++ } ++ ++ /** + * Calculates distance between 2 entities + * @param e1 + * @param e2 @@ -206,5 +237,5 @@ index 256614d..e86ca6c 100644 public NBTTagList() {} -- -2.8.2 +2.7.4 (Apple Git-66) diff --git a/Spigot-Server-Patches/0160-Ensure-Chunks-never-ever-load-async.patch b/Spigot-Server-Patches/0160-Ensure-Chunks-never-ever-load-async.patch new file mode 100644 index 000000000..21e287ce2 --- /dev/null +++ b/Spigot-Server-Patches/0160-Ensure-Chunks-never-ever-load-async.patch @@ -0,0 +1,46 @@ +From fe9ff05b1c9d84e8894f5034991ebdb21d35e783 Mon Sep 17 00:00:00 2001 +From: Aikar +Date: Fri, 27 May 2016 21:41:26 -0400 +Subject: [PATCH] Ensure Chunks never ever load async + +Safely pushes the operation to main thread, then back to the posting thread + +diff --git a/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOExecutor.java b/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOExecutor.java +index 7b7a3d0..9aaca21 100644 +--- a/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOExecutor.java ++++ b/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOExecutor.java +@@ -4,6 +4,7 @@ import com.destroystokyo.paper.PaperConfig; + import net.minecraft.server.Chunk; + import net.minecraft.server.ChunkProviderServer; + import net.minecraft.server.ChunkRegionLoader; ++import net.minecraft.server.MCUtil; // Paper + import net.minecraft.server.World; + import org.bukkit.craftbukkit.util.AsynchronousExecutor; + +@@ -14,7 +15,7 @@ public class ChunkIOExecutor { + private static final AsynchronousExecutor instance = new AsynchronousExecutor(new ChunkIOProvider(), BASE_THREADS); + + public static Chunk syncChunkLoad(World world, ChunkRegionLoader loader, ChunkProviderServer provider, int x, int z) { +- return instance.getSkipQueue(new QueuedChunk(x, z, loader, world, provider)); ++ return MCUtil.ensureMain("Async Chunk Load", () -> instance.getSkipQueue(new QueuedChunk(x, z, loader, world, provider))); // Paper + } + + public static void queueChunkLoad(World world, ChunkRegionLoader loader, ChunkProviderServer provider, int x, int z, Runnable runnable) { +diff --git a/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOProvider.java b/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOProvider.java +index 7752b50..4d2b371 100644 +--- a/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOProvider.java ++++ b/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOProvider.java +@@ -32,8 +32,8 @@ class ChunkIOProvider implements AsynchronousExecutor.CallBackProvider