fix(app): Phase A.1 — pending-spawn list in GpuWorldState (proper fix)

Fifth and final Phase A.1 hotfix. Replaces the previous "drop on
miss" semantics in GpuWorldState.AppendLiveEntity with a per-landblock
pending bucket that survives the race where a CreateObject arrives
before its landblock has been streamed in.

Root cause:
The post-login spawn flood (40+ NPCs/items) drains in a single
WorldSession.Tick() call. The synchronous streamer enqueues all 25
visible-window landblocks in one shot but StreamingController.Tick
was capped at MaxCompletionsPerFrame=4, so only 4 landblocks landed
in GpuWorldState on the first frame. The center landblock 0xA9B4FFFF
may or may not have been in those first 4 (HashSet iteration order
is undefined). Spawns whose target landblock wasn't yet loaded were
silently dropped by AppendLiveEntity. Re-ordering the OnUpdate
(streaming first, live second) didn't fix it because the cap still
limited to 4 per frame; spawns for landblocks #5+ kept dropping
until the queue drained, by which point the spawn flood was over.

The reordering was correct but insufficient. The cap was a relic of
the original async streamer design (limit GPU upload spikes per
frame). With the synchronous streamer there's no backlog to spread,
so the cap was pure latency for no benefit. Setting it to int.MaxValue
restores "drain everything you just enqueued" semantics.

The pending-spawn list is the *correct* architecture fix that makes
the system robust against any future ordering bug, not just the cap:
- AppendLiveEntity for an unloaded landblock parks the entity in a
  per-landblock pending bucket instead of dropping it.
- AddLandblock drains pending entries for its landblock and merges
  them into the loaded record before storing.
- RemoveLandblock drops pending entries for the same landblock —
  if the player moved away, the spawns are no longer relevant; the
  server resends them via CreateObject when the player returns.

Diagnostic counter PendingLiveEntityCount exposes the bucket size
so future regressions are visible without spelunking.

7 new GpuWorldStateTests pin the contract:
- AppendLiveEntity_LandblockAlreadyLoaded_AppendsImmediately
- AppendLiveEntity_LandblockNotLoaded_ParksInPending
- AddLandblock_DrainsPendingEntriesForThatLandblock
- AddLandblock_DoesNotDrainPendingForADifferentLandblock
- RemoveLandblock_DropsPendingForThatLandblock
- RemoveLandblock_LoadedThenRemoved_DropsItsEntities
- IsLoaded_ReturnsTrueForLoaded_FalseForPendingOnly

Also removes the diagnostic Console.WriteLine I added in the previous
debugging round and the old LiveAppendsResolved/Dropped counters that
were never read by anyone.

219 tests green (212 + 7 new).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Erik 2026-04-11 23:19:40 +02:00
parent 4b01c95ecb
commit f792931d21
3 changed files with 250 additions and 22 deletions

View file

@ -24,7 +24,28 @@ public sealed class StreamingController
private StreamingRegion? _region;
public int Radius { get; set; }
public int MaxCompletionsPerFrame { get; set; } = 4;
/// <summary>
/// Cap on completions drained per <see cref="Tick"/> call. Defaults to
/// effectively unlimited because the current <c>LandblockStreamer</c>
/// is synchronous — every <c>EnqueueLoad</c> writes to the outbox on
/// the same thread, so by the time we drain there's no backlog
/// to spread, and the cap only serves to *delay* applying landblocks
/// the user is already trying to look at.
///
/// <para>
/// The original async design used a small cap (4) to limit per-frame
/// GPU upload spikes. That reasoning becomes relevant again if/when
/// the streamer moves back to async loading; lower this knob then.
/// Crucially, dropping completions to a lower frame is what was
/// silently breaking live spawns: the post-login spawn flood would
/// arrive on a frame where only 4 of the 25 visible-window landblocks
/// had been applied, the spawns for the other 21 hit
/// <c>AppendLiveEntity</c> with no matching loaded slot, and got
/// dropped (now: parked in the pending bucket).
/// </para>
/// </summary>
public int MaxCompletionsPerFrame { get; set; } = int.MaxValue;
public StreamingController(
Action<uint> enqueueLoad,