Paper/patches/server/0133-Properly-handle-async-calls-to-restart-the-server.patch
Jason Penilla 0fa2a949ae
Updated Upstream (Bukkit/CraftBukkit/Spigot)
Upstream has released updates that appear to apply and compile correctly.
This update has not been tested by PaperMC and as with ANY update, please do your own testing

Bukkit Changes:
8503c3c9 #621: Add HumanEntity#getItemInUse and Material#getSlipperiness
248deb09 #622: Add methods to check if item is the breed item for an entity
2ce691d8 Clarify Player#breakBlock only works for blocks in the same world
5dcdd48e SPIGOT-6514: Small Dripleaf block data is missing half property
cc9610b7 #619: Add Player#breakBlock()
862bc475 Fix bad merge of SPIGOT-6502 fix
989bb0c1 Downgrade SnakeYAML due to issues with comments parsing
1dff62ae Fix inverted visual fire docs

CraftBukkit Changes:
40caacc8 SPIGOT-6526: World entities are not populated when plugin onEnable is called
c9a92ad0 SPIGOT-6536: Marker position not set on spawn
20d3e57c #855: Add HumanEntity#getItemInUse and Material#getSlipperiness
d9c69b44 SPIGOT-6529: Fix BundleMeta#setItems
8bd43be5 SPIGOT-6535: PlayerGameModeChangeEvent event incorrectly reports old gamemode
4ece3ff3 #856: Add methods to check if item is the breed item for an entity
dd4bec5f Add additional validation to Player#breakBlock
bc835ae6 SPIGOT-6532: Fix Entity#setGlowing
384e116e Restore 1.16.5 behaviour of InventoryDragEvent being called even when a single item is 'dragged' to its own slot
b42e708c Fix new map colors rendering as transparent
cfe7fecf SPIGOT-6524: Inventory desync when InventoryClickEvent is cancelled
eeae1b19 SPIGOT-6522: ItemStack on cursor is always AIR
7490724d Fix missing PlayerEditBookEvent
06875f76 SPIGOT-6513: Placing ItemStack in Inventory causes InventoryAction.NOTHING
27835bde SPIGOT-6519: Fix end gateway teleports
4ac634ad SPIGOT-6515: "Un-waterlogging" throws UnsupportedOperationException in some cases
da425fa2 SPIGOT-6518: Anvils falling onto dripstone can sometimes crash server
50530da9 SPIGOT-6514: Small Dripleaf block data is missing half property
6fdecf20 #853: Implement Player#breakBlock()
4db9c49f SPIGOT-6510: Bukkit#createMap throws NullPointerException
89e2b127 SPIGOT-6517: Spider jockey crash on dripstone
cbf2f678 SPIGOT-6508: Rename conflicted getServer
74575d48 SPIGOT-6506: Fix crash with custom inventories
a3df386f Fix NPE with Entity.getNearbyEntities
d747f8ed Fix NPE with World.getNearbyEntities
4d2c7800 Fix second usage of worldGenSettings just in case
5182f923 SPIGOT-6504: Fix generating fresh worlds

Spigot Changes:
66f9d3c1 Rebuild patches
191e4971 Rebuild patches
a09c0bb6 Restore Spigot experience merging
2021-06-12 22:13:07 -07:00

291 lines
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 3e2955013ce75034170b5eeae60482e1c174769d..220ead6455587a07375b6e86913865c6b590a87d 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -233,6 +233,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
public final Map<ResourceKey<Level>, ServerLevel> levels;
private PlayerList playerList;
private volatile boolean running;
+ private volatile boolean isRestarting = false; // Paper - flag to signify we're attempting to restart
private boolean stopped;
private int tickCount;
protected final Proxy proxy;
@@ -917,7 +918,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
if (this.playerList != null) {
MinecraftServer.LOGGER.info("Saving players");
this.playerList.saveAll();
- this.playerList.removeAll();
+ this.playerList.removeAll(this.isRestarting); // Paper
try { Thread.sleep(100); } catch (InterruptedException ex) {} // CraftBukkit - SPIGOT-625 - give server at least a chance to send packets
}
@@ -983,6 +984,12 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
}
public void halt(boolean flag) {
+ // Paper start - allow passing of the intent to restart
+ this.safeShutdown(flag, false);
+ }
+ public void safeShutdown(boolean flag, boolean isRestarting) {
+ this.isRestarting = isRestarting;
+ // Paper end
this.running = false;
if (flag) {
try {
diff --git a/src/main/java/net/minecraft/server/players/PlayerList.java b/src/main/java/net/minecraft/server/players/PlayerList.java
index 3eaf106f8f17288857ce1a149d0366cf04235307..79ebb15bed6eec80c12c1020b2b6b07c758332aa 100644
--- a/src/main/java/net/minecraft/server/players/PlayerList.java
+++ b/src/main/java/net/minecraft/server/players/PlayerList.java
@@ -1133,8 +1133,15 @@ public abstract class PlayerList {
}
public void removeAll() {
+ // Paper start - Extract method to allow for restarting flag
+ this.removeAll(false);
+ }
+
+ public void removeAll(boolean isRestarting) {
+ // Paper end
// CraftBukkit start - disconnect safely
for (ServerPlayer player : this.players) {
+ if (isRestarting) player.connection.disconnect(org.spigotmc.SpigotConfig.restartMessage); else // Paper
player.connection.disconnect(this.server.server.shutdownMessage()); // CraftBukkit - add custom shutdown message // Paper - Adventure
}
// CraftBukkit end
diff --git a/src/main/java/org/spigotmc/RestartCommand.java b/src/main/java/org/spigotmc/RestartCommand.java
index 94d8ba376cd1f024b244654cac9bb62bb19e3060..a142a56a920e153ed84c08cece993f10d76f7793 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 ( ServerPlayer p : (List<ServerPlayer>) MinecraftServer.getServer().getPlayerList().players )
- {
- p.connection.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().getConnection().stop();
+ // Paper start - sync copied from above with minor changes, async added
+ private static void shutdownServer(boolean isRestarting)
+ {
+ if ( MinecraftServer.getServer().isSameThread() )
+ {
+ // Kick all players
+ for ( ServerPlayer p : com.google.common.collect.ImmutableList.copyOf( MinecraftServer.getServer().getPlayerList().players ) )
+ {
+ p.connection.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().getConnection().stop();
+
+ // 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
+
}