Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ConcurrentModificationException in tick handlers #6

Open
wants to merge 1 commit into
base: 1.20.4
Choose a base branch
from

Conversation

BluSpring
Copy link

If a tick handler is registered within a ticking event, the game crashes to a ConcurrentModificationException.
This PR solves that problem by replacing TickManager's LinkedList with a ConcurrentLinkedQueue.

@Saereth
Copy link
Collaborator

Saereth commented Oct 29, 2024

hey @BluSpring do you have an info on the cause of this, a crash log or anything like that? CME shouldnt really happen here to the best of my knowledge. Im curious if this is some mixin or something perhaps? If you have any more info we'd appreciate it! Ty for the pr

@BluSpring
Copy link
Author

Basically, if you register/unregister a tick handler to the same TickManager directly inside the tick method of a TickHandler, it will cause a CME due to a new handler being added to the list or a handler being removed from the list at the same time as that list being iterated.

Reproducing the crash can be triggered by simply registering a new level type TickHandler to a TickManager, and unregistering it immediately within the tick, for instance:

public class TickHandler implements ITickHandler {
    @Override
    public void tick(TickEvent.Type type, Object... objects) {
        Observerlibrepro.tickManager.unregister(this);
    }

    @Override
    public EnumSet<TickEvent.Type> getHandledTypes() {
        return EnumSet.of(TickEvent.Type.LEVEL);
    }

    @Override
    public boolean canFire(TickEvent.Phase phase) {
        return phase == TickEvent.Phase.END;
    }

    @Override
    public String getName() {
        return "Test";
    }
}
@Mod(Observerlibrepro.MODID)
public class Observerlibrepro {
    public static final String MODID = "observerlibrepro";
    public static final TickManager tickManager = new TickManager();
    
    public Observerlibrepro(IEventBus modEventBus, ModContainer modContainer) {
        NeoForge.EVENT_BUS.register(this);

        tickManager.attachListeners(NeoForge.EVENT_BUS);
        tickManager.register(new TickHandler());
    }

    @SubscribeEvent
    private void registerCommands(RegisterCommandsEvent event) {
        // This is used only to register a new TickHandler while in the world, to verify that the CME crash has been fixed.
        event.getDispatcher().register(Commands.literal("testaddtick").executes(ctx -> {
            tickManager.register(new TickHandler());

            return 1;
        }));
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants