CompoundTag 并发修改修复
一个轻量级的 Mixin 补丁,旨在消除在 NBT 数据序列化期间由 java.util.ConcurrentModificationException(并发修改异常)导致的客户端断开连接问题。虽然该模组最初是为了解决原版 Weather, Storms & Tornadoes (Weather2) 模组中长期存在的一个问题,但此模组提供了一个全局的、引擎级别的修复方案,以保护你的整个整合包。
? 问题所在

当某个模组尝试从异步网络线程(如 Netty I/O)对 CompoundTag(NBT)进行编码时,如果主游戏线程同时正在修改同一个标签,Minecraft 就会频繁地断开客户端连接。由于 Java 标准的 HashMap 本质上不是线程安全的,这种并发访问会立即触发 ConcurrentModificationException。Minecraft 处理此错误的方式是立即丢弃客户端连接,从而破坏游戏体验。
?️ 解决方案
与其追逐各个模组的更新,此补丁直接注入到 Minecraft 原版的 net.minecraft.nbt.CompoundTag.write() 方法中。它拦截了序列化过程并实现了一个优化的“安全快照”机制:
- 无锁重试:动态捕获内部映射状态,而不会引入线程死锁。
- 故障安全上限:强制执行严格的 100 次重试限制。如果超过限制,它将安全地写入一个空标签以保持连接的稳定性。
- 快速执行:在实际环境中,安全快照通常在 1–3 次尝试内成功解决。
✨ 主要特性
- 通用保护:自动屏蔽任何跨多个线程不当处理 NBT 数据的任意模组。
- 零配置:真正的即插即用解决方案。无需配置文件或调整设置。
- 非侵入式架构:不触碰单个模组的源代码,大幅降低冲突风险。
- 经过压力测试的性能:在加速至 1000 TPS 的环境下验证过,处理开销微乎其微。
- 面向未来的设计:作为一种被动故障安全机制。如果上游模组在其代码中解决了线程 bug,此补丁将优雅地变为“无操作”(no-op),以维持原版行为。
⚙️ 支持版本与加载器
此模组旨在在现代模组生态系统中具有高度的通用性:
- 模组加载器:Minecraft Forge 和 NeoForge
- 游戏版本:Minecraft 1.20.x、1.21.x 以及任何受 NBT 驱动的网络断开问题影响的相邻版本(只需忽略兼容性警告即可)。
? 常见问题
问:这仅适用于 Weather2 吗?
答:不是。Weather2 只是开发的契机,但该修复程序是在原版引擎层面运行的。任何执行异步 NBT 修改的模组都能从中受益。
问:这会影响服务器或客户端性能吗?
答:不会。执行开销完全可忽略不计。在极端模拟 tick 环境下的测试显示,帧率或 tick 时间没有任何下降。
问:我需要配置任何东西吗?
答:不需要。补丁会在初始化时自动激活。你可以通过在控制台中检查以下信息来确认它是否正在运行:[weather2patch] CompoundTag thread-safety patch is active。
问:如果上游模组更新并修复了该 bug 会发生什么?
答:什么都不会坏。因为此模组针对的是原版层,它将简单地注意到线程状态已经是干净的,完全跳过重试,并让原版代码接管。
[描述在 Gemini 3.1 Flash 的帮助下进行了更新]
CompoundTag Concurrent Modification Fix
A lightweight Mixin patch that eliminates client disconnections caused by java.util.ConcurrentModificationException during NBT data serialisation. While developed to resolve a long-standing issue in the original Weather, Storms & Tornadoes (Weather2) mod, this mod applies a global, engine-level fix to protect your entire modpack.
? The Problem

Minecraft frequently disconnects clients when a mod attempts to encode a CompoundTag (NBT) from an asynchronous network thread (such as Netty I/O) while the main game thread is simultaneously modifying that same tag. Because Java’s standard HashMap is inherently non-thread-safe, this concurrent access triggers an immediate ConcurrentModificationException. Minecraft handles this error by instantly dropping the client connection, disrupting gameplay.
?️ The Solution
Instead of chasing individual mod updates, this patch injects directly into vanilla Minecraft's net.minecraft.nbt.CompoundTag.write() method. It intercepts the serialisation process and implements an optimised "safe snapshot" mechanism:
- Lock-Free Retries: Dynamically captures the internal map state without introducing thread deadlocks.
- Fail-Safe Cap: Enforces a strict 100-retry limit. If exceeded, it safely writes an empty tag to maintain connection stability.
- Rapid Execution: In real-world environments, the safe snapshot successfully resolves within 1–3 attempts.
✨ Key Features
- Universal Protection: Automatically shields any mod that improperly handles NBT data across multiple threads.
- Zero Configuration: A true plug-and-play solution. No config files or tweaks needed.
- Non-Invasive Architecture: Leaves individual mod source codes untouched, drastically reducing conflict risks.
- Stress-Tested Performance: Verified at accelerated rates of 1000 TPS with negligible processing overhead.
- Future-Proof Design: Acts as a passive fail-safe. If a mod resolves its threading bugs upstream, this patch gracefully becomes a "no-op" (no operation) to maintain vanilla behaviour.
⚙️ Supported Versions & Loaders
This mod is built to be highly versatile across modern modding ecosystems:
- Mod Loaders: Minecraft Forge and NeoForge
- Game Versions: Minecraft 1.20.x, 1.21.x, and any adjacent versions suffering from NBT-driven network drops (just ignore the compatibility warnings).
? Frequently Asked Questions
Q: Does this only work for Weather2?
A: No. Weather2 was the catalyst for development, but the fix operates at the vanilla engine level. Any mod executing asynchronous NBT modifications will benefit.
Q: Will this impact server or client performance?
A: No. The execution overhead is completely negligible. Testing under extreme tick-simulated environments showed zero degradation in frame rates or tick times.
Q: Do I need to configure anything?
A: No. The patch activates automatically upon initialisation. You can confirm it is running by checking your console for: [weather2patch] CompoundTag thread-safety patch is active.
Q: What happens if an upstream mod updates and fixes the bug?
A: Nothing breaks. Because this mod targets the vanilla layer, it will simply notice the thread state is already clean, bypass retries entirely, and let the vanilla code take over.
[Description Updated with the help of Gemini 3.1 Flash]
评论 0
登录后参与评分
还没有评论,来抢沙发吧