Skip to content

Fix race condition in ForgeZetaEventBus (#52)#80

Open
MauveAvenger wants to merge 1 commit intoVazkiiMods:1.20from
MauveAvenger:fix/event-bus-race-condition
Open

Fix race condition in ForgeZetaEventBus (#52)#80
MauveAvenger wants to merge 1 commit intoVazkiiMods:1.20from
MauveAvenger:fix/event-bus-race-condition

Conversation

@MauveAvenger
Copy link

Problem

ForgeZetaEventBus.convertedHandlers uses Object2ObjectOpenHashMap which is not thread-safe. During mod loading with many mods (370+), concurrent calls to subscribeMethod() and unsubscribeMethod() can corrupt the map, causing:

java.lang.ArrayIndexOutOfBoundsException: Index 256 out of bounds for length 129
    at it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap.rehash(...)
    at org.violetmoon.zetaimplforge.event.ForgeZetaEventBus.subscribeMethod(...)

Relates to #52

Fix

Replace Object2ObjectOpenHashMap with ConcurrentHashMap. This is a minimal change that makes the map operations thread-safe without changing the API or behavior.

Testing

  • Tested with 370+ mod modpack that previously failed ~50% of startups
  • Server now starts reliably with no race condition crashes
  • No performance impact observed (this is startup-time code, not hot path)

Alternatives Considered

  • Synchronized blocks around map access - would work but ConcurrentHashMap is cleaner
  • Making the whole class synchronized - overkill for this use case

Happy to adjust the approach if you'd prefer something different.

Replace Object2ObjectOpenHashMap with ConcurrentHashMap to prevent
race condition during concurrent mod loading.

The non-thread-safe fastutil map was being accessed from multiple
modloading-worker threads simultaneously, causing ArrayIndexOutOfBoundsException
during rehash operations.
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.

1 participant