Uploaded image for project: 'Minecraft: Java Edition'
  1. Minecraft: Java Edition
  2. MC-258939

Non-atomic cached state can cause multithreaded crashes

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • 23w14a
    • 1.19.3
    • None
    • Plausible
    • Crash
    • Very Important
    • Platform

      ItemStacks have a an emptyCacheFlag that is used to return AIR instead of null from getItem(). This flag is updated every time count is set, however, and it is set to false momentarily before being updated to the true state in ItemStack.updateEmptyCacheFlag(). It is possible that calling ItemStack.setCount() on ItemStack.EMPTY on one thread during a very unlucky window can cause another thread to get null back from ItemStack.getItem(), which will almost universally cause a crash, as the method is not nullable, and returns AIR instead of null when the emptyCacheFlag is set correctly. Crashes like these are somewhat rare, but common enough that I've seen it happen close to a dozen times.

      I don't have any unmodded crash reports because I always play with mods, however, this crash is easily possible in vanilla, and provides a very rare and hard to diagnose crash when calling ItemStack.setCount(), which is normally completely safe even on ItemStack.EMPTY. Ideally, this method should be changed to be atomic, and not have ItemStack.EMPTY flicker to be cached as non-empty frequently.

      Crashlog from another user: https://mclo.gs/sGocG0Z

            gegy1000 [Mojang] Gegy
            EmilyPloszaj Emily
            Votes:
            10 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated:
              Resolved:
              CHK: