Skip to content

Commit 40402e4

Browse files
committed
Adhere to new ServerPlayer lifespan from Paper
The ServerPlayer may be created multiple times for a single login if the player is entered into configuration mode. As a result, we need make the following changes to become compliant: 1. Retire the player scheduler when they are removed from the world, rather than on the connection close 2. Change thread check logic on common disconnect to no longer require a ServerPlayer instance Fixes #378
1 parent 9d308fa commit 40402e4

File tree

2 files changed

+26
-39
lines changed

2 files changed

+26
-39
lines changed

folia-server/minecraft-patches/features/0001-Region-Threading-Base.patch

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,7 +1557,7 @@ index 0000000000000000000000000000000000000000..a1043c426d031755b57b77a9b2eec685
15571557
\ No newline at end of file
15581558
diff --git a/io/papermc/paper/threadedregions/RegionizedServer.java b/io/papermc/paper/threadedregions/RegionizedServer.java
15591559
new file mode 100644
1560-
index 0000000000000000000000000000000000000000..b362fe60c94d3ea05addd8f093046dfcc3164c45
1560+
index 0000000000000000000000000000000000000000..e1842ab8f20271a8b20ab8462b09a4745a612a9a
15611561
--- /dev/null
15621562
+++ b/io/papermc/paper/threadedregions/RegionizedServer.java
15631563
@@ -0,0 +1,469 @@
@@ -1915,7 +1915,7 @@ index 0000000000000000000000000000000000000000..b362fe60c94d3ea05addd8f093046dfc
19151915
+ }
19161916
+
19171917
+ if (conn.getPacketListener() instanceof net.minecraft.server.network.ServerConfigurationPacketListenerImpl configurationPacketListener) {
1918-
+ return configurationPacketListener.switchToMain;
1918+
+ return configurationPacketListener.switchToMain != null;
19191919
+ }
19201920
+
19211921
+ return false;
@@ -12755,10 +12755,10 @@ index 11fed81a4696ba18440e755c3b8a5ca39ed8a6b1..45d6a95196a791d366791e02d23c53b8
1275512755

1275612756
public boolean isOldChunkAround(ChunkPos pos, int radius) {
1275712757
diff --git a/net/minecraft/server/network/ServerCommonPacketListenerImpl.java b/net/minecraft/server/network/ServerCommonPacketListenerImpl.java
12758-
index 08c59d603fca038fc2dde36384eea1b6c971e659..2c71556899986a481b62c46dd2a2fa0f4c6df308 100644
12758+
index 08c59d603fca038fc2dde36384eea1b6c971e659..714813cba67da4a737682fe9276cb0d96fea336d 100644
1275912759
--- a/net/minecraft/server/network/ServerCommonPacketListenerImpl.java
1276012760
+++ b/net/minecraft/server/network/ServerCommonPacketListenerImpl.java
12761-
@@ -85,12 +85,29 @@ public abstract class ServerCommonPacketListenerImpl implements ServerCommonPack
12761+
@@ -85,8 +85,19 @@ public abstract class ServerCommonPacketListenerImpl implements ServerCommonPack
1276212762
}
1276312763
}
1276412764

@@ -12778,17 +12778,7 @@ index 08c59d603fca038fc2dde36384eea1b6c971e659..2c71556899986a481b62c46dd2a2fa0f
1277812778
if (this.isSingleplayerOwner()) {
1277912779
LOGGER.info("Stopping singleplayer server as player logged out");
1278012780
this.server.halt(false);
12781-
}
12782-
+ // Folia start - region threading
12783-
+ if (!(this instanceof ServerGamePacketListenerImpl gamePacketListener)) {
12784-
+ throw new UnsupportedOperationException("Configuration mode is not supported in its current state");
12785-
+ }
12786-
+ gamePacketListener.player.getBukkitEntity().taskScheduler.retire();
12787-
+ // Folia end - region threading
12788-
}
12789-
12790-
@Override
12791-
@@ -350,24 +367,13 @@ public abstract class ServerCommonPacketListenerImpl implements ServerCommonPack
12781+
@@ -350,24 +361,19 @@ public abstract class ServerCommonPacketListenerImpl implements ServerCommonPack
1279212782
if (this.processedDisconnect) {
1279312783
return;
1279412784
}
@@ -12811,12 +12801,18 @@ index 08c59d603fca038fc2dde36384eea1b6c971e659..2c71556899986a481b62c46dd2a2fa0f
1281112801
- throw new RuntimeException(e);
1281212802
- }
1281312803
+ // Folia start - region threading
12814-
+ if (!(this instanceof ServerGamePacketListenerImpl gamePacketListener)) {
12815-
+ throw new UnsupportedOperationException("Configuration mode is not supported in its current state");
12804+
+ boolean onMain;
12805+
+ if (this instanceof ServerGamePacketListenerImpl serverGamePacketListener) {
12806+
+ onMain = ca.spottedleaf.moonrise.common.util.TickThread.isTickThreadFor(serverGamePacketListener.player);
12807+
+ } else if (this instanceof ServerConfigurationPacketListenerImpl configurationPacketListener) {
12808+
+ net.minecraft.server.level.ServerPlayer player = configurationPacketListener.switchToMain;
12809+
+ onMain = player == null ? io.papermc.paper.threadedregions.RegionizedServer.isGlobalTickThread() : ca.spottedleaf.moonrise.common.util.TickThread.isTickThreadFor(player);
12810+
+ } else {
12811+
+ onMain = io.papermc.paper.threadedregions.RegionizedServer.isGlobalTickThread();
1281612812
+ }
12817-
+ // Folia end - region threading
12818-
+ if (!ca.spottedleaf.moonrise.common.util.TickThread.isTickThreadFor(gamePacketListener.player)) { // Folia - region threading
12819-
+ this.connection.disconnectSafely(disconnectionDetails); // Folia - region threading - it HAS to be delayed/async to avoid deadlock if we try to wait for another region
12813+
+ if (!onMain) {
12814+
+ this.connection.disconnectSafely(disconnectionDetails); // it HAS to be delayed/async to avoid deadlock if we try to wait for another region
12815+
+ // Folia end - region threading
1282012816
return;
1282112817
}
1282212818

@@ -12830,14 +12826,14 @@ index 08c59d603fca038fc2dde36384eea1b6c971e659..2c71556899986a481b62c46dd2a2fa0f
1283012826

1283112827
// Paper start - add proper async disconnect
1283212828
diff --git a/net/minecraft/server/network/ServerConfigurationPacketListenerImpl.java b/net/minecraft/server/network/ServerConfigurationPacketListenerImpl.java
12833-
index ea892dd1d23d0f16c11381d1e8d75164a2791a3d..a3ccce0ed014c7a81fd68b1c11e4d2328325576a 100644
12829+
index ea892dd1d23d0f16c11381d1e8d75164a2791a3d..2c1930ec25f0e0bc66f967f05515838cad6cf066 100644
1283412830
--- a/net/minecraft/server/network/ServerConfigurationPacketListenerImpl.java
1283512831
+++ b/net/minecraft/server/network/ServerConfigurationPacketListenerImpl.java
1283612832
@@ -48,6 +48,7 @@ public class ServerConfigurationPacketListenerImpl extends ServerCommonPacketLis
1283712833
@Nullable
1283812834
private SynchronizeRegistriesTask synchronizeRegistriesTask;
1283912835
public io.papermc.paper.connection.PaperPlayerConfigurationConnection paperConnection; // Paper
12840-
+ public boolean switchToMain = false; // Folia - region threading - rewrite login process
12836+
+ public ServerPlayer switchToMain; // Folia - region threading - rewrite login process
1284112837

1284212838
public ServerConfigurationPacketListenerImpl(MinecraftServer server, Connection connection, CommonListenerCookie cookie) {
1284312839
super(server, connection, cookie);
@@ -12898,14 +12894,14 @@ index ea892dd1d23d0f16c11381d1e8d75164a2791a3d..a3ccce0ed014c7a81fd68b1c11e4d232
1289812894
+ ca.spottedleaf.concurrentutil.util.Priority.HIGHER
1289912895
+ );
1290012896
+ });
12901-
+ this.switchToMain = true;
12897+
+ this.switchToMain = serverPlayer;
1290212898
+ try {
1290312899
+ // now the connection responsibility is transferred on the region
1290412900
+ playerList.loadSpawnForNewPlayer(this.connection, serverPlayer, clientData, scopedCollector, data, lastKnownName, toComplete);
1290512901
+ } catch (final Throwable throwable) {
1290612902
+ // assume toComplete will not be invoked
1290712903
+ // ensure global tick thread owns the connection again, to properly disconnect it
12908-
+ this.switchToMain = false;
12904+
+ this.switchToMain = null;
1290912905
+ throw new RuntimeException(throwable);
1291012906
+ }
1291112907
+ // Folia end - region threading - rewrite login process
@@ -12943,7 +12939,7 @@ index bd07e6a5aa1883786d789ea71711a0c0c0a95c26..09469ad131622158fe5579216fc41642
1294312939
}
1294412940
// Spigot end
1294512941
diff --git a/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/net/minecraft/server/network/ServerGamePacketListenerImpl.java
12946-
index 5bba860a5f913d3a83c1d469f0f4854b6e063a49..05fe39bda63d78b6c8fcfb9dbbab20e79e2a7f8e 100644
12942+
index 5bba860a5f913d3a83c1d469f0f4854b6e063a49..736cefb8dd20b93995ee3acf8e8671e5c8fb4d8b 100644
1294712943
--- a/net/minecraft/server/network/ServerGamePacketListenerImpl.java
1294812944
+++ b/net/minecraft/server/network/ServerGamePacketListenerImpl.java
1294912945
@@ -287,10 +287,10 @@ public class ServerGamePacketListenerImpl
@@ -13181,7 +13177,7 @@ index 5bba860a5f913d3a83c1d469f0f4854b6e063a49..05fe39bda63d78b6c8fcfb9dbbab20e7
1318113177
// Paper start - Fix kick event leave message not being sent
1318213178
final net.kyori.adventure.text.Component quitMessage = details.quitMessage().map(io.papermc.paper.adventure.PaperAdventure::asAdventure).orElse(null);
1318313179
- this.removePlayerFromWorld(quitMessage);
13184-
+ if (!this.waitingForSwitchToConfig) this.removePlayerFromWorld(quitMessage); // Folia - region threading
13180+
+ if (!this.waitingForSwitchToConfig) this.removePlayerFromWorld(quitMessage); // Folia - region threading - don't double remove
1318513181
// Paper end - Fix kick event leave message not being sent
1318613182
super.onDisconnect(details);
1318713183
}
@@ -13236,7 +13232,7 @@ index 5bba860a5f913d3a83c1d469f0f4854b6e063a49..05fe39bda63d78b6c8fcfb9dbbab20e7
1323613232
+ // removed from the world
1323713233
+ // Folia end - rewrite login process - fix bad ordering of this field write - move after removed from world
1323813234
+ try { // Folia - rewrite login process - move connection ownership to global region
13239-
+ this.hackSwitchingConfig = true; // Folia - rewrite login process - avoid adding logout ticket here and retiring scheduler
13235+
+ this.hackSwitchingConfig = true; // Folia - rewrite login process - avoid adding logout ticket here
1324013236
this.removePlayerFromWorld();
1324113237
+ } finally { // Folia start - rewrite login process - move connection ownership to global region
1324213238
+ io.papermc.paper.threadedregions.RegionizedWorldData worldData = this.player.level().getCurrentWorldData();
@@ -13444,7 +13440,7 @@ index 194082763f41a58dc8ee0241a2eeb6dd39dff88d..484d3a17d22a2237e6102fde56890df9
1344413440
date = defaultValue;
1344513441
}
1344613442
diff --git a/net/minecraft/server/players/PlayerList.java b/net/minecraft/server/players/PlayerList.java
13447-
index dea72819f2933e5a806bb3a0603e4ebdf7f99456..fd8531f26ab14cfc5dc28128862684fba98f0910 100644
13443+
index dea72819f2933e5a806bb3a0603e4ebdf7f99456..da0efd01e0cd59dee8856779a89962c12e1e7603 100644
1344813444
--- a/net/minecraft/server/players/PlayerList.java
1344913445
+++ b/net/minecraft/server/players/PlayerList.java
1345013446
@@ -109,10 +109,10 @@ public abstract class PlayerList {
@@ -13649,15 +13645,6 @@ index dea72819f2933e5a806bb3a0603e4ebdf7f99456..fd8531f26ab14cfc5dc28128862684fb
1364913645
final net.minecraft.world.scores.Scoreboard scoreBoard = this.server.getLevel(Level.OVERWORLD).getScoreboard();
1365013646
final PlayerTeam team = scoreBoard.getPlayersTeam(this.collideRuleTeamName);
1365113647
if (player.getTeam() == team && team != null) {
13652-
@@ -573,7 +647,7 @@ public abstract class PlayerList {
13653-
}
13654-
13655-
serverLevel.removePlayerImmediately(player, Entity.RemovalReason.UNLOADED_WITH_PLAYER);
13656-
- player.retireScheduler(); // Paper - Folia schedulers
13657-
+ //player.retireScheduler(); // Paper - Folia schedulers // Folia - region threading - move to onDisconnect of common packet listener
13658-
player.getAdvancements().stopListening();
13659-
this.players.remove(player);
13660-
this.playersByName.remove(player.getScoreboardName().toLowerCase(java.util.Locale.ROOT)); // Spigot
1366113648
@@ -591,8 +665,7 @@ public abstract class PlayerList {
1366213649
// CraftBukkit start
1366313650
// this.broadcastAll(new ClientboundPlayerInfoRemovePacket(List.of(player.getUUID())));

folia-server/paper-patches/features/0001-Region-Threading-Base.patch

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Subject: [PATCH] Region Threading Base
55

66

77
diff --git a/src/main/java/ca/spottedleaf/moonrise/common/util/TickThread.java b/src/main/java/ca/spottedleaf/moonrise/common/util/TickThread.java
8-
index 69cdd304d255d52c9b7dc9b6a33ffdb630b79abe..c95769a4e64fabd7acdff6c5f6f349107e1cf5c0 100644
8+
index 69cdd304d255d52c9b7dc9b6a33ffdb630b79abe..e417c9a140778954572fb8bc190fad64feacffe0 100644
99
--- a/src/main/java/ca/spottedleaf/moonrise/common/util/TickThread.java
1010
+++ b/src/main/java/ca/spottedleaf/moonrise/common/util/TickThread.java
1111
@@ -1,5 +1,11 @@
@@ -175,7 +175,7 @@ index 69cdd304d255d52c9b7dc9b6a33ffdb630b79abe..c95769a4e64fabd7acdff6c5f6f34910
175175
+ return gamePacketListener.waitingForSwitchToConfig;
176176
+ }
177177
+ if (packetListener instanceof net.minecraft.server.network.ServerConfigurationPacketListenerImpl configurationPacketListener) {
178-
+ return !configurationPacketListener.switchToMain;
178+
+ return configurationPacketListener.switchToMain == null;
179179
+ }
180180
+ return true;
181181
+ } else {

0 commit comments

Comments
 (0)