Folia Threading Error on teleportation
sofianedjerbi opened this issue ยท 3 comments
This stacktrace is created when teleporting off main thread
[16:11:52 INFO]: skoaboi issued server command: /co teleport wid:1 8950.5 6 6652.5
[16:11:52 ERROR]: Thread Region Scheduler Thread #4 failed main thread check: Scheduling chunk load off-main
java.lang.Throwable: null
at io.papermc.paper.util.TickThread.ensureTickThread(TickThread.java:71) ~[kaiiju-1.19.4.jar:git-Kaiiju-"74d3638"]
at net.minecraft.server.level.ServerChunkCache.getChunkFutureMainThread(ServerChunkCache.java:462) ~[?:?]
at net.minecraft.server.level.ServerChunkCache.getChunk(ServerChunkCache.java:390) ~[?:?]
at net.minecraft.world.level.chunk.ChunkSource.getChunk(ChunkSource.java:13) ~[?:?]
at org.bukkit.craftbukkit.v1_19_R3.CraftWorld.getChunkAt(CraftWorld.java:338) ~[kaiiju-1.19.4.jar:git-Kaiiju-"74d3638"]
at org.bukkit.craftbukkit.v1_19_R3.CraftWorld.getChunkAt(CraftWorld.java:958) ~[kaiiju-1.19.4.jar:git-Kaiiju-"74d3638"]
at net.coreprotect.command.TeleportCommand.runCommand(TeleportCommand.java:103) ~[CoreProtect-21.3.jar:?]
at net.coreprotect.command.CommandHandler.onCommand(CommandHandler.java:1232) ~[CoreProtect-21.3.jar:?]
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[kaiiju-api-1.19.4-R0.1-SNAPSHOT.jar:?]
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:155) ~[kaiiju-api-1.19.4-R0.1-SNAPSHOT.jar:?]
at org.bukkit.craftbukkit.v1_19_R3.CraftServer.dispatchCommand(CraftServer.java:1017) ~[kaiiju-1.19.4.jar:git-Kaiiju-"74d3638"]
at org.bukkit.craftbukkit.v1_19_R3.command.BukkitCommandWrapper.run(BukkitCommandWrapper.java:64) ~[kaiiju-1.19.4.jar:git-Kaiiju-"74d3638"]
at com.mojang.brigadier.CommandDispatcher.execute(CommandDispatcher.java:264) ~[kaiiju-1.19.4.jar:?]
at net.minecraft.commands.Commands.performCommand(Commands.java:322) ~[?:?]
at net.minecraft.commands.Commands.performCommand(Commands.java:306) ~[?:?]
at net.minecraft.server.network.ServerGamePacketListenerImpl.performChatCommand(ServerGamePacketListenerImpl.java:2325) ~[?:?]
at net.minecraft.server.network.ServerGamePacketListenerImpl.lambda$handleChatCommand$16(ServerGamePacketListenerImpl.java:2285) ~[?:?]
at io.papermc.paper.threadedregions.EntityScheduler.executeTick(EntityScheduler.java:173) ~[kaiiju-1.19.4.jar:git-Kaiiju-"74d3638"]
at net.minecraft.server.MinecraftServer.tickServer(MinecraftServer.java:1533) ~[kaiiju-1.19.4.jar:git-Kaiiju-"74d3638"]
at io.papermc.paper.threadedregions.TickRegions$ConcreteRegionTickHandle.tickRegion(TickRegions.java:360) ~[kaiiju-1.19.4.jar:git-Kaiiju-"74d3638"]
at io.papermc.paper.threadedregions.TickRegionScheduler$RegionScheduleHandle.runTick(TickRegionScheduler.java:385) ~[kaiiju-1.19.4.jar:git-Kaiiju-"74d3638"]
at ca.spottedleaf.concurrentutil.scheduler.SchedulerThreadPool$TickThreadRunner.run(SchedulerThreadPool.java:525) ~[kaiiju-1.19.4.jar:git-Kaiiju-"74d3638"]
at java.lang.Thread.run(Thread.java:833) ~[?:?]
[16:11:52 ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing command 'co' in plugin CoreProtect v21.3
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[kaiiju-api-1.19.4-R0.1-SNAPSHOT.jar:?]
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:155) ~[kaiiju-api-1.19.4-R0.1-SNAPSHOT.jar:?]
at org.bukkit.craftbukkit.v1_19_R3.CraftServer.dispatchCommand(CraftServer.java:1017) ~[kaiiju-1.19.4.jar:git-Kaiiju-"74d3638"]
at org.bukkit.craftbukkit.v1_19_R3.command.BukkitCommandWrapper.run(BukkitCommandWrapper.java:64) ~[kaiiju-1.19.4.jar:git-Kaiiju-"74d3638"]
at com.mojang.brigadier.CommandDispatcher.execute(CommandDispatcher.java:264) ~[kaiiju-1.19.4.jar:?]
at net.minecraft.commands.Commands.performCommand(Commands.java:322) ~[?:?]
at net.minecraft.commands.Commands.performCommand(Commands.java:306) ~[?:?]
at net.minecraft.server.network.ServerGamePacketListenerImpl.performChatCommand(ServerGamePacketListenerImpl.java:2325) ~[?:?]
at net.minecraft.server.network.ServerGamePacketListenerImpl.lambda$handleChatCommand$16(ServerGamePacketListenerImpl.java:2285) ~[?:?]
at io.papermc.paper.threadedregions.EntityScheduler.executeTick(EntityScheduler.java:173) ~[kaiiju-1.19.4.jar:git-Kaiiju-"74d3638"]
at net.minecraft.server.MinecraftServer.tickServer(MinecraftServer.java:1533) ~[kaiiju-1.19.4.jar:git-Kaiiju-"74d3638"]
at io.papermc.paper.threadedregions.TickRegions$ConcreteRegionTickHandle.tickRegion(TickRegions.java:360) ~[kaiiju-1.19.4.jar:git-Kaiiju-"74d3638"]
at io.papermc.paper.threadedregions.TickRegionScheduler$RegionScheduleHandle.runTick(TickRegionScheduler.java:385) ~[kaiiju-1.19.4.jar:git-Kaiiju-"74d3638"]
at ca.spottedleaf.concurrentutil.scheduler.SchedulerThreadPool$TickThreadRunner.run(SchedulerThreadPool.java:525) ~[kaiiju-1.19.4.jar:git-Kaiiju-"74d3638"]
at java.lang.Thread.run(Thread.java:833) ~[?:?]
Caused by: java.lang.IllegalStateException: Scheduling chunk load off-main
at io.papermc.paper.util.TickThread.ensureTickThread(TickThread.java:72) ~[kaiiju-1.19.4.jar:git-Kaiiju-"74d3638"]
at net.minecraft.server.level.ServerChunkCache.getChunkFutureMainThread(ServerChunkCache.java:462) ~[?:?]
at net.minecraft.server.level.ServerChunkCache.getChunk(ServerChunkCache.java:390) ~[?:?]
at net.minecraft.world.level.chunk.ChunkSource.getChunk(ChunkSource.java:13) ~[?:?]
at org.bukkit.craftbukkit.v1_19_R3.CraftWorld.getChunkAt(CraftWorld.java:338) ~[kaiiju-1.19.4.jar:git-Kaiiju-"74d3638"]
at org.bukkit.craftbukkit.v1_19_R3.CraftWorld.getChunkAt(CraftWorld.java:958) ~[kaiiju-1.19.4.jar:git-Kaiiju-"74d3638"]
at net.coreprotect.command.TeleportCommand.runCommand(TeleportCommand.java:103) ~[CoreProtect-21.3.jar:?]
at net.coreprotect.command.CommandHandler.onCommand(CommandHandler.java:1232) ~[CoreProtect-21.3.jar:?]
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[kaiiju-api-1.19.4-R0.1-SNAPSHOT.jar:?]
... 14 more
This is caused by
if (location.getWorld().isChunkLoaded(chunkX, chunkZ) == false) {
location.getWorld().getChunkAt(location);
}
Teleport.performSafeTeleport(((Player) player), location, true);
Which is run in the incorrect thread.
A fix will be to run this part of the code in a RegionScheduler at location
.
However, Folia does not provide any API to load chunks, the check will occur in an unloaded chunk which may lead to an unexpected behavior.
My suggestion is to SafeTeleport player only if the server does not run Folia.
If the server run Folia, just teleportAsync
the player.
This commit only fixes teleportation issues in loaded chunks in the current thread.
If a chunk is not already loaded in the current thread, CoreProtect will attempt to load it unsafely, resulting sometimes in errors and failed teleportation (executing Bukkit methods for regions that are loaded in another thread).
How to reproduce:
1/ Teleport in an unloaded chunk
2/ Break a block
3/ Teleport far away from this region, wait till chunks are unloaded
4/ Lookup your own action
5/ Teleport to the broken block location
6/ It fail
A potential fix is to:
1- make safeTeleportation
thread safe with RegionScheduler
: this change will be useful when Folia will add chunk loading API.
2- disable safeTeleportation
on Folia since we can't load chunk for the moment, even if safeTeleportation
is safe, it will throw errors.
If you feel like this fix is alright, i can create a PR.
Fixed here: #349