Repository navigation
Check idle players on the MicroServer that ticks them, not on main - #28
Conversation
On Blueprint the main thread's tick is exclusive, and the idle packet task still walked every connection in it every tick: at 455 players that was 34 of 511 samples (6.7%, about 0.12 ms per tick) in a 60 s profile of legacy-fluid's main thread, in UserConnectionImpl.get, the loop itself, Bukkit.getPlayer and the hand-off's ownerOf, even though #27 already handed the ticks themselves to the game loops. Every game loop now runs the idle check for its own players at the start of each of its ticks, where #27's handed-over ticks ran: the same conditions (active connection, a MovementTracker, its idle packet due) and the same tick, on the thread that owns the player's world. The check starts from the loop's players and looks up the connection by UUID, so there is no Bukkit.getPlayer. The main thread's pass only schedules the check on loops that do not have it yet and checks the players in the worlds it ticks itself. Players and worlds only change hands in the main thread's exclusive tick, so each player is checked by one thread at a time. A loop that stops drops its check and its worlds go back to the main thread. If a call into MicroServers fails, the loops stop checking and the idle packet task walks the connections again, as before. Off Blueprint, with MicroServers disabled, in packet mode or with -Dviaversion.idleTicksOnOwnerLoop=false, the task runs its own pass as it did before #27. IdleTickDeferral and afterIdlePass are gone with nothing left to defer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| /** {@code loop}'s pass over its own players, at the start of each of its ticks. */ | ||
| void loopPass(final Object loop) { | ||
| // the main thread checks everyone once this has failed; a task the loop hands on when it stops is not its tick | ||
| if (failed.get() || !domains.isOwnedByCurrentThread(loop)) { |
There was a problem hiding this comment.
·
error-handling · Ownership check failure escapes the fallback
In loopPass, domains.isOwnedByCurrentThread(loop) sits outside the try, so a reflective failure (MicroServersSupport.invoke throws RuntimeException, or the (Boolean) cast hits null) never reaches fail(). The exception goes into the game loop's timer task every tick, and the main thread never gets the idle check back. Players in that loop then stop getting idle packets, while the loop is logged or disrupted each tick. Move the call inside the try.
Trace
bukkit/src/main/java/com/viaversion/viaversion/bukkit/providers/OwnerLoopIdleChecks.java:130changedisOwnedByCurrentThreadbukkit/src/main/java/com/viaversion/viaversion/bukkit/providers/BukkitViaMovementTransmitter.java:250callsisOwnedByCurrentThreadbukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/MicroServersSupport.java:141callsinvoke
Prompt for AI agents
In ruleyourowngame/ViaVersion PR #28, file bukkit/src/main/java/com/viaversion/viaversion/bukkit/providers/OwnerLoopIdleChecks.java lines 130-130:
Ownership check failure escapes the fallback. In `loopPass`, `domains.isOwnedByCurrentThread(loop)` sits outside the `try`, so a reflective failure (`MicroServersSupport.invoke` throws `RuntimeException`, or the `(Boolean)` cast hits null) never reaches `fail()`. The exception goes into the game loop's timer task every tick, and the main thread never gets the idle check back. Players in that loop then stop getting idle packets, while the loop is logged or disrupted each tick. Move the call inside the `try`.
Trace: bukkit/src/main/java/com/viaversion/viaversion/bukkit/providers/OwnerLoopIdleChecks.java:130 (changed) -> bukkit/src/main/java/com/viaversion/viaversion/bukkit/providers/BukkitViaMovementTransmitter.java:250 (calls) -> bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/MicroServersSupport.java:141 (calls).
Verify this is a real problem; if so, fix it with a minimal change and add a regression test.
Loopy reviewConfidence: 4/5 — Only P2 findings are open. Reviewed 9 of 9 changed files. Checked 2 of 2 leads Loopy raised; 0 became findings. The change moves the 1.8→1.9 idle-packet check off the main thread on Blueprint MicroServers. Each game loop now checks its own players at the start of each tick. Main only schedules that check on any loop that doesn't have it yet and checks the players in the worlds main ticks itself.
Open findings (1)
Review passes: 4 ran (general ×2, failure-paths, contracts) · 2 planned unit and lens pair(s) left out by the budget. Reviews: 1 · last reviewed |
Problem
legacy-fluid's main thread ("Server Thread - ES") runs an exclusive tick on Blueprint MicroServers: every game loop waits while it runs. In a 60 s JFR of that thread at 455 players,
IdlePacketTask.runtook 34 of 511 samples (6.7% of main, ≈0.12 ms/tick):UserConnectionImpl.get:114(theMovementTrackerlookup for every connection)IdlePacketTask.runitself and the connection-set iterationBukkitViaMovementTransmitter.sendPlayer:115→Bukkit.getPlayer→CraftServer.getPlayersendPlayer:119→IdleTickDeferral.tick/flush→MicroServerLoops.ownerOf→CraftEntity.getWorld#27 already hands the idle tick to the player's game loop, but main still walks every connection every tick to decide who is due.
Change
MicroServer#getPlayers) at the start of each of its ticks, throughrunTaskTimer(task, 1, 1). That is the slot where Run idle player ticks on the MicroServer that ticks the player's world #27's handed-over ticks ran: queued tasks first, then the players' packets.MovementTracker, and an idle packet that is due (OwnerLoopIdleChecks.dueTracker).PlayerConnection#a(PacketPlayInFlying), on the thread that owns the player's world.Bukkit.getPlayer. The check starts from the loop'sPlayerand looks up the connection by the same UUID (ConnectionManager#getServerConnection).IdlePacketTaskasks the provider first (MovementTransmitterProvider#runIdlePass, replacing Run idle player ticks on the MicroServer that ticks the player's world #27'safterIdlePass). On MicroServers, main's pass only:getWorlds()and their players. It deliberately avoids the global domain'sgetPlayers(), which walks every player on the server.IdleTickDeferraland the now-unusedMicroServersSupport.gameLoopOf/execute. Nothing is deferred any more.Why it's safe
adopt/release/joins/quits/cross-world moves), while no game loop runs. So at any moment a player is in exactly one domain'sgetPlayers(), and only that thread checks them. A player who moves is checked by the new owner from its next tick. Their tracker was already advanced, so the move can't double-tick them beyond the usual catch-up.MicroTaskQueue.close), and its worlds go back to main's pass.IdlePacketTaskwalks the connections on main again. Main is exclusive, so that fallback is safe.nms-player-ticking: false, or with-Dviaversion.idleTicksOnOwnerLoop=false,runIdlePassreturns false and the task runs its usual pass.MicroServersSupport.Loops). A Blueprint without them loses only this feature, not the listener/ack support.Expected saving
About 33 of the 34 samples leave main: ≈6.5% of main, ≈0.12 ms/tick at 455 players. Since main's tick is exclusive, that also comes off the time every game loop waits. Main keeps 3–4 reflective calls per tick plus a scan of the world list. The per-player work (≈41 players per loop) now runs on the 11 game loops in parallel.
Testing
./gradlew build(JDK 21): all green.IdleTickDeferralTest(5) was replaced byOwnerLoopIdleChecksTest(10).IdlePacketTaskTest(2).OwnerLoopIdleChecksTestcovers:dueTrackermatches the task's conditions (no connection, no tracker, not due, due, inactive).IdlePacketTaskTest: the task ticks due connections by default and skips its walk when the provider ran its own pass. It fails without therunIdlePasscheck (verified by removing the check).IdlePacketTask.runat ~0, plusOwnerLoopIdleChecks.loopPasson the "MicroServer - game-N" threads.The prod jar (
5.12.2-arch.13, sha256d6b3e1a0…) matches master structurally. Its class list differs from this build only by theIdleTickDeferral*→OwnerLoopIdleChecks*swap.🤖 Generated with Claude Code