Folia icon indicating copy to clipboard operation
Folia copied to clipboard

Unsafety in sort of important variable in CraftBlockStates

Open PedroMPagani opened this issue 1 year ago • 0 comments

Expected behavior

The variable DISABLE_SNAPSHOT is kept as a static boolean on paper, but since paper is ticking single threaded, it doesn't really run into issues, every Folia call however, does not take this into consideration, I think this should be changed to ThreadLocal, otherwise this could possibly lead into issues?


# public static boolean DISABLE_SNAPSHOT = false; << PAPER.

public static final ThreadLocal<Boolean> DISABLE_SNAPSHOT = ThreadLocal.withInitial(() -> false);

        this.snapshotDisabled = DISABLE_SNAPSHOT.get();
        if (snapshotDisabled) {
            this.snapshot = this.tileEntity;
        } else {
            this.snapshot = this.createSnapshot(tileEntity);
        }
        
        
                boolean prev = CraftBlockEntityState.DISABLE_SNAPSHOT;
        CraftBlockEntityState.DISABLE_SNAPSHOT.set(!useSnapshot);
        try {
        // Paper end
        CraftBlockState blockState = CraftBlockStates.getBlockState(world, blockPosition, blockData, tileEntity);
        blockState.setWorldHandle(craftBlock.getHandle()); // Inject the block's generator access
        return blockState;
        // Paper start
        } finally {
            CraftBlockEntityState.DISABLE_SNAPSHOT.set(prev);
        }

Observed/Actual behavior

Steps/models to reproduce

Plugin and Datapack List

Folia version

Other

PedroMPagani avatar Sep 19 '24 21:09 PedroMPagani