Skip to content

Check idle players on the MicroServer that ticks them, not on main - #28

Merged
GrowlyX merged 1 commit into
masterfrom
perf/idle-checks-off-main
Nov 16, 2026
Merged

GrowlyX merged 1 commit into
masterfrom
perf/idle-checks-off-main

Conversation

@GrowlyX

@GrowlyX GrowlyX commented Nov 16, 2026

Copy link
Copy Markdown

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.run took 34 of 511 samples (6.7% of main, ≈0.12 ms/tick):

Frame Samples
UserConnectionImpl.get:114 (the MovementTracker lookup for every connection) 12
IdlePacketTask.run itself and the connection-set iteration 13
BukkitViaMovementTransmitter.sendPlayer:115 → Bukkit.getPlayer → CraftServer.getPlayer 5
sendPlayer:119 → IdleTickDeferral.tick/flush → MicroServerLoops.ownerOf → CraftEntity.getWorld 4

#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

  • Game loops check their own players. Every game MicroServer runs the idle check for its own players (MicroServer#getPlayers) at the start of each of its ticks, through runTaskTimer(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.
    • The conditions are the same as the task's: an active connection, a MovementTracker, and an idle packet that is due (OwnerLoopIdleChecks.dueTracker).
    • The tick is the same PlayerConnection#a(PacketPlayInFlying), on the thread that owns the player's world.
  • No Bukkit.getPlayer. The check starts from the loop's Player and looks up the connection by the same UUID (ConnectionManager#getServerConnection).
  • What main still does: IdlePacketTask asks the provider first (MovementTransmitterProvider#runIdlePass, replacing Run idle player ticks on the MicroServer that ticks the player's world #27's afterIdlePass). On MicroServers, main's pass only:
    • schedules the check on any game loop that doesn't have it yet (O(loops), an identity-set hit per loop per tick);
    • checks the players in the worlds main ticks itself, through the global domain's getWorlds() and their players. It deliberately avoids the global domain's getPlayers(), which walks every player on the server.
  • Removed: IdleTickDeferral and the now-unused MicroServersSupport.gameLoopOf/execute. Nothing is deferred any more.

Why it's safe

  • One thread per player. On Blueprint, worlds and players only change hands in main's exclusive tick (adopt/release/joins/quits/cross-world moves), while no game loop runs. So at any moment a player is in exactly one domain's getPlayers(), 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.
  • Loop lifecycle.
    • A new loop gets its check from main's next pass, at most one tick later. Run idle player ticks on the MicroServer that ticks the player's world #27 had the same one-tick latency for every tick.
    • A loop that stops drops its timers (MicroTaskQueue.close), and its worlds go back to main's pass.
    • The loop task also returns unless it is on that loop's own thread.
    • Stopped loops are pruned from the scheduled set.
  • Tracker races are unchanged. The tracker is still shared with the connection's netty thread, as it was with main.
  • Failure fallback. If any reflective call into MicroServers fails, the loops stop checking, one warning is logged, and IdlePacketTask walks the connections on main again. Main is exclusive, so that fallback is safe.
  • Unchanged elsewhere. Off Blueprint (Spigot/Paper), with MicroServers disabled, with nms-player-ticking: false, or with -Dviaversion.idleTicksOnOwnerLoop=false, runIdlePass returns false and the task runs its usual pass.
  • Separate resolution. The new reflective lookups are resolved in their own holder (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.
    • bukkit: 18 → 23 tests. IdleTickDeferralTest (5) was replaced by OwnerLoopIdleChecksTest (10).
    • common: 115 tests, including the new IdlePacketTaskTest (2).
  • OwnerLoopIdleChecksTest covers:
    • main checks only its own worlds' players;
    • each loop checks its own players on its own thread every tick, and is scheduled once;
    • a loop created later is picked up by the next main pass;
    • a player moving between loops is checked by the new owner only;
    • a pass off the loop's thread does nothing;
    • a loop that stopped before scheduling is left to main;
    • stopped loops are pruned;
    • a failed call hands everything back to the task and reports once;
    • dueTracker matches 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 the runIdlePass check (verified by removing the check).
  • Not yet measured live. After deploy, a fresh main-thread JFR should show IdlePacketTask.run at ~0, plus OwnerLoopIdleChecks.loopPass on the "MicroServer - game-N" threads.

The prod jar (5.12.2-arch.13, sha256 d6b3e1a0…) matches master structurally. Its class list differs from this build only by the IdleTickDeferral* → OwnerLoopIdleChecks* swap.

🤖 Generated with Claude Code

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)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 · 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

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.

@loopyreviews-bot

Copy link
Copy Markdown

Loopy review

Confidence: 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. IdlePacketTask asks MovementTransmitterProvider#runIdlePass first and falls back to its own walk over all connections when that returns false.

  • Adds OwnerLoopIdleChecks, which holds the scheduling, failure fallback and dueTracker logic. Adds MicroServersSupport.Loops and helpers, with reflective lookups resolved apart from Domains.
  • Removes IdleTickDeferral, its test, MicroServersSupport.gameLoopOf/execute and afterIdlePass. No code still references them. The remaining execute calls are on netty event loops and executors, not the removed methods.
  • BukkitViaMovementTransmitter wires in runIdlePass and checkIdle. checkIdle looks up the connection by UUID and ticks through PlayerTicker. MicroServerLoops now implements OwnerLoopIdleChecks.Domains.
  • New tests: OwnerLoopIdleChecksTest and IdlePacketTaskTest.
    I found nothing to report in my files. I read the code but did not run the tests or build.

Open findings (1)

Finding Location
P2 Ownership check failure escapes the fallback bukkit/src/main/java/com/viaversion/viaversion/bukkit/providers/OwnerLoopIdleChecks.java:130

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 0fa31a5 · harness claude/claude-sonnet-5-5 · Reply @loopyreviews-bot review to re-run · @loopyreviews-bot remember <rule> to teach Loopy

@GrowlyX
GrowlyX merged commit 1d2838b into master Nov 16, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant