Mass Battle Frameworkv1.20.0

失步怎么查 Finding a Desync

NETWORK & REPLICATION

从一个不一致的哈希,倒推回第一帧不一样的地方 · 2026-11-21From one differing hash back to the first tick that differed · 2026-11-21

系列这些文章写给人和他的 agent 一起看。这套文档怎么读 →SeriesThese posts are for you and your agent. How to read these docs →

症状是"两边算得不一样" The Symptom: Two Different Answers

锁步里出了失步,程序通常照跑:没有弹窗,没有调用栈,C++ 一行也没写错。唯一的迹象是两端的模拟哈希对不上——同一帧、同一份输入,两边算出的世界状态不同。屏幕上可能只是几个单位站偏了,某个单位少挨一次伤害,某发子弹落点差半米;也可能什么都看不出来,只有哈希在叫。

A lockstep desync usually leaves the program running: no dialog, no call stack, no C++ mistake you could point at. The only signal is the two peers' simulation hashes disagreeing — same tick, same inputs, different world state. On screen that might be a handful of units standing a little off, one unit taking one hit too few, a bullet landing half a metre wide. Or nothing at all, and only the hash complaining.

还有一条事实得先记住,排查里最常搞反的也是它:报警的那一帧,几乎一定不是开始出错的那一帧。哈希是状态的摘要,记不下因果。两边的状态在第 N 帧就已经不同,报警却通常来自更晚的一次校验或更晚的一个比对窗口。活全在“倒推回第一帧”上,下面这些工具都是围着这一件事做的。

One more fact to hold on to, and the one most hunts get backwards: the tick that complains is almost never the tick that broke. A hash digests state; it keeps no record of cause. The two worlds were already apart at tick N; the alarm comes from a later check, or a later comparison window. All the work sits in walking back to the first tick, and every tool below is built around that one move.

第一步:先分清是哪一类,而不是先看转储 Step One: Classify, Do Not Dump Yet

动手之前先回答一个二选一:这次失步是重同步之后才出现的,还是自由奔跑时自己跑偏的?两种情况根因离得很远。前者是恢复写回去的值和原来不一致,后者是同一条模拟路径在两端算出了不同结果。一个在快照编解码里修,一个在处理器里修。

Before touching a dump, answer one either-or question: did this desync appear right after a resync, or did the sim drift on its own? The two root causes live far apart. The first is a restore writing back values that do not match what was there; the second is the same simulation path returning different results on the two peers. One is fixed in the snapshot codec, the other inside a processor.

分辨的方法就是比两个数:onset 与 payloadTick 的关系。第一帧不一致(onset)如果正好等于重同步载荷帧 + 1,问题在恢复/重放的路径上;如果 onset 明显晚于载荷帧,恢复没有问题,是模拟自己跑偏了。日志里那行 [FORK_ONSET] 就是为这个写的:第一帧不一致落在第几帧,绑定的是哪个载荷帧,一行看完。

The divider is a comparison of two numbers: onset against payloadTick. If the first divergent tick (onset) equals the resync payload tick + 1, the fault is in restore and replay. If onset comes well after the payload, restore was fine and the simulation drifted. That is what the [FORK_ONSET] log line is for: which tick diverged first, and which payload tick it hangs off.

如果是恢复那一类,还有一个更快的机器判据:快照回读校验。恢复方把每个还原过的片段,用捕获时那个编解码器重新编一遍,再跟收到的字节逐字节比。对不上,工具会点名是哪个片段、哪个字段写进了不同的值。这一步不看画面,也不用人工比对,是整条链上最便宜的一刀。

If it is the restore kind, there is a faster mechanical test: the snapshot round-trip check. The restoring peer re-encodes every restored fragment with the same codec the capture used and compares bytes against the blob it received. On a mismatch the report names the fragment and the field that came back with a different value. No frame to inspect, no human comparison; it is the cheapest cut on the chain.

第二步:让两端冻在同一帧 Step Two: Freeze Both Peers on the Same Tick

默认两端是自由奔跑的。检测到失步的那一端在自己那一帧冻住,而另一端要等网络消息才知道,这时它已经又跑了几帧。这个状态下各存一份再比,差异里绝大多数只是帧差:实体集合不一样,位置差一个步长,甚至“少了几个实体”其实只是对方还没跟上。

By default both peers free-run. The peer that notices the mismatch freezes on its own tick; the other one only hears about it over the network, by which point it has advanced a few ticks. Dump both there and nearly every difference is just that offset: different entity sets, positions a step apart, even "a few entities missing" that are really a few entities not yet arrived.

所以取证时要开哈希栅栏:第 N 帧的比对没在两端都做完之前,谁也不许从 N 推进到 N+1。代价大约每帧一个 RTT,只在调试时开,而且要和转储开关一起开才有意义。转储自己带着同样的保护:触发时两端各自在自己那一帧暂停,同步写完转储,再把暂停状态还回原样——原来停着的继续停,原来跑着的继续跑。这样重同步机制没机会在取证期间把现场盖掉。

So for forensics, arm the hash barrier: neither peer steps from N to N+1 until the comparison for N has finished on both. It costs about one RTT per tick, it is a debug-only switch, and it means nothing without the dump switch. The dump carries the same protection: when it fires, each peer pauses on its own tick, writes the dump synchronously, then puts the pause state back the way it was — still paused if it was paused, running if it was running. The resync machinery gets no chance to overwrite the scene while it is being captured.

想复现一次“重同步之后”的失步,不用等人掐着点去点按钮:把自动重同步的触发帧写进 ini,客户端跑到那一帧自己发一次请求。两端读同一份 ini,触发精确到帧,整轮就能脚本化跑。这类开关不该常开,它们是取证工具,用完就关。

To reproduce a post-resync desync, you do not need someone clicking a button at the right moment. Put the trigger tick for an automatic resync in the ini and the client fires one request when the simulation reaches it. Both peers read the same ini, the trigger lands on an exact tick, and the whole run scripts. Switches like this should not stay on; they are forensics tools.

第三步:从类别收到实体 Step Three: Narrow from Category to Entity

帧号对齐之后,先别逐字段比整个世界,那是最后一步。范围得一层层压:哈希按分量拆开,看哪一路先动——移动、索敌、攻击,还是状态。哪一路先分叉,对应的处理器就是嫌疑人,其余几路可以整条划掉。

With matching tick numbers in hand, do not start field-diffing the world; that is the last step. Narrow first: split the hash by component and see which channel moved first — movement, targeting, attack, state. The first channel to fork names the suspect processor, and the other channels drop out wholesale.

再往下到实体。把发散帧附近的逐 tick、逐实体哈希各存成一份,两端对照着看,每行固定是 tick / 类别 / UID / 哈希。第一个对不上的实体就是第一个跑偏的实体,它之前的帧全都还一致。必须把一整圈排出来才看得出从哪一帧开始斜:发散帧从来不是起点。

Then down to entities. Write out the per-tick, per-entity hashes around the divergence, one file per peer, every row fixed as tick / category / UID / hash, and read the two side by side. The first entity that disagrees is the first one that went wrong, and every earlier tick still agrees. You need the whole ring laid out to see where the slope starts: the divergence tick is never the onset.

机械的部分已经有工具做了。命令行里一条 first-fork 就能报出起始帧和分叉的 UID 列表,并把名单写成文件;后面所有针对性的排查都读这份文件,别在脚本里手写 UID 集合——“我记得就是这几个”是这活里最常见的错误来源。另外还有按分量拆分的子命令,按 UID 和帧区间拉时间线的子命令,以及把长度折叠值还原成原始长度的子命令(折叠省空间,读日志的人要的是原值)。

The mechanical half is tooled already. One first-fork command reports the onset tick and the forking UIDs and writes the list to a file; every targeted probe after that reads the file instead of hard-coding a UID set in a script, because "I remember it was these" is the most common way this work goes wrong. There are also subcommands for the per-component split, for a timeline over UID and tick range, and for turning a length fold back into raw lengths (folds pack well; whoever reads the log wants the number).

第四步:全量转储,然后逐字段比对 Step Four: Full Dump, Then Field-by-Field

范围收到“这一帧、这几个实体”,才轮到全量转储。它落在工程 Saved 目录下的失步目录里,每端一份,文件名带角色和触发帧。内容是按 UID 升序排好的每个实体:旗标的高低两个字,加一批热门片段里每一个反射属性。浮点写成十六进制位模式,这样 −0 和 +0、NaN 和同值的差异都看得出来;句柄、对象引用、委托这几类会变的东西跳过;数组带截断上限,但记下真实长度。

Only when the scope is "this tick, these entities" is a full dump worth taking. It lands in a desync folder under the project's Saved directory, one file per peer, role and trigger tick in the name. The content is every entity, sorted by ascending UID: the two flag words, plus every reflected property of a set of hot fragments. Floats go out as hex bit patterns, so −0 against +0 and NaN against an equal-looking value stay visible; handles, object references and delegates are skipped; arrays are capped per element with the true length recorded.

比对工具吃两份文件,按各自头里的角色分辨谁是谁,所以参数顺序不会把两列写反;默认只列最严重的若干实体,可以按 UID 过滤,也可以关掉分字段的统计表。它干的是人做不到的事:把两份几万行的转储摊成“这个实体的这个字段,这边是这些位、那边是那些位”。

The differ takes both files and identifies each peer from its own header, so the column order cannot come out swapped; by default it lists the worst handful of entities, and you can filter by UID or turn off the per-field histogram. It does what no human can: flatten two tens-of-thousands-line dumps into "this field of this entity holds these bits here, those bits there".

这里有一道必须先做的校验,我们花了些时间才想明白:先证明这份转储真的在它标的那一帧上。转储头部同时记着“触发帧”和“这个世界的实际帧”,两个可以不等——冻结晚到的那一端就会差几帧。工具可以拿转储去对它自己那一端的日志:逐个实体比哈希,命中率低就说明转储不在标的那一帧。这时候再去读字段差异,读到的全是帧差造成的假象。

One check has to come first, and it took us a while to get into the habit: prove the dump is really on the tick it claims. The header records both the trigger tick and the tick the world was actually on, and the two can differ — the peer that froze late is a few ticks behind. The tool can check a dump against its own peer's log by comparing each entity's hash with the row logged for that tick; a low hit rate means the dump is not where it says it is. Read field differences at that point and you are reading offset artefacts.

还有两个常驻的低成本守卫,怀疑“看起来不像失步”的时候可以先各看一眼:一个在每次重同步恢复之后核对快照往返(就是前面那一步),一个在每一次目标写入时检查写进去的目标主类型是不是 Agent。命中才点火,正常运行一行输出都没有。

Two standing guards cost almost nothing and are worth a glance whenever something "does not look like a desync": one re-checks the snapshot round trip after every resync restore (the check described earlier), the other inspects every single target write and asks whether the target's main type is Agent. They fire only on a hit and print nothing in a healthy run.

常见来源,其实只有四类 The Usual Suspects Are Only Four

定位到具体字段之后,答案大多落在四类里,正好是确定性那篇列的四条纪律各自的反面:未播种的随机(哪怕只有一处 FMath::Rand)、依赖并行归约的顺序(求和、求最小值)、依赖容器的遍历顺序(TMap 不作承诺)、读了一个没同步、也没进哈希的值。最后一类最阴,因为两端都“算对了”,只是一端读了它没资格读的东西:一个只在本端解析的渲染期数值,一个没有快照的帧位置。

Once the field is known, the answer almost always lands in one of four buckets, each the flip side of a discipline from the determinism article: unseeded randomness (even one FMath::Rand), dependence on parallel reduction order (sums, minima), dependence on container iteration order (TMap promises nothing), and reading a value that is neither synchronised nor hashed. That last one is the nastiest, because both peers "computed correctly"; one of them just read something it had no right to: a render-time value resolved locally, a frame position with no snapshot.

前三类当场就露,或者很快露;第四类能潜伏很久,它在单机上完全正确,回放里也完全正确,只有两个真实的对端才会分叉。所以针对它的检验很窄,就一句话:这个新结果的每一个输入,是被同步的,还是烘焙期就不可变的?两个都不是,那就是它。

The first three show up on the spot or soon after. The fourth can sit for a long time: it is correct on one machine, correct in a replay, and only two real peers make it fork. So the test is one narrow question: is every input to this new result either synchronised or immutable at bake time? If an input is neither, that is the bug.

开关与工具一览 Switches and Tools at a Glance

前面讲的是顺序,这里是可以照着填的清单。诊断开关全都是 Config,写进 DefaultMassBattle.ini,段名跟着拥有它的那个子系统走:主子系统的在 [/Script/MassBattle.MassBattleSubsystem],网络子系统那几个在 [/Script/MassBattle.MassBattleNetworkSubsystem]。在编辑器里改子系统默认值也一样。两端要读同一份值。只在排查那一轮打开:每个都有代价,最贵的那个每帧一个往返。

What came before is the order; here is the checklist. Every diagnostic switch is a Config property in DefaultMassBattle.ini, and the section follows the subsystem that owns it: the core ones live under [/Script/MassBattle.MassBattleSubsystem], the network ones under [/Script/MassBattle.MassBattleNetworkSubsystem]. Editing the subsystem defaults in the editor works too. Both peers must read the same values. Turn them on for the hunt only: each one costs something, and the priciest is a round trip per tick.

要做的事What you want开关或工具Switch or tool注意Note
失步当帧暂停并全量转储Pause and dump the full world on divergence bDesyncAutoDump 每端每世界只转储一次,用于防刷屏One dump per peer per world, as a spam guard
让两端冻在同一帧Freeze both peers on one tick bDesyncBarrierMode,超时由 DesyncBarrierTimeoutSeconds 兜住, with DesyncBarrierTimeoutSeconds as the escape hatch 每帧大约一个往返;不开转储就没意义Roughly one round trip per tick; meaningless without the dump
按分量与实体收窄Narrow by component and entity bEnablePerEntityHashDiag 两端都要开;它门控 [FORK_ONSET] 和逐分量哈希行;日志能到几百 MBBoth peers; gates [FORK_ONSET] and the per-component hashes; logs reach hundreds of MB
核对重同步是否写错值Check whether the restore wrote wrong values bEnableSnapshotRoundTripCheck 只在重同步路径上跑,平时零开销Runs on the resync path only; zero cost otherwise
找"不在哈希也不在快照"里的实体Find entities inside neither the hash nor the snapshot DesyncUidCensusTick 一次性普查;要打开诊断总闸One-shot census; needs the diag master switch
复现"重同步之后"的失步Reproduce a post-resync desync bDesyncDebugAutoResync + DesyncDebugAutoResyncTick 客户端在指定帧自动发一次请求,整轮可以脚本化The client fires one request at that tick; the whole run becomes scriptable
离线逐字段比对两份转储Field-diff two dumps offline Tools/desync_dump_diff.py 可以用 --uid 限定;也能拿转储对它自己的日志验帧号Filter with --uid; can verify a dump against its own peer's log
从日志里找首个分歧帧与实体名单Get the onset tick and the forking UID list from a log Tools/desync/dsync.py 子命令:first-fork / timeline / agh-split / rt-check / fold-decodeSubcommands: first-fork / timeline / agh-split / rt-check / fold-decode

我们在这套工具上踩过的两个坑 Two Scars from Building This Tooling

第一个坑出在“一次会话只转储一份”这道防刷屏闸门上。它原本按进程记状态,PIE 里服务端和客户端两个世界共用一个进程,先发散的那个世界就把另一个的转储整个挡住了。改成按世界记就对了,但键得用世界的包名,不能用世界对象名:PIE 的实例前缀在包上,世界对象名对所有实例都只是裸地图名,拿它作键会把同一会话的第二个世界判成重复。这个坑只在单进程双世界时出现,两端各起一个进程的时候永远看不到。

The first scar is the "one dump per session" spam guard. It kept its state per process, so in PIE, where the server and client worlds share a process, whichever world diverged first blocked the other one's dump entirely. Keying it per world fixed that, but the key had to be the world's package name, not the world object's name: PIE's per-instance prefix lives on the package, while the world object hands back the bare map name for every instance, so keying on that made the session's second world look like a duplicate. This only shows up with two worlds in one process; run the peers as separate processes and you never see it.

第二个坑更麻烦。转储要先把现场冻住,而那条路上的暂停没有归属者,转储结束后没有代码去解除它。它本来只该覆盖一次同步转储那么短的时间,可一旦留在置位状态,整局就冻死;之后每次手动重同步都会被“失步暂停中”挡回去,看起来像重同步坏了。我们是在一次良性的加入握手哈希不匹配之后发现的:那次根本没有真的失步,却把所有 PIE 世界永久冻住了。修法改成保存和还原:先把原状态存下来,用完再还回去。取证代码自己也会坏,而且坏起来的样子跟它要查的故障一样。

The second scar is worse. A dump has to freeze the scene first, and the pause on that path had no owner: nothing released it when the dump finished. It only needed to last one synchronous dump, but left set it wedged the entire session, and every later manual resync was refused as "desync pause active", so it read as a broken resync. We found it after a benign join-handshake hash mismatch: no real desync anywhere, yet every PIE world froze for good. The fix moved to save-and-restore, storing the original state and putting it back afterwards. Forensic code breaks too, and when it breaks it looks like the fault it was built to investigate.

每一条失步最后都变成一条纪律 Every Desync Ends as a Rule

把这条链走完一遍,会发现失步的成本几乎全在定位上。定位难有三个原因:报出来的帧不可信,两端的状态不在同一帧,还有“我记得是这几个实体”这类人为误差。工具就是照这三件事长的:栅栏让帧可比,校验让转储可信,名单交给文件去记。

Walk the chain once and nearly all of the cost is in locating. Locating is hard for three reasons: the reported tick cannot be trusted, the two states are not on the same tick, and human memory of "it was these entities" fails. The tools are shaped by those three: the barrier makes ticks comparable, the verification makes a dump trustworthy, and the list lives in a file instead of someone's head.

每查完一条,真正的产出是它变成的那条纪律:禁止未播种的随机,禁止依赖归约顺序,禁止依赖容器的遍历顺序,禁止读没同步也没烘焙的值。这些规则写下来之前,每一条背后都是一次真实的失步;写下来之后,新代码不会再用同一种方式犯。这篇如果只剩一句有用的话,就是:失步是一条还没被写下来的规则。

Each hunt ends by producing the rule it turns into: no unseeded randomness, no dependence on reduction order, no dependence on container iteration order, no reading values that are neither synchronised nor baked. Every one of those had cost a real desync before it was written down, and new code stops repeating it afterwards. If one line from this article survives, let it be this: a desync is a rule nobody has written down yet.

Leroy Works · Mass Battle Framework 作者
联系:Discord · QQ 群 916358710 · Fab · 全部联系方式

Leroy Works — author of Mass Battle Framework
Contact: Discord · QQ group 916358710 · Fab · all channels