A field guide to desyncs
When two clients disagree about the world, the bug is somewhere in a very long list. The list, ordered by how often I have found it, and the instrumentation that finds it in an hour.
What a desync is
Two clients running a deterministic simulation with the same inputs arrive at different states. From that tick on they diverge, quietly, until something visible happens: a unit that died on one screen is alive on another, a building stands in one world and not the other. By then the divergence is hundreds of ticks old and the cause is invisible. A desync is never found by looking at the symptom. It is found by knowing on which tick and in which system the states first differed, and everything below is about getting that information.
The instrumentation, before the list
Hash the simulation state at the end of every tick, exchange the hashes with the inputs, and stop the match the moment they differ. That gives the tick. Hash after every system inside the tick, keep the last few hundred ticks of per-system hashes in a ring buffer, and dump them on mismatch. That gives the system. Record inputs so the match can be replayed offline on both machines’ builds. That gives reproduction. With these three, most desyncs go from weeks to an hour, and without them they are the most expensive bugs a multiplayer project has.
The hash must cover all simulation state and nothing else. Presentation state in the hash produces false desyncs; simulation state missing from the hash produces silent ones. The hash is a contract about what the simulation is, and writing it is a useful audit on its own.
The list, most common first
Iteration order. A system whose result depends on the order it visits entities, and an order that differs between clients because their chunk layouts diverged. Symptom: the first system to differ is one with a tie-break, a “first found”, or an accumulation into a shared value. Fix: stable ids and explicit sorting wherever order matters.
Floating point. Different results for the same expression on different hardware or under different compiler settings. Symptom: the divergence starts in a math-heavy system and the difference is in the last bits. Fix: fixed-point for simulation state, or strict floating-point mode with no fast-math and no platform-dependent instructions, and no transcendental functions from platform libraries.
Uninitialised or stale data. A component that was never written on one client because a code path did not run, a buffer reused without clearing. Symptom: the divergence appears on spawn or on a rare event. Fix: initialise everything explicitly; treat “it was zero anyway” as a bug.
Random numbers. A simulation that draws from a generator that presentation also draws from, or that is seeded from something local. Symptom: divergence on the first tick that uses randomness. Fix: one simulation-owned generator advanced by the tick, and a linter rule that fails the build on any other random call in simulation code.
Time. Anything that reads wall-clock time, frame delta, or the frame count inside simulation. Symptom: divergence that depends on frame rate. Fix: the simulation knows ticks and nothing else.
Parallel reductions. A parallel job that combines results in completion order. Symptom: divergence in a system that sums, finds a maximum, or counts across entities. Fix: per-thread partials combined in thread-index order.
Structural changes at different times. An entity created or destroyed in the middle of a tick on one client and at the end on another, because of a code path that took a different branch. Symptom: entity counts differ. Fix: all structural changes through a command buffer played back at one sync point.
Hash mismatch that is not a desync. The hash includes something that legitimately differs, a cached value, a presentation field. Symptom: the states are actually identical when compared field by field. Fix: audit the hash.
Input delivery. Clients simulating tick N with different inputs because the transport delivered them in a different order or dropped one. Symptom: divergence exactly at a tick where an input arrived late. Fix: this is a transport bug, and the input log will show it.
Platform differences. Different behaviour of the same code on console versus PC, usually floating point or a library. Symptom: cross-platform matches desync, same-platform matches do not. Fix: the same as floating point, plus a cross-platform replay in the nightly build.
The nightly replay
A recorded match, replayed on every platform the game ships on, with the final hashes compared. It is the cheapest determinism test there is and the one that catches the regression before a player does. LEGION runs it on every merge, and the number of desyncs that reached a real match after it was in place is the reason I recommend it in every lockstep review.
When you cannot find it
Bisect the systems: hash after each one and narrow. Bisect the time: replay to the tick before divergence and compare field by field. Bisect the code: revert to the last commit where the replay passed. All three are mechanical once the instrumentation exists, and none of them are possible without it, which is why the instrumentation is the first thing I build and the first thing I look for.
If your lockstep game desyncs and the team is guessing, the netcode and transport review starts by building the three tools above against your code.