2021-06-11 14:02:28 +02:00
|
|
|
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
|
2024-04-12 21:14:06 +02:00
|
|
|
index b9c006bef0491c401ae3a447a3f67742a75cb74f..48513e585ce02c301983adbe1224e95ff7f9a345 100644
|
2021-06-11 14:02:28 +02:00
|
|
|
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
|
2023-12-05 22:34:01 +01:00
|
|
|
@@ -236,6 +236,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
|
2022-08-14 10:03:13 +02:00
|
|
|
private Map<ResourceKey<Level>, ServerLevel> levels;
|
2021-06-11 14:02:28 +02:00
|
|
|
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;
|
2023-12-05 22:34:01 +01:00
|
|
|
private int ticksUntilAutosave;
|
2024-01-25 10:54:46 +01:00
|
|
|
@@ -919,7 +920,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
|
2021-06-11 14:02:28 +02:00
|
|
|
if (this.playerList != null) {
|
|
|
|
MinecraftServer.LOGGER.info("Saving players");
|
|
|
|
this.playerList.saveAll();
|
|
|
|
- this.playerList.removeAll();
|
2021-06-12 11:01:04 +02:00
|
|
|
+ this.playerList.removeAll(this.isRestarting); // Paper
|
2021-06-11 14:02:28 +02:00
|
|
|
try { Thread.sleep(100); } catch (InterruptedException ex) {} // CraftBukkit - SPIGOT-625 - give server at least a chance to send packets
|
|
|
|
}
|
|
|
|
|
2024-01-25 10:54:46 +01:00
|
|
|
@@ -999,6 +1000,12 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
|
2021-06-11 14:02:28 +02:00
|
|
|
}
|
|
|
|
|
2022-12-07 19:52:24 +01:00
|
|
|
public void halt(boolean waitForShutdown) {
|
2021-06-12 11:01:04 +02:00
|
|
|
+ // Paper start - allow passing of the intent to restart
|
2022-12-07 19:52:24 +01:00
|
|
|
+ this.safeShutdown(waitForShutdown, false);
|
2021-06-11 14:02:28 +02:00
|
|
|
+ }
|
2022-12-07 19:52:24 +01:00
|
|
|
+ public void safeShutdown(boolean waitForShutdown, boolean isRestarting) {
|
2021-06-11 14:02:28 +02:00
|
|
|
+ this.isRestarting = isRestarting;
|
2021-06-12 11:01:04 +02:00
|
|
|
+ // Paper end
|
|
|
|
this.running = false;
|
2022-12-07 19:52:24 +01:00
|
|
|
if (waitForShutdown) {
|
2021-06-11 14:02:28 +02:00
|
|
|
try {
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/players/PlayerList.java b/src/main/java/net/minecraft/server/players/PlayerList.java
|
Updated Upstream (Bukkit/CraftBukkit/Spigot) (#10277)
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:
9a80d38c SPIGOT-336, SPIGOT-3366, SPIGOT-5768, SPIGOT-6409, SPIGOT-6861, PR-722: Add EntityRemoveEvent
258086d9 SPIGOT-7417, PR-967: Add Sign#getTargetSide and Sign#getAllowedEditor
ffaba051 SPIGOT-7584: Add missing Tag.ITEMS_NON_FLAMMABLE_WOOD
CraftBukkit Changes:
98b6c1ac7 SPIGOT-7589 Fix NullPointerException when bans expire
a2736ddb0 SPIGOT-336, SPIGOT-3366, SPIGOT-5768, SPIGOT-6409, SPIGOT-6861, PR-1008: Add EntityRemoveEvent
5bf12cb89 SPIGOT-7565: Throw a more descriptive error message when a developer tries to spawn an entity from a CraftBukkit class
76d95fe7e SPIGOT-7417, PR-1343: Add Sign#getTargetSide and Sign#getAllowedEditor
Spigot Changes:
e9ec5485 Rebuild patches
f1b62e0c Rebuild patches
2024-02-23 14:37:33 +01:00
|
|
|
index e45568112220b246adf3efdc43d26258336efbf9..1c313dbfc501ce5782fb2d7500b9af35f0855c46 100644
|
2021-06-11 14:02:28 +02:00
|
|
|
--- a/src/main/java/net/minecraft/server/players/PlayerList.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/players/PlayerList.java
|
Updated Upstream (Bukkit/CraftBukkit/Spigot) (#10277)
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:
9a80d38c SPIGOT-336, SPIGOT-3366, SPIGOT-5768, SPIGOT-6409, SPIGOT-6861, PR-722: Add EntityRemoveEvent
258086d9 SPIGOT-7417, PR-967: Add Sign#getTargetSide and Sign#getAllowedEditor
ffaba051 SPIGOT-7584: Add missing Tag.ITEMS_NON_FLAMMABLE_WOOD
CraftBukkit Changes:
98b6c1ac7 SPIGOT-7589 Fix NullPointerException when bans expire
a2736ddb0 SPIGOT-336, SPIGOT-3366, SPIGOT-5768, SPIGOT-6409, SPIGOT-6861, PR-1008: Add EntityRemoveEvent
5bf12cb89 SPIGOT-7565: Throw a more descriptive error message when a developer tries to spawn an entity from a CraftBukkit class
76d95fe7e SPIGOT-7417, PR-1343: Add Sign#getTargetSide and Sign#getAllowedEditor
Spigot Changes:
e9ec5485 Rebuild patches
f1b62e0c Rebuild patches
2024-02-23 14:37:33 +01:00
|
|
|
@@ -1176,8 +1176,15 @@ public abstract class PlayerList {
|
2021-06-11 14:02:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void removeAll() {
|
2021-06-12 11:01:04 +02:00
|
|
|
+ // Paper start - Extract method to allow for restarting flag
|
|
|
|
+ this.removeAll(false);
|
2021-06-11 14:02:28 +02:00
|
|
|
+ }
|
|
|
|
+
|
2021-06-12 11:01:04 +02:00
|
|
|
+ public void removeAll(boolean isRestarting) {
|
|
|
|
+ // Paper end
|
2021-06-11 14:02:28 +02:00
|
|
|
// 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
|
2023-12-05 18:20:55 +01:00
|
|
|
index 1e251d0f6de25a0a8c739c5f18ec5b949d4fc396..051b9e3a5d29a5840d596468e3ddd013bedc8da3 100644
|
2021-06-11 14:02:28 +02:00
|
|
|
--- a/src/main/java/org/spigotmc/RestartCommand.java
|
|
|
|
+++ b/src/main/java/org/spigotmc/RestartCommand.java
|
2023-09-17 00:54:33 +02:00
|
|
|
@@ -45,86 +45,134 @@ public class RestartCommand extends Command
|
|
|
|
AsyncCatcher.enabled = false; // Disable async catcher incase it interferes with us
|
2021-06-11 14:02:28 +02:00
|
|
|
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 );
|
2023-03-23 22:57:03 +01:00
|
|
|
+ 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();
|
|
|
|
|
2021-06-11 14:02:28 +02:00
|
|
|
- // Disable Watchdog
|
|
|
|
- WatchdogThread.doStop();
|
2023-03-23 22:57:03 +01:00
|
|
|
+ shutdownServer( isRestarting );
|
|
|
|
+ // Paper end
|
|
|
|
+ } catch ( Exception ex )
|
|
|
|
+ {
|
|
|
|
+ ex.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
2021-06-11 14:02:28 +02:00
|
|
|
- // 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 )
|
|
|
|
+ {
|
|
|
|
+ }
|
2023-03-23 22:57:03 +01:00
|
|
|
|
|
|
|
- // Give time for it to kick in
|
|
|
|
- try
|
|
|
|
- {
|
|
|
|
- Thread.sleep( 100 );
|
|
|
|
- } catch ( InterruptedException ex )
|
|
|
|
- {
|
|
|
|
- }
|
2021-06-11 14:02:28 +02:00
|
|
|
+ closeSocket();
|
2023-03-23 22:57:03 +01:00
|
|
|
|
|
|
|
- // Actually shutdown
|
|
|
|
- try
|
|
|
|
- {
|
|
|
|
- MinecraftServer.getServer().close();
|
|
|
|
- } catch ( Throwable t )
|
|
|
|
- {
|
|
|
|
- }
|
2021-06-11 14:02:28 +02:00
|
|
|
+ // Actually shutdown
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ MinecraftServer.getServer().close(); // calls stop()
|
|
|
|
+ } catch ( Throwable t )
|
|
|
|
+ {
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Actually stop the JVM
|
|
|
|
+ System.exit( 0 );
|
2023-03-23 22:57:03 +01:00
|
|
|
|
|
|
|
- // This will be done AFTER the server has completely halted
|
|
|
|
- Thread shutdownHook = new Thread()
|
2021-06-11 14:02:28 +02:00
|
|
|
+ } 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()
|
2023-03-23 22:57:03 +01:00
|
|
|
{
|
|
|
|
- @Override
|
|
|
|
- public void run()
|
2021-06-11 14:02:28 +02:00
|
|
|
+ try
|
2023-03-23 22:57:03 +01:00
|
|
|
{
|
|
|
|
- try
|
2021-06-11 14:02:28 +02:00
|
|
|
+ String os = System.getProperty( "os.name" ).toLowerCase(java.util.Locale.ENGLISH);
|
|
|
|
+ if ( os.contains( "win" ) )
|
2023-03-23 22:57:03 +01:00
|
|
|
{
|
|
|
|
- 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 )
|
2021-06-11 14:02:28 +02:00
|
|
|
+ Runtime.getRuntime().exec( "cmd /c start " + restartScript );
|
|
|
|
+ } else
|
2023-03-23 22:57:03 +01:00
|
|
|
{
|
|
|
|
- e.printStackTrace();
|
2021-06-11 14:02:28 +02:00
|
|
|
+ Runtime.getRuntime().exec( "sh " + restartScript );
|
2023-03-23 22:57:03 +01:00
|
|
|
}
|
2021-06-11 14:02:28 +02:00
|
|
|
+ } catch ( Exception e )
|
|
|
|
+ {
|
|
|
|
+ e.printStackTrace();
|
2023-03-23 22:57:03 +01:00
|
|
|
}
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- 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 )
|
2021-06-11 14:02:28 +02:00
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ shutdownHook.setDaemon( true );
|
|
|
|
+ Runtime.getRuntime().addShutdownHook( shutdownHook );
|
|
|
|
+ return true;
|
|
|
|
+ } else
|
2023-03-23 22:57:03 +01:00
|
|
|
{
|
|
|
|
- ex.printStackTrace();
|
2021-06-11 14:02:28 +02:00
|
|
|
+ return false;
|
2023-03-23 22:57:03 +01:00
|
|
|
}
|
|
|
|
}
|
2021-06-11 14:02:28 +02:00
|
|
|
+ // Paper end
|
|
|
|
+
|
|
|
|
}
|