geforkt von Mirrors/Paper
c97ce029e9
PaperMC believes that 1.16.2 is now ready for general release as we fixed the main issue plagueing the 1.16.x release, the MapLike data conversion issues. Until now, it was not safe for a server to convert a world to 1.16.2 without data conversion issues around villages and potentially other things. If you did, those MapLike errors meant something went wrong. This is now resolved. Big thanks to all those that helped, notably @BillyGalbreath and @Proximyst who did large parts of the update process with me. Please as always, backup your worlds and test before updating to 1.16.2! If you update to 1.16.2, there is no going back to an older build than this. --------------------------------- Co-authored-by: William Blake Galbreath <Blake.Galbreath@GMail.com> Co-authored-by: Mariell Hoversholm <proximyst@proximyst.com> Co-authored-by: krolik-exe <69214078+krolik-exe@users.noreply.github.com> Co-authored-by: BillyGalbreath <BillyGalbreath@users.noreply.github.com> Co-authored-by: stonar96 <minecraft.stonar96@gmail.com> Co-authored-by: Shane Freeder <theboyetronic@gmail.com> Co-authored-by: Jason <jasonpenilla2@me.com> Co-authored-by: kashike <kashike@vq.lc> Co-authored-by: Aurora <21148213+aurorasmiles@users.noreply.github.com> Co-authored-by: KennyTV <kennytv@t-online.de> Co-authored-by: commandblockguy <commandblockguy1@gmail.com> Co-authored-by: DigitalRegent <misterwener@gmail.com> Co-authored-by: ishland <ishlandmc@yeah.net>
309 Zeilen
12 KiB
Diff
309 Zeilen
12 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Zach Brown <zach.brown@destroystokyo.com>
|
|
Date: Fri, 12 May 2017 23:34:11 -0500
|
|
Subject: [PATCH] Properly handle async calls to restart the server
|
|
|
|
The watchdog thread calls the server restart function asynchronously. Prior to
|
|
this change, it attempted to do several non-safe operations from the watchdog
|
|
thread, rather than the main. Specifically, because of a separate upstream change,
|
|
it causes player entities to be ticked asynchronously, among other things.
|
|
|
|
This is dangerous.
|
|
|
|
This patch moves the old handling into a synchronous variant, for calls from the
|
|
restart command, and adds separate handling for async calls, such as those from
|
|
the watchdog thread.
|
|
|
|
When calling from the watchdog thread, we cannot assume the main thread is in a
|
|
tickable state; it may be completely deadlocked. In order to handle this, we mark
|
|
the server as stopping, in order to account for situations where the server should
|
|
complete a tick reasonbly soon, i.e. 99% of cases.
|
|
|
|
Should the server not enter a state where it is stopping within 10 seconds, We
|
|
will assume that the server has in fact deadlocked and will proceed to force
|
|
kill the server.
|
|
|
|
This modification does not force restart the server should we actually enter a
|
|
deadlocked state where the server is stopping, whereas this will in most cases
|
|
exit within a reasonable amount of time, to put a fixed limit on a process that
|
|
will have plugins and worlds saving to the disk has a high potential to result
|
|
in corruption/dataloss.
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
index b9e757b94d7c00b8bea23102417b2afbe7eccd7a..42365b2f183a08e799533c1e9757cca13729d8fe 100644
|
|
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
@@ -91,6 +91,7 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
|
|
public final Map<ResourceKey<World>, WorldServer> worldServer;
|
|
private PlayerList playerList;
|
|
private volatile boolean isRunning;
|
|
+ private volatile boolean isRestarting = false; // Paper - flag to signify we're attempting to restart
|
|
private boolean isStopped;
|
|
private int ticks;
|
|
protected final Proxy proxy;
|
|
@@ -739,7 +740,7 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
|
|
if (this.playerList != null) {
|
|
MinecraftServer.LOGGER.info("Saving players");
|
|
this.playerList.savePlayers();
|
|
- this.playerList.shutdown();
|
|
+ this.playerList.shutdown(this.isRestarting); // Paper
|
|
try { Thread.sleep(100); } catch (InterruptedException ex) {} // CraftBukkit - SPIGOT-625 - give server at least a chance to send packets
|
|
}
|
|
|
|
@@ -804,8 +805,13 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
|
|
return this.isRunning;
|
|
}
|
|
|
|
+ // Paper start - allow passing of the intent to restart
|
|
public void safeShutdown(boolean flag) {
|
|
+ this.safeShutdown(flag, false);
|
|
+ }
|
|
+ public void safeShutdown(boolean flag, boolean isRestarting) {
|
|
this.isRunning = false;
|
|
+ this.isRestarting = isRestarting;
|
|
if (flag) {
|
|
try {
|
|
this.serverThread.join();
|
|
@@ -815,6 +821,7 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
|
|
}
|
|
|
|
}
|
|
+ // Paper end
|
|
|
|
// Spigot Start
|
|
private static double calcTps(double avg, double exp, double tps)
|
|
diff --git a/src/main/java/net/minecraft/server/PlayerList.java b/src/main/java/net/minecraft/server/PlayerList.java
|
|
index 8c2d49215f4ac4232cf5850b67fa5cc46477a9e9..6e4fb963cb5d8962a3021b1a614f81b08cb10bdb 100644
|
|
--- a/src/main/java/net/minecraft/server/PlayerList.java
|
|
+++ b/src/main/java/net/minecraft/server/PlayerList.java
|
|
@@ -1084,10 +1084,15 @@ public abstract class PlayerList {
|
|
entityplayer.playerInteractManager.b(worldserver.worldDataServer.getGameType()); // CraftBukkit
|
|
}
|
|
|
|
+ // Paper start - Extract method to allow for restarting flag
|
|
public void shutdown() {
|
|
+ this.shutdown(false);
|
|
+ }
|
|
+
|
|
+ public void shutdown(boolean isRestarting) {
|
|
// CraftBukkit start - disconnect safely
|
|
for (EntityPlayer player : this.players) {
|
|
- player.playerConnection.disconnect(this.server.server.getShutdownMessage()); // CraftBukkit - add custom shutdown message
|
|
+ player.playerConnection.disconnect(!isRestarting ? this.server.server.getShutdownMessage() : org.spigotmc.SpigotConfig.restartMessage); // CraftBukkit - add custom shutdown message // Paper - add isRestarting flag
|
|
}
|
|
// CraftBukkit end
|
|
|
|
@@ -1099,6 +1104,7 @@ public abstract class PlayerList {
|
|
}
|
|
// Paper end
|
|
}
|
|
+ // Paper end
|
|
|
|
// CraftBukkit start
|
|
public void sendMessage(IChatBaseComponent[] iChatBaseComponents) {
|
|
diff --git a/src/main/java/org/spigotmc/RestartCommand.java b/src/main/java/org/spigotmc/RestartCommand.java
|
|
index ccea803f58e09067cc998c62ffa134d6604878ff..aefea3a9a8b9b75c62bd20018be7cd166a213001 100644
|
|
--- a/src/main/java/org/spigotmc/RestartCommand.java
|
|
+++ b/src/main/java/org/spigotmc/RestartCommand.java
|
|
@@ -46,86 +46,134 @@ public class RestartCommand extends Command
|
|
org.spigotmc.AsyncCatcher.shuttingDown = true; // Paper
|
|
try
|
|
{
|
|
- String[] split = restartScript.split( " " );
|
|
- if ( split.length > 0 && new File( split[0] ).isFile() )
|
|
+ // Paper - extract method and cleanup
|
|
+ boolean isRestarting = addShutdownHook( restartScript );
|
|
+ if ( isRestarting )
|
|
{
|
|
- System.out.println( "Attempting to restart with " + restartScript );
|
|
+ System.out.println( "Attempting to restart with " + SpigotConfig.restartScript );
|
|
+ } else
|
|
+ {
|
|
+ System.out.println( "Startup script '" + SpigotConfig.restartScript + "' does not exist! Stopping server." );
|
|
+ }
|
|
+ // Stop the watchdog
|
|
+ WatchdogThread.doStop();
|
|
|
|
- // Disable Watchdog
|
|
- WatchdogThread.doStop();
|
|
+ shutdownServer( isRestarting );
|
|
+ // Paper end
|
|
+ } catch ( Exception ex )
|
|
+ {
|
|
+ ex.printStackTrace();
|
|
+ }
|
|
+ }
|
|
|
|
- // Kick all players
|
|
- for ( EntityPlayer p : (List< EntityPlayer>) MinecraftServer.getServer().getPlayerList().players )
|
|
- {
|
|
- p.playerConnection.disconnect(SpigotConfig.restartMessage);
|
|
- }
|
|
- // Give the socket a chance to send the packets
|
|
- try
|
|
- {
|
|
- Thread.sleep( 100 );
|
|
- } catch ( InterruptedException ex )
|
|
- {
|
|
- }
|
|
- // Close the socket so we can rebind with the new process
|
|
- MinecraftServer.getServer().getServerConnection().b();
|
|
+ // Paper start - sync copied from above with minor changes, async added
|
|
+ private static void shutdownServer(boolean isRestarting)
|
|
+ {
|
|
+ if ( MinecraftServer.getServer().isMainThread() )
|
|
+ {
|
|
+ // Kick all players
|
|
+ for ( EntityPlayer p : com.google.common.collect.ImmutableList.copyOf( MinecraftServer.getServer().getPlayerList().players ) )
|
|
+ {
|
|
+ p.playerConnection.disconnect(SpigotConfig.restartMessage);
|
|
+ }
|
|
+ // Give the socket a chance to send the packets
|
|
+ try
|
|
+ {
|
|
+ Thread.sleep( 100 );
|
|
+ } catch ( InterruptedException ex )
|
|
+ {
|
|
+ }
|
|
|
|
- // Give time for it to kick in
|
|
- try
|
|
- {
|
|
- Thread.sleep( 100 );
|
|
- } catch ( InterruptedException ex )
|
|
- {
|
|
- }
|
|
+ closeSocket();
|
|
|
|
- // Actually shutdown
|
|
- try
|
|
- {
|
|
- MinecraftServer.getServer().close();
|
|
- } catch ( Throwable t )
|
|
- {
|
|
- }
|
|
+ // Actually shutdown
|
|
+ try
|
|
+ {
|
|
+ MinecraftServer.getServer().close(); // calls stop()
|
|
+ } catch ( Throwable t )
|
|
+ {
|
|
+ }
|
|
+
|
|
+ // Actually stop the JVM
|
|
+ System.exit( 0 );
|
|
|
|
- // This will be done AFTER the server has completely halted
|
|
- Thread shutdownHook = new Thread()
|
|
+ } else
|
|
+ {
|
|
+ // Mark the server to shutdown at the end of the tick
|
|
+ MinecraftServer.getServer().safeShutdown( false, isRestarting );
|
|
+
|
|
+ // wait 10 seconds to see if we're actually going to try shutdown
|
|
+ try
|
|
+ {
|
|
+ Thread.sleep( 10000 );
|
|
+ }
|
|
+ catch (InterruptedException ignored)
|
|
+ {
|
|
+ }
|
|
+
|
|
+ // Check if we've actually hit a state where the server is going to safely shutdown
|
|
+ // if we have, let the server stop as usual
|
|
+ if (MinecraftServer.getServer().isStopped()) return;
|
|
+
|
|
+ // If the server hasn't stopped by now, assume worse case and kill
|
|
+ closeSocket();
|
|
+ System.exit( 0 );
|
|
+ }
|
|
+ }
|
|
+ // Paper end
|
|
+
|
|
+ // Paper - Split from moved code
|
|
+ private static void closeSocket()
|
|
+ {
|
|
+ // Close the socket so we can rebind with the new process
|
|
+ MinecraftServer.getServer().getServerConnection().b();
|
|
+
|
|
+ // Give time for it to kick in
|
|
+ try
|
|
+ {
|
|
+ Thread.sleep( 100 );
|
|
+ } catch ( InterruptedException ex )
|
|
+ {
|
|
+ }
|
|
+ }
|
|
+ // Paper end
|
|
+
|
|
+ // Paper start - copied from above and modified to return if the hook registered
|
|
+ private static boolean addShutdownHook(String restartScript)
|
|
+ {
|
|
+ String[] split = restartScript.split( " " );
|
|
+ if ( split.length > 0 && new File( split[0] ).isFile() )
|
|
+ {
|
|
+ Thread shutdownHook = new Thread()
|
|
+ {
|
|
+ @Override
|
|
+ public void run()
|
|
{
|
|
- @Override
|
|
- public void run()
|
|
+ try
|
|
{
|
|
- try
|
|
+ String os = System.getProperty( "os.name" ).toLowerCase(java.util.Locale.ENGLISH);
|
|
+ if ( os.contains( "win" ) )
|
|
{
|
|
- String os = System.getProperty( "os.name" ).toLowerCase(java.util.Locale.ENGLISH);
|
|
- if ( os.contains( "win" ) )
|
|
- {
|
|
- Runtime.getRuntime().exec( "cmd /c start " + restartScript );
|
|
- } else
|
|
- {
|
|
- Runtime.getRuntime().exec( "sh " + restartScript );
|
|
- }
|
|
- } catch ( Exception e )
|
|
+ Runtime.getRuntime().exec( "cmd /c start " + restartScript );
|
|
+ } else
|
|
{
|
|
- e.printStackTrace();
|
|
+ Runtime.getRuntime().exec( "sh " + restartScript );
|
|
}
|
|
+ } catch ( Exception e )
|
|
+ {
|
|
+ e.printStackTrace();
|
|
}
|
|
- };
|
|
-
|
|
- shutdownHook.setDaemon( true );
|
|
- Runtime.getRuntime().addShutdownHook( shutdownHook );
|
|
- } else
|
|
- {
|
|
- System.out.println( "Startup script '" + SpigotConfig.restartScript + "' does not exist! Stopping server." );
|
|
-
|
|
- // Actually shutdown
|
|
- try
|
|
- {
|
|
- MinecraftServer.getServer().close();
|
|
- } catch ( Throwable t )
|
|
- {
|
|
}
|
|
- }
|
|
- System.exit( 0 );
|
|
- } catch ( Exception ex )
|
|
+ };
|
|
+
|
|
+ shutdownHook.setDaemon( true );
|
|
+ Runtime.getRuntime().addShutdownHook( shutdownHook );
|
|
+ return true;
|
|
+ } else
|
|
{
|
|
- ex.printStackTrace();
|
|
+ return false;
|
|
}
|
|
}
|
|
+ // Paper end
|
|
+
|
|
}
|