[1.16] Critical thread-safety problems that crash game randomly (with solutions)
BloCamLimb opened this issue ยท 1 comments
Version: ALL versions
I could tell you that you are a very careless person. You have so many methods are not guaranteed to be thread-safe. An example is your ClientProxy, whose run()
will run on FMLClientSetupEvent, this is a parallel event and called from a ForkJoinPool thread pool to your mod event bus. So you can NOT call methods such as in BlockColors
, ItemColors
, ItemModelProperties
directly, because their internal HashMap is not thread safe.
Your code is like this:
Example
public void run() {
ItemModelsProperties.registerProperty(ModuleTool.INFINITY_LAUNCHER, new ResourceLocation(Reference.MOD_ID, "cooldown"), (stack, world, entity) -> {
if (entity instanceof PlayerEntity) {
return ((PlayerEntity) entity).getCooldownTracker().hasCooldown(stack.getItem()) ? 1 : 2;
}
return 2;
});
}
Therefore, players will randomly get ConcurrentModificationException
.
The solution is very simple:
Pass the event (FMLClientSetupEvent) to run()
method, and wrap all the code with event.enqueueWork(() -> { //run here );
. I hope there will be no such issues in all your future projects.
Fixed in 75e0676