Troubleshooting desyncs (desync debugger)
Desyncs are repaired automatically first
When the two peers' states differ, the hash check finds it (in real time on every confirmed tick, in turn-based play at the end of every turn). The host's (peer 0) state is then sent to the guest and replaces its state (resync), so the match goes on.
- Real time:
detphys_hasrt_ frame. flags DETPHYS_(found) andRT_ FRAME_ DESYNC DETPHYS_(the guest was replaced). Counts are inRT_ FRAME_ RESYNCED detphys_:rt_ get_ stats hashes_,mismatched desyncs,resyncs. - Turn-based: the turn's result has
DETPHYS_andTURN_ FLAG_ DESYNC DETPHYS_inTURN_ FLAG_ RESYNCED flags.
A desync is a sign that a rule (4. Determinism rules) is broken somewhere. The desync debugger points at the place in one go. (If the peers do not connect at all, the problem comes before any desync; see When peers do not connect.)
Turning the desync debugger on
It is off by default. When off, nothing is recorded or sent (the bytes on the wire, the frame time and the hashes are the same as without it).
detphys_debug_session_config dbg;
detphys_debug_session_config_default(&dbg);
dbg.enabled = 1; // turn it on on both peers
dbg.ticks_before = 30; // range to exchange: from 30 ticks before the desync tick T up to T (default)
detphys_rt_set_debug(session, &dbg); // turn-based: detphys_turn_set_debug(session, &dbg)Setting (detphys_) | Default | Meaning |
|---|---|---|
enabled | 0 | 1 = record, and exchange recordings on a desync |
flags | DETPHYS_ | Also record Box2D's deep hash (REC_BOX2D_HASH; Box3D's state hash in 3D) and body types, shapes and tags (REC_META). Without them recording costs about half |
ticks_before | 30 | How many ticks before the desync tick T to exchange |
ring_ticks | 0 (auto) | Length of the recording ring. Auto: real time = ticks_before + prediction window + 1 + 2 seconds; turn-based = ticks_before + turn limit + 2 |
max_bodies | 0 (auto) | Bodies per tick that fit in a recording. When too small it doubles and starts over (earlier recordings are lost) |
max_reports | 4 | Maximum number of exchanges (later desyncs are only counted) |
timeout_ms | 10000 | How long to wait for the other peer's recording (FAILED here if the other peer has it off) |
scene_id / label | 0 / empty | Marks on the recording (0 = hash of the session settings, empty = "host" / "guest") |
- Turn it on on both peers. If the other peer has it off, its recording never arrives and the report fails after
timeout_ms. - It can be called at any time (called at tick 0 it records from the state right after creation).
- Engine layers: in Unreal, use the sessions'
SetDebug,GetDesyncReportCountandGetDesyncReport(from Blueprint too;FDetPhysDebugSettings,FDetPhysDesyncReport). Unity uses the session'sSetDebug,TryGetDebugReportandDebugBundle; Godot uses the real-time session'sset_debug,get_debug_reportandget_debug_bundle(7. API reference). - 3D: in 3D sessions (
detphys_,rt_ create3d detphys_) turn it on the same way withturn_ create_ rule3d detphys_/rt_ set_ debug detphys_(with a core whereturn_ set_ debug detphys_is 3 or later). What differs (fields, the meaning of the deep hash, the recording format) is in 3D worlds and sessions.debug_ abi_ version()
Cost (when on)
| Condition | Recording | One frame |
|---|---|---|
| Real time, 77 bodies (PC, i7-12700F) | 5 µs / tick | +10–15% |
| Real time, 200 bodies, up to 10 re-simulated ticks (PC) | 12 µs / tick | 0.13 → 0.15 ms |
| Same (Galaxy S9) | 20 µs / tick | 0.19 → 0.22 ms |
| Turn-based | 1.2 µs / tick | – |
| 3D real time, about 115 bodies (PC) | 7.4–7.8 µs / tick | – |
| 3D turn-based, up to 12 bodies (PC) | 1.5 µs / tick | – |
| 3D real time, about 70 bodies, two processes PC ↔ S9 (PC side) | 15–19 µs / tick | – |
| Same (Galaxy S9 side) | about 175 µs / tick | p99 7.1–9.3 → 8.9–10.6 ms |
We recommend on during development and testing, off in production (or on for testers only).
What happens on a desync
- When the hash check finds a mismatch, the host picks the desync tick T and the range [T − ticks_before, T] (only after the previous resync).
- Both peers send their own recordings (in real time, confirmed ticks only) to each other in small pieces, so play traffic is not held up. The resync runs at the same time as usual.
- When both recordings are there, both peers compare them with A = host and B = guest and hold the same report. In that frame
DETPHYS_is set (turn-based:RT_ FRAME_ DEBUG DETPHYS_in that turn's result). Between a PC and a Galaxy S9 the report was ready about 0.3 s after the desync was found (3D, two peers in one process with 20–40 ms one way and 5% loss: 0.4–0.6 s).TURN_ FLAG_ DEBUG - If a peer rejoins or restarts during an exchange, it fails (
DETPHYS_).DEBUG_ FAIL_ LINK
Reading the report
uint32_t n = detphys_rt_debug_count(session); // number of reports (including WAITING)
detphys_debug_session_report rep = { sizeof rep };
detphys_debug_diff diffs[16];
detphys_rt_debug_get(session, n - 1, &rep, diffs, 16, NULL, 0);
if (rep.status == DETPHYS_DEBUG_EXCHANGE_READY) {
// rep.report.cause / first_tick, rep.first_diff.body / field / a / b / ulp
}status:WAITING(exchanging),READY(report done) orFAILED(fail_reason: TIMEOUT, LINK, RECORD, COMPARE).report.: the first tick where anything differs.first_ tick input_first_tick,state_first_tick,body_first_tick,box2d_first_tickandhash_first_ticktell which part differed first (DETPHYS_if it never did).DEBUG_ NONE first_diff: the first differing body ID, field (xycossinvxvyw, …; in 3Dxyzqx–qwvxvyvzwx–wz), both raw values and the distance in ULPs.report.,diff_ bodies diff_values: how many bodies and fields differ at that tick. Passspreadto get "number of differing bodies" per tick and watch the desync spread.
Causes (report.cause) and what to suspect
| Cause | Name | Meaning | Suspect |
|---|---|---|---|
DETPHYS_DEBUG_CAUSE_INPUT | input | The inputs differ (before the physics) | How inputs are passed and quantized (quantizing again on the receiving side?), lost button presses |
DETPHYS_DEBUG_CAUSE_STATE | state | Same inputs and physics, but the game state words differ | Rule code (random numbers, iteration order, clocks, values kept on the rule object; rules 5–8 in 4. Determinism rules) |
DETPHYS_DEBUG_CAUSE_SETUP | setup | Body count, types, shapes or tags differ | How bodies are created and removed (order, conditions, shape registration order). At tick 0: the initial setup |
DETPHYS_DEBUG_CAUSE_PHYSICS | physics | Same inputs and shapes, but the physics values differ | Float math in rules, build settings (fast-math, FMA), a core you built yourself |
DETPHYS_DEBUG_CAUSE_HIDDEN | hidden | All visible values are equal, only Box2D's deep hash (Box3D's state hash in 3D) differs | Hidden state such as contacts; it shows in the values from the next tick. Suspect the same as physics |
DETPHYS_DEBUG_SAME / NO_OVERLAP | same / no_overlap | Everything equal / no common ticks | Widen the range (ticks_before) |
Example: against a peer whose core was built with fast-math, both peers' reports pointed at "tick 0, body 1, x, 1 ULP, physics" (a mm → m division by 1000 had become a multiplication by 0.001).
Saving recordings to a file (bundle)
uint32_t size = 0;
detphys_rt_debug_bundle(session, index, NULL, 0, &size); // returns DETPHYS_ERR_CAPACITY with the size
void* buf = malloc(size);
detphys_rt_debug_bundle(session, index, buf, size, &size);
detphys_debug_write_file("desync_0.dndb", buf, size); // UTF-8 path (non-ASCII paths work on Windows too)A bundle (DNDB) holds both peers' recordings (DNDR) and the report. It can be made even when WAITING or FAILED, from what has arrived. Handy for testers to send you.
Comparing recordings again
To read a tester's bundle (or two recordings) again on your side, compare the two recordings in code:
- C:
detphys_gives where the host's and the guest's recordings are in a bundle;debug_ bundle_ info_ get detphys_compares two recordings.debug_ compare detphys_reads a recording's header and tick range.debug_ file_ info_ get - Unity (C#):
DetPhysDebug.(A = the reference, for example the host) returns aCompare(a, b) DetPhysDebugComparisonwith the same fields as the report: the first diverged tick, the body, the field, both values, the ULP distance and the cause.DetPhysDebug.reads a recording's header.TryGetFileInfo - Read the result the same way as the report (Reading the report, the cause table).
- When you contact support about a desync, attach the bundle file (
.dndb). - 3D recordings and bundles are compared the same way (see 3D worlds and sessions).
Recording without a session
If you run the ticks yourself, use the recording parts directly (detphys_debug.h): detphys_ → allocate a buffer → detphys_ → every tick detphys_ → detphys_ to DNDR bytes → compare with detphys_ (Unity: DetPhysDebug.). Recording only reads the world and never changes the hash. Recording the same tick again (after a rollback) drops that tick and later ones and records them anew.
3D worlds and sessions
With 3D worlds (detphys3d.h), recording, exchanging, reports and bundles work the same way as in 2D. Check detphys_ to know that 3D sessions accept the debugger (the rt and turn versions did not change). What differs:
- Fields: a 3D body has 13 fields: position
xyz, orientation (quaternion)qxqyqzqw, velocityvxvyvzand angular velocitywxwywz.DETPHYS_,DEBUG_ FIELD_ X Y,VXandVYkeep their 2D numbers;ZtoWZare 12–20 (cos,sinandware not used). Reports (first_diff.field) use these names. - Deep hash: in 3D,
box2d_hash,box2d_first_tick, andDETPHYS_hold Box3D's state hash (the same value asDEBUG_ REC_ BOX2D_ HASH detphys3d_). The names are kept from 2D. Read the hidden cause as "only Box3D's hash differs".world_ engine_ hash - Hash: the recorded
hashis the same value asdetphys3d_(the one used for the hash check).world_ hash - Types and shapes: body types are
DETPHYS3D_BODY_*and shapes are box, sphere, capsule and hull (DETPHYS3D_); where 2D has a mesh index (SHAPE_* detphys_), 3D has the hull index.debug_ diff. mesh - Recording format: 3D recordings are DNDR format 2 and have
DETPHYS_inDEBUG_ FILE_ 3D detphys_. 2D recordings stay in format 1. Comparing a 2D recording with a 3D one returnsdebug_ file_ info. flags DETPHYS_.ERR_ FORMAT - Size: in 3D real time (about 115 bodies, PC) one tick was about 7.5 KB and the exchanged recordings were 166–222 KB per peer (31 ticks). 3D turn-based (up to 12 bodies) was 384 bytes per tick.
- Example: comparing a 3D recording from a core built with fast-math against the reference recording pointed at "tick 0, body 1 (a box), x, 1 ULP", as in 2D.
- 3D recordings were byte-identical across builds (msvc, clang-cl, mingw, debug), and a recording written on a Galaxy S9 (android-arm64) was the same as the PC reference recording (61 ticks, no differences).
- 3D matches over two processes (UDP; the 3D push scene with about 70 bodies [40 boxes, 8 spheres, 12 hulls and more], 600 ticks at 60 Hz, injected 20–40 ms one way, 5% loss and 1% duplicates, with a guest value nudged by 1 ULP at ticks 150 and 400): two processes on one PC and PC ↔ S9 (S9 as the guest and as the host; UDP over Wi-Fi) all gave the same report in both processes, pointing at both nudges in one go (tick 150, body 68, x, 1 ULP; tick 400, body 76, x, 1 ULP). Outside the nudged ranges, the confirmed tick hashes matched on all 600 ticks.
- Against a core built with fast-math, both processes' first reports also said "tick 0, body 1, x, 1 ULP" (in a scene without hulls; see the note below).
- Cost (table above): on an S9 recording takes about 175 µs per tick, much more than on a PC (15–19 µs). PC ↔ S9, the S9's one-frame p99 went from 7.1–9.3 ms with the debugger off to 8.9–10.6 ms with it on (the S9 was not pinned to its big cores; measured at a 60 Hz wall clock). Traffic goes from 34–39 B to 497–795 B per tick, but that includes the recording exchange on a desync (about 300,000 B); without a desync nothing is exchanged.
- Note: 3D sessions include the hull contents (checksums) in the config check. A peer whose build computes hulls differently (fast-math and so on) does not connect, and both report "shapes, first differing shape 0" (When peers do not connect).
Recording 3D without a session
Create the recorder with dimensions = 3 in the recording settings (cfg. after detphys_) and call detphys_ every tick. The rest is the same as in 2D: detphys_ → detphys_. Read bodies with detphys_ (detphys_: 13 raw values and the hull index). Mixing up the 2D and 3D calls returns DETPHYS_ when recording and DETPHYS_ when reading.
With moving concave shapes
detphys_ checks that the kinematic bodies in the world are bit-identical to the values rebuilt from their motion functions (0 = all equal). If it is not 0 after a rollback, a motion callback depends on something other than the tick (a clock, a variable outside the game state). Check 3D tables the same way with detphys3d_ (raw bits of position, orientation, velocity and angular velocity). In 3D sessions, peers whose tables differ (motions, split settings) do not connect (the table is part of the config check). Table contents do not appear in recordings or reports. When they do not connect, diff_parts in detphys_tag_info shows kinematic (When peers do not connect).
When peers do not connect (config check mismatch)
If a session keeps waiting for its peer (rt: DETPHYS_ never appears; turn-based: stays CONNECTING) even on the same LAN and port, first suspect that, before any determinism problem, the two peers are not running the same thing. Before connecting, peers compare a config check tag (built from the settings, rule, shapes, hulls, kinematic tables and core version; rt: the config check value, turn-based: the setup hash) and drop packets from a peer whose tag differs, because running different things would diverge from tick 0. Cores with rt version 7 / turn version 9 count such packets and exchange the tag contents (one hash per part) with that peer, so both peers learn which part differs. When both are new, this usually takes 1–2 round trips (tens of ms) from the first packet.
detphys_tag_info ti = { sizeof ti };
detphys_rt_get_tag_info(session, &ti); // turn-based: detphys_turn_get_tag_info. Reading every frame is fine
if (ti.flags & DETPHYS_TAG_INFO_ACTIVE) { // packets come from a peer with a different tag, nothing yet from a matching one
if (ti.flags & DETPHYS_TAG_INFO_REMOTE_KNOWN) { // the peer's contents arrived = we know which part differs
for (uint32_t p = 1; p < (1u << DETPHYS_TAG_PART_COUNT); p <<= 1)
if (ti.diff_parts & p) printf("config differs from the peer: %s\n", detphys_tag_part_name(p));
if (ti.diff_parts & DETPHYS_TAG_PART_SHAPES) printf("first differing shape: %d\n", ti.first_shape_diff);
} else {
printf("config differs from the peer (part unknown; the peer's core may be older)\n");
}
}- Flags: rt sets
DETPHYS_inRT_ FRAME_ TAG_ MISMATCH detphys_(while nothing has arrived from a peer with a matching tag, and when a mismatching packet arrived during that call). Turn-based sessions: checkrt_ frame. flags DETPHYS_inTAG_ INFO_ ACTIVE detphys_. The fields are described in 7. API reference.tag_ info. flags - Session behavior: rt sessions keep waiting (they never give up; the app decides whether to stop). Turn-based sessions still go to ERROR on a HELLO / WELCOME with a different setup hash, and once the peer's contents arrive the differing parts are appended to the
detphys_text, like "(違う所: shapes (first shape 0))" (the error text is in Japanese; 違う所 = differing part).turn_ error - Engine layers and samples: Unity reads the same contents with
TryGetTagInfo, Godot withget_tag_info()and thetag_mismatchsignal, Unreal withGetTagInfo(7. API reference). When the samples see ACTIVE before connecting, they wait for the peer's contents, show "Peer has different settings (differs: rule)" and stop connecting. Whether to stop is the game's choice (the core only reports). - Peers that differ only in session_id (turn-based): no ERROR; the session counts and reports it (the differing part is config) and keeps waiting, so stray old packets from another match on the same port do not break the current match.
- An older core on the other side: cores up to rt version 6 / turn version 8 do not send their tag contents, so the differing part is unknown. If ACTIVE stays on without REMOTE_KNOWN, suspect the peer's core version.
- REMOTE_KNOWN with
diff_parts= 0: all part hashes are equal and only the whole tag differs (only when the 32-bit folded part hashes collide; practically never).
| Differing part | What differs | How to fix |
|---|---|---|
| version | The core's check / protocol version, or 2D vs 3D | Rebuild both with the same core version (same package, plugin, APK). 2D and 3D sessions never connect to each other |
| config | session_id, tick rate, substeps, gravity, 3D contact tuning, turn-based first mover and tick limit per turn | Pass the same settings on both. Use the same session_id on both for each match |
| rule | rule_id, input and state word counts, params (settings of the built-in push rule such as the box count are params too) | Same rule, same version of the rule code and same settings on both (for the samples: the same sample and the same moving field) |
| shapes | 2D: contents, count and order of the registered shapes (.dnshape) / 3D: hull contents (checksums), vertex counts, count and order | In 2D, register the same .dnshape files in the same order. In 3D, if the shape data is the same but this still differs, suspect a build that computes hulls differently (fast-math, FMA, a core you built yourself; 4. Determinism rules). first_shape_diff is the first differing shape |
| kinematic | The kinematic table (whether one was passed, flags, motions, split settings) | Pass the same table on both (same motions in the same order, same split settings) |
Measured (two processes on one PC, UDP, 20–40 ms delay, 5% loss): with only the guest changing the push box count in 2D rt (rule), only the guest moving hull 0 by 1 mm in 3D rt (shapes, shape 0), a different tick limit per turn in turn-based play (config; both go to ERROR with "違う所: config" in the text) and only the session_id differing in turn-based play (config, no ERROR), both processes learned the differing part within 0.1 s of starting (sending their contents 1–2 times per side). On a one-process loopback with 10 ms one way, both peers learned it in 30–31 ms (three one-way trips).