NeoForge icon indicating copy to clipboard operation
NeoForge copied to clipboard

ModConfigEvent can cause concurrency issues in mods

Open embeddedt opened this issue 1 year ago • 2 comments

Forge fires ModConfigEvent.Loading when a config is first loaded, and ModConfigEvent.Reloading when a config reloads as a result of the file watcher detecting a change. However, there is no locking done in the function that posts these events, meaning that it is possible for both Loading and Reloading to fire simultaneously.

Many mods have ModConfigEvent event handlers that are not protected against this, and will therefore occasionally throw CMEs. This is technically a bug in those mods but I also think the design of this event is confusing/misleading - it really does not make sense for a config to load and reload at the exact same time.

https://github.com/ExcessiveAmountsOfZombies/Croptopia/issues/517 is an example of this occuring in the wild; I patched it in ModernFix by wrapping the Forge function that posts the event with a wrapper that synchronizes on the underlying config object, ensuring that all mods will never see two config events posted concurrently.

embeddedt avatar Jul 21 '23 23:07 embeddedt