From c2b2de43cf16fd418d799f61d8e3842180691570 Mon Sep 17 00:00:00 2001 From: Shane Freeder Date: Mon, 29 Jul 2024 16:10:53 +0100 Subject: [PATCH] Shutdown L4J cordially if the server stops before it's even started (#11172) primarily, this allows us to ensure that appenders are flushed, console, log files before the JVM exits to avoid issues such as error messages disappearing --- .../server/Improved-Watchdog-Support.patch | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/patches/server/Improved-Watchdog-Support.patch b/patches/server/Improved-Watchdog-Support.patch index f8e16778f6..7401a5daba 100644 --- a/patches/server/Improved-Watchdog-Support.patch +++ b/patches/server/Improved-Watchdog-Support.patch @@ -58,6 +58,38 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 // Many servers tend to restart at a fixed time at xx:00 which causes an uneven distribution of requests on the // bStats backend. To circumvent this problem, we introduce some randomness into the initial and second delay. +diff --git a/src/main/java/io/papermc/paper/util/LogManagerShutdownThread.java b/src/main/java/io/papermc/paper/util/LogManagerShutdownThread.java +new file mode 100644 +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 +--- /dev/null ++++ b/src/main/java/io/papermc/paper/util/LogManagerShutdownThread.java +@@ -0,0 +0,0 @@ ++package io.papermc.paper.util; ++ ++public class LogManagerShutdownThread extends Thread { ++ ++ static LogManagerShutdownThread INSTANCE = new LogManagerShutdownThread(); ++ public static final void hook() { ++ if (INSTANCE == null) { ++ throw new IllegalStateException("Cannot re-hook after being unhooked"); ++ } ++ Runtime.getRuntime().addShutdownHook(INSTANCE); ++ } ++ ++ public static final void unhook() { ++ Runtime.getRuntime().removeShutdownHook(INSTANCE); ++ INSTANCE = null; ++ } ++ ++ private LogManagerShutdownThread() { ++ super("Log4j2 Shutdown Thread"); ++ } ++ ++ @Override ++ public void run() { ++ org.apache.logging.log4j.LogManager.shutdown(); ++ } ++} diff --git a/src/main/java/net/minecraft/CrashReport.java b/src/main/java/net/minecraft/CrashReport.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/net/minecraft/CrashReport.java @@ -70,6 +102,18 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 while (cause instanceof CompletionException && cause.getCause() != null) { cause = cause.getCause(); } +diff --git a/src/main/java/net/minecraft/server/Main.java b/src/main/java/net/minecraft/server/Main.java +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 +--- a/src/main/java/net/minecraft/server/Main.java ++++ b/src/main/java/net/minecraft/server/Main.java +@@ -0,0 +0,0 @@ public class Main { + + @DontObfuscate + public static void main(final OptionSet optionset) { // CraftBukkit - replaces main(String[] astring) ++ io.papermc.paper.util.LogManagerShutdownThread.hook(); // Paper + SharedConstants.tryDetectVersion(); + /* CraftBukkit start - Replace everything + OptionParser optionparser = new OptionParser(); 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 @@ -93,6 +137,14 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 public static S spin(Function serverFactory) { AtomicReference atomicreference = new AtomicReference(); Thread thread = new ca.spottedleaf.moonrise.common.util.TickThread(() -> { // Paper - rewrite chunk system +@@ -0,0 +0,0 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop