From dbb8da04d329ef6dbf93111c0a6dc154721c4b98 Mon Sep 17 00:00:00 2001 From: Matt <4009945+MattBDev@users.noreply.github.com> Date: Sun, 1 Nov 2026 18:47:22 -0500 Subject: [PATCH 1/5] fix: avoid section lock-order deadlock in PaperweightGetBlocks reset() and CharGetBlocks.trim() take every section lock, but readers take a section lock before sectionLock (CharBlocks EMPTY.get -> update -> getSections). internalCall, updateGet and trim(true) called them while holding the sectionLock write lock, so a reader on a neighbouring chunk could deadlock the blocking executor and stall every queued edit. Run them after the write lock is released instead, in all adapters. --- .../fawe/v1_21_R1/PaperweightGetBlocks.java | 27 ++++++++++++++++--- .../fawe/v1_21_11/PaperweightGetBlocks.java | 27 ++++++++++++++++--- .../fawe/v1_21_4/PaperweightGetBlocks.java | 27 ++++++++++++++++--- .../fawe/v1_21_5/PaperweightGetBlocks.java | 27 ++++++++++++++++--- .../fawe/v1_21_6/PaperweightGetBlocks.java | 27 ++++++++++++++++--- .../fawe/v1_21_9/PaperweightGetBlocks.java | 27 ++++++++++++++++--- .../impl/fawe/v26_1/PaperweightGetBlocks.java | 27 ++++++++++++++++--- .../impl/fawe/v26_2/PaperweightGetBlocks.java | 27 ++++++++++++++++--- .../impl/fawe/v26_3/PaperweightGetBlocks.java | 27 ++++++++++++++++--- 9 files changed, 207 insertions(+), 36 deletions(-) diff --git a/worldedit-bukkit/adapters/adapter-1_21/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_R1/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-1_21/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_R1/PaperweightGetBlocks.java index 05bcded740..d96706e6d8 100644 --- a/worldedit-bukkit/adapters/adapter-1_21/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_R1/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-1_21/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_R1/PaperweightGetBlocks.java @@ -529,16 +529,21 @@ protected > T internalCall( synchronized (lock) { lock.acquire(); // Wait until we have the lock lock.release(); + // Lock order: a section's lock (sectionLocks[i]) comes before sectionLock. Readers take them in + // that order (CharBlocks EMPTY.get -> update -> getSections). reset() takes every section's lock, + // so calling it while holding sectionLock deadlocks against a reader. Defer it until after unlock. + boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = null; - this.reset(); + resetAll = true; } else if (existingSection != getSections(false)[getSectionIndex]) { this.sections[getSectionIndex] = existingSection; - this.reset(); + resetAll = true; } else if (!Arrays.equals(update(getSectionIndex, new char[4096], true), load(layerNo))) { + // Safe under sectionLock: it only takes this section's lock, which we already hold. this.reset(layerNo); /*} else if (lock.isModified()) { this.reset(layerNo);*/ @@ -546,6 +551,11 @@ protected > T internalCall( } finally { sectionLock.writeLock().unlock(); } + // Deferred from above: sectionLock is released, so taking every section's lock here is safe. + // Still runs before newChunkSection(this::load, ...) reads the cache below. + if (resetAll) { + this.reset(); + } PalettedContainer> biomeData = setBiomesToPalettedContainer( biomes, @@ -824,13 +834,16 @@ private void updateGet( char[] arr, int layer ) { + // reset() takes every section's lock, and a section's lock must come before sectionLock (see internalCall). + // Defer it until sectionLock is released to avoid deadlocking against a reader. + boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = new LevelChunkSection[chunkSections.length]; System.arraycopy(chunkSections, 0, this.sections, 0, chunkSections.length); - this.reset(); + resetAll = true; } if (this.sections == null) { this.sections = new LevelChunkSection[chunkSections.length]; @@ -843,6 +856,10 @@ private void updateGet( } finally { sectionLock.writeLock().unlock(); } + // Deferred reset (see above). Must run before blocks[layer] is set, or it would clear the new array. + if (resetAll) { + this.reset(); + } this.blocks[layer] = arr; } @@ -1076,11 +1093,13 @@ public boolean trim(boolean aggressive) { blockLight = new DataLayer[getSectionCount()]; sections = null; levelChunk = null; - return super.trim(true); } } finally { sectionLock.writeLock().unlock(); } + // super.trim(true) takes every section's lock, and a section's lock must come before sectionLock (see + // internalCall). Run it after sectionLock is released to avoid deadlocking against a reader. + return super.trim(true); } synchronized (this) { for (int i = getMinSectionPosition(); i <= getMaxSectionPosition(); i++) { diff --git a/worldedit-bukkit/adapters/adapter-1_21_11/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_11/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-1_21_11/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_11/PaperweightGetBlocks.java index 5704a1ebbb..1d36112927 100644 --- a/worldedit-bukkit/adapters/adapter-1_21_11/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_11/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-1_21_11/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_11/PaperweightGetBlocks.java @@ -521,19 +521,24 @@ protected > T internalCall( synchronized (lock) { lock.acquire(); // Wait until we have the lock lock.release(); + // Lock order: a section's lock (sectionLocks[i]) comes before sectionLock. Readers take them in + // that order (CharBlocks EMPTY.get -> update -> getSections). reset() takes every section's lock, + // so calling it while holding sectionLock deadlocks against a reader. Defer it until after unlock. + boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = null; - this.reset(); + resetAll = true; } else if (existingSection != getSections(false)[getSectionIndex]) { this.sections[getSectionIndex] = existingSection; - this.reset(); + resetAll = true; } else if (!Arrays.equals( update(getSectionIndex, new char[4096], true), load(layerNo) )) { + // Safe under sectionLock: it only takes this section's lock, which we already hold. this.reset(layerNo); /*} else if (lock.isModified()) { this.reset(layerNo);*/ @@ -541,6 +546,11 @@ protected > T internalCall( } finally { sectionLock.writeLock().unlock(); } + // Deferred from above: sectionLock is released, so taking every section's lock here is safe. + // Still runs before newChunkSection(this::load, ...) reads the cache below. + if (resetAll) { + this.reset(); + } PalettedContainer> biomeData = setBiomesToPalettedContainer( biomes, @@ -775,13 +785,16 @@ private void updateGet( char[] arr, int layer ) { + // reset() takes every section's lock, and a section's lock must come before sectionLock (see internalCall). + // Defer it until sectionLock is released to avoid deadlocking against a reader. + boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = new LevelChunkSection[chunkSections.length]; System.arraycopy(chunkSections, 0, this.sections, 0, chunkSections.length); - this.reset(); + resetAll = true; } if (this.sections == null) { this.sections = new LevelChunkSection[chunkSections.length]; @@ -794,6 +807,10 @@ private void updateGet( } finally { sectionLock.writeLock().unlock(); } + // Deferred reset (see above). Must run before blocks[layer] is set, or it would clear the new array. + if (resetAll) { + this.reset(); + } this.blocks[layer] = arr; } @@ -1027,11 +1044,13 @@ public boolean trim(boolean aggressive) { blockLight = new DataLayer[getSectionCount()]; sections = null; levelChunk = null; - return super.trim(true); } } finally { sectionLock.writeLock().unlock(); } + // super.trim(true) takes every section's lock, and a section's lock must come before sectionLock (see + // internalCall). Run it after sectionLock is released to avoid deadlocking against a reader. + return super.trim(true); } synchronized (this) { for (int i = getMinSectionPosition(); i <= getMaxSectionPosition(); i++) { diff --git a/worldedit-bukkit/adapters/adapter-1_21_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_4/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-1_21_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_4/PaperweightGetBlocks.java index d5ad3fb9bf..5f04b5fee3 100644 --- a/worldedit-bukkit/adapters/adapter-1_21_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_4/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-1_21_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_4/PaperweightGetBlocks.java @@ -521,19 +521,24 @@ protected > T internalCall( synchronized (lock) { lock.acquire(); // Wait until we have the lock lock.release(); + // Lock order: a section's lock (sectionLocks[i]) comes before sectionLock. Readers take them in + // that order (CharBlocks EMPTY.get -> update -> getSections). reset() takes every section's lock, + // so calling it while holding sectionLock deadlocks against a reader. Defer it until after unlock. + boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = null; - this.reset(); + resetAll = true; } else if (existingSection != getSections(false)[getSectionIndex]) { this.sections[getSectionIndex] = existingSection; - this.reset(); + resetAll = true; } else if (!Arrays.equals( update(getSectionIndex, new char[4096], true), load(layerNo) )) { + // Safe under sectionLock: it only takes this section's lock, which we already hold. this.reset(layerNo); /*} else if (lock.isModified()) { this.reset(layerNo);*/ @@ -541,6 +546,11 @@ protected > T internalCall( } finally { sectionLock.writeLock().unlock(); } + // Deferred from above: sectionLock is released, so taking every section's lock here is safe. + // Still runs before newChunkSection(this::load, ...) reads the cache below. + if (resetAll) { + this.reset(); + } PalettedContainer> biomeData = setBiomesToPalettedContainer( biomes, @@ -770,13 +780,16 @@ private void updateGet( char[] arr, int layer ) { + // reset() takes every section's lock, and a section's lock must come before sectionLock (see internalCall). + // Defer it until sectionLock is released to avoid deadlocking against a reader. + boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = new LevelChunkSection[chunkSections.length]; System.arraycopy(chunkSections, 0, this.sections, 0, chunkSections.length); - this.reset(); + resetAll = true; } if (this.sections == null) { this.sections = new LevelChunkSection[chunkSections.length]; @@ -789,6 +802,10 @@ private void updateGet( } finally { sectionLock.writeLock().unlock(); } + // Deferred reset (see above). Must run before blocks[layer] is set, or it would clear the new array. + if (resetAll) { + this.reset(); + } this.blocks[layer] = arr; } @@ -1022,11 +1039,13 @@ public boolean trim(boolean aggressive) { blockLight = new DataLayer[getSectionCount()]; sections = null; levelChunk = null; - return super.trim(true); } } finally { sectionLock.writeLock().unlock(); } + // super.trim(true) takes every section's lock, and a section's lock must come before sectionLock (see + // internalCall). Run it after sectionLock is released to avoid deadlocking against a reader. + return super.trim(true); } synchronized (this) { for (int i = getMinSectionPosition(); i <= getMaxSectionPosition(); i++) { diff --git a/worldedit-bukkit/adapters/adapter-1_21_5/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_5/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-1_21_5/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_5/PaperweightGetBlocks.java index ca76b87977..5f1fdf7f09 100644 --- a/worldedit-bukkit/adapters/adapter-1_21_5/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_5/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-1_21_5/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_5/PaperweightGetBlocks.java @@ -521,19 +521,24 @@ protected > T internalCall( synchronized (lock) { lock.acquire(); // Wait until we have the lock lock.release(); + // Lock order: a section's lock (sectionLocks[i]) comes before sectionLock. Readers take them in + // that order (CharBlocks EMPTY.get -> update -> getSections). reset() takes every section's lock, + // so calling it while holding sectionLock deadlocks against a reader. Defer it until after unlock. + boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = null; - this.reset(); + resetAll = true; } else if (existingSection != getSections(false)[getSectionIndex]) { this.sections[getSectionIndex] = existingSection; - this.reset(); + resetAll = true; } else if (!Arrays.equals( update(getSectionIndex, new char[4096], true), load(layerNo) )) { + // Safe under sectionLock: it only takes this section's lock, which we already hold. this.reset(layerNo); /*} else if (lock.isModified()) { this.reset(layerNo);*/ @@ -541,6 +546,11 @@ protected > T internalCall( } finally { sectionLock.writeLock().unlock(); } + // Deferred from above: sectionLock is released, so taking every section's lock here is safe. + // Still runs before newChunkSection(this::load, ...) reads the cache below. + if (resetAll) { + this.reset(); + } PalettedContainer> biomeData = setBiomesToPalettedContainer( biomes, @@ -769,13 +779,16 @@ private void updateGet( char[] arr, int layer ) { + // reset() takes every section's lock, and a section's lock must come before sectionLock (see internalCall). + // Defer it until sectionLock is released to avoid deadlocking against a reader. + boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = new LevelChunkSection[chunkSections.length]; System.arraycopy(chunkSections, 0, this.sections, 0, chunkSections.length); - this.reset(); + resetAll = true; } if (this.sections == null) { this.sections = new LevelChunkSection[chunkSections.length]; @@ -788,6 +801,10 @@ private void updateGet( } finally { sectionLock.writeLock().unlock(); } + // Deferred reset (see above). Must run before blocks[layer] is set, or it would clear the new array. + if (resetAll) { + this.reset(); + } this.blocks[layer] = arr; } @@ -1021,11 +1038,13 @@ public boolean trim(boolean aggressive) { blockLight = new DataLayer[getSectionCount()]; sections = null; levelChunk = null; - return super.trim(true); } } finally { sectionLock.writeLock().unlock(); } + // super.trim(true) takes every section's lock, and a section's lock must come before sectionLock (see + // internalCall). Run it after sectionLock is released to avoid deadlocking against a reader. + return super.trim(true); } synchronized (this) { for (int i = getMinSectionPosition(); i <= getMaxSectionPosition(); i++) { diff --git a/worldedit-bukkit/adapters/adapter-1_21_6/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_6/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-1_21_6/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_6/PaperweightGetBlocks.java index fa7e8c1f22..31e4734bb1 100644 --- a/worldedit-bukkit/adapters/adapter-1_21_6/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_6/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-1_21_6/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_6/PaperweightGetBlocks.java @@ -525,19 +525,24 @@ protected > T internalCall( synchronized (lock) { lock.acquire(); // Wait until we have the lock lock.release(); + // Lock order: a section's lock (sectionLocks[i]) comes before sectionLock. Readers take them in + // that order (CharBlocks EMPTY.get -> update -> getSections). reset() takes every section's lock, + // so calling it while holding sectionLock deadlocks against a reader. Defer it until after unlock. + boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = null; - this.reset(); + resetAll = true; } else if (existingSection != getSections(false)[getSectionIndex]) { this.sections[getSectionIndex] = existingSection; - this.reset(); + resetAll = true; } else if (!Arrays.equals( update(getSectionIndex, new char[4096], true), load(layerNo) )) { + // Safe under sectionLock: it only takes this section's lock, which we already hold. this.reset(layerNo); /*} else if (lock.isModified()) { this.reset(layerNo);*/ @@ -545,6 +550,11 @@ protected > T internalCall( } finally { sectionLock.writeLock().unlock(); } + // Deferred from above: sectionLock is released, so taking every section's lock here is safe. + // Still runs before newChunkSection(this::load, ...) reads the cache below. + if (resetAll) { + this.reset(); + } PalettedContainer> biomeData = setBiomesToPalettedContainer( biomes, @@ -777,13 +787,16 @@ private void updateGet( char[] arr, int layer ) { + // reset() takes every section's lock, and a section's lock must come before sectionLock (see internalCall). + // Defer it until sectionLock is released to avoid deadlocking against a reader. + boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = new LevelChunkSection[chunkSections.length]; System.arraycopy(chunkSections, 0, this.sections, 0, chunkSections.length); - this.reset(); + resetAll = true; } if (this.sections == null) { this.sections = new LevelChunkSection[chunkSections.length]; @@ -796,6 +809,10 @@ private void updateGet( } finally { sectionLock.writeLock().unlock(); } + // Deferred reset (see above). Must run before blocks[layer] is set, or it would clear the new array. + if (resetAll) { + this.reset(); + } this.blocks[layer] = arr; } @@ -1029,11 +1046,13 @@ public boolean trim(boolean aggressive) { blockLight = new DataLayer[getSectionCount()]; sections = null; levelChunk = null; - return super.trim(true); } } finally { sectionLock.writeLock().unlock(); } + // super.trim(true) takes every section's lock, and a section's lock must come before sectionLock (see + // internalCall). Run it after sectionLock is released to avoid deadlocking against a reader. + return super.trim(true); } synchronized (this) { for (int i = getMinSectionPosition(); i <= getMaxSectionPosition(); i++) { diff --git a/worldedit-bukkit/adapters/adapter-1_21_9/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_9/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-1_21_9/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_9/PaperweightGetBlocks.java index 861673840b..64314784fc 100644 --- a/worldedit-bukkit/adapters/adapter-1_21_9/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_9/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-1_21_9/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_9/PaperweightGetBlocks.java @@ -525,19 +525,24 @@ protected > T internalCall( synchronized (lock) { lock.acquire(); // Wait until we have the lock lock.release(); + // Lock order: a section's lock (sectionLocks[i]) comes before sectionLock. Readers take them in + // that order (CharBlocks EMPTY.get -> update -> getSections). reset() takes every section's lock, + // so calling it while holding sectionLock deadlocks against a reader. Defer it until after unlock. + boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = null; - this.reset(); + resetAll = true; } else if (existingSection != getSections(false)[getSectionIndex]) { this.sections[getSectionIndex] = existingSection; - this.reset(); + resetAll = true; } else if (!Arrays.equals( update(getSectionIndex, new char[4096], true), load(layerNo) )) { + // Safe under sectionLock: it only takes this section's lock, which we already hold. this.reset(layerNo); /*} else if (lock.isModified()) { this.reset(layerNo);*/ @@ -545,6 +550,11 @@ protected > T internalCall( } finally { sectionLock.writeLock().unlock(); } + // Deferred from above: sectionLock is released, so taking every section's lock here is safe. + // Still runs before newChunkSection(this::load, ...) reads the cache below. + if (resetAll) { + this.reset(); + } PalettedContainer> biomeData = setBiomesToPalettedContainer( biomes, @@ -782,13 +792,16 @@ private void updateGet( char[] arr, int layer ) { + // reset() takes every section's lock, and a section's lock must come before sectionLock (see internalCall). + // Defer it until sectionLock is released to avoid deadlocking against a reader. + boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = new LevelChunkSection[chunkSections.length]; System.arraycopy(chunkSections, 0, this.sections, 0, chunkSections.length); - this.reset(); + resetAll = true; } if (this.sections == null) { this.sections = new LevelChunkSection[chunkSections.length]; @@ -801,6 +814,10 @@ private void updateGet( } finally { sectionLock.writeLock().unlock(); } + // Deferred reset (see above). Must run before blocks[layer] is set, or it would clear the new array. + if (resetAll) { + this.reset(); + } this.blocks[layer] = arr; } @@ -1034,11 +1051,13 @@ public boolean trim(boolean aggressive) { blockLight = new DataLayer[getSectionCount()]; sections = null; levelChunk = null; - return super.trim(true); } } finally { sectionLock.writeLock().unlock(); } + // super.trim(true) takes every section's lock, and a section's lock must come before sectionLock (see + // internalCall). Run it after sectionLock is released to avoid deadlocking against a reader. + return super.trim(true); } synchronized (this) { for (int i = getMinSectionPosition(); i <= getMaxSectionPosition(); i++) { diff --git a/worldedit-bukkit/adapters/adapter-26.1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v26_1/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-26.1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v26_1/PaperweightGetBlocks.java index b2bd05ee5d..101fd7c792 100644 --- a/worldedit-bukkit/adapters/adapter-26.1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v26_1/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-26.1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v26_1/PaperweightGetBlocks.java @@ -521,19 +521,24 @@ protected > T internalCall( synchronized (lock) { lock.acquire(); // Wait until we have the lock lock.release(); + // Lock order: a section's lock (sectionLocks[i]) comes before sectionLock. Readers take them in + // that order (CharBlocks EMPTY.get -> update -> getSections). reset() takes every section's lock, + // so calling it while holding sectionLock deadlocks against a reader. Defer it until after unlock. + boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = null; - this.reset(); + resetAll = true; } else if (existingSection != getSections(false)[getSectionIndex]) { this.sections[getSectionIndex] = existingSection; - this.reset(); + resetAll = true; } else if (!Arrays.equals( update(getSectionIndex, new char[4096], true), load(layerNo) )) { + // Safe under sectionLock: it only takes this section's lock, which we already hold. this.reset(layerNo); /*} else if (lock.isModified()) { this.reset(layerNo);*/ @@ -541,6 +546,11 @@ protected > T internalCall( } finally { sectionLock.writeLock().unlock(); } + // Deferred from above: sectionLock is released, so taking every section's lock here is safe. + // Still runs before newChunkSection(this::load, ...) reads the cache below. + if (resetAll) { + this.reset(); + } PalettedContainer> biomeData = setBiomesToPalettedContainer( biomes, @@ -775,13 +785,16 @@ private void updateGet( char[] arr, int layer ) { + // reset() takes every section's lock, and a section's lock must come before sectionLock (see internalCall). + // Defer it until sectionLock is released to avoid deadlocking against a reader. + boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = new LevelChunkSection[chunkSections.length]; System.arraycopy(chunkSections, 0, this.sections, 0, chunkSections.length); - this.reset(); + resetAll = true; } if (this.sections == null) { this.sections = new LevelChunkSection[chunkSections.length]; @@ -794,6 +807,10 @@ private void updateGet( } finally { sectionLock.writeLock().unlock(); } + // Deferred reset (see above). Must run before blocks[layer] is set, or it would clear the new array. + if (resetAll) { + this.reset(); + } this.blocks[layer] = arr; } @@ -1027,11 +1044,13 @@ public boolean trim(boolean aggressive) { blockLight = new DataLayer[getSectionCount()]; sections = null; levelChunk = null; - return super.trim(true); } } finally { sectionLock.writeLock().unlock(); } + // super.trim(true) takes every section's lock, and a section's lock must come before sectionLock (see + // internalCall). Run it after sectionLock is released to avoid deadlocking against a reader. + return super.trim(true); } synchronized (this) { for (int i = getMinSectionPosition(); i <= getMaxSectionPosition(); i++) { diff --git a/worldedit-bukkit/adapters/adapter-26.2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v26_2/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-26.2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v26_2/PaperweightGetBlocks.java index 342afb5921..8ed7c286b2 100644 --- a/worldedit-bukkit/adapters/adapter-26.2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v26_2/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-26.2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v26_2/PaperweightGetBlocks.java @@ -523,19 +523,24 @@ protected > T internalCall( synchronized (lock) { lock.acquire(); // Wait until we have the lock lock.release(); + // Lock order: a section's lock (sectionLocks[i]) comes before sectionLock. Readers take them in + // that order (CharBlocks EMPTY.get -> update -> getSections). reset() takes every section's lock, + // so calling it while holding sectionLock deadlocks against a reader. Defer it until after unlock. + boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = null; - this.reset(); + resetAll = true; } else if (existingSection != getSections(false)[getSectionIndex]) { this.sections[getSectionIndex] = existingSection; - this.reset(); + resetAll = true; } else if (!Arrays.equals( update(getSectionIndex, new char[4096], true), load(layerNo) )) { + // Safe under sectionLock: it only takes this section's lock, which we already hold. this.reset(layerNo); /*} else if (lock.isModified()) { this.reset(layerNo);*/ @@ -543,6 +548,11 @@ protected > T internalCall( } finally { sectionLock.writeLock().unlock(); } + // Deferred from above: sectionLock is released, so taking every section's lock here is safe. + // Still runs before newChunkSection(this::load, ...) reads the cache below. + if (resetAll) { + this.reset(); + } PalettedContainer> biomeData = setBiomesToPalettedContainer( biomes, @@ -780,13 +790,16 @@ private void updateGet( char[] arr, int layer ) { + // reset() takes every section's lock, and a section's lock must come before sectionLock (see internalCall). + // Defer it until sectionLock is released to avoid deadlocking against a reader. + boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = new LevelChunkSection[chunkSections.length]; System.arraycopy(chunkSections, 0, this.sections, 0, chunkSections.length); - this.reset(); + resetAll = true; } if (this.sections == null) { this.sections = new LevelChunkSection[chunkSections.length]; @@ -799,6 +812,10 @@ private void updateGet( } finally { sectionLock.writeLock().unlock(); } + // Deferred reset (see above). Must run before blocks[layer] is set, or it would clear the new array. + if (resetAll) { + this.reset(); + } this.blocks[layer] = arr; } @@ -1032,11 +1049,13 @@ public boolean trim(boolean aggressive) { blockLight = new DataLayer[getSectionCount()]; sections = null; levelChunk = null; - return super.trim(true); } } finally { sectionLock.writeLock().unlock(); } + // super.trim(true) takes every section's lock, and a section's lock must come before sectionLock (see + // internalCall). Run it after sectionLock is released to avoid deadlocking against a reader. + return super.trim(true); } synchronized (this) { for (int i = getMinSectionPosition(); i <= getMaxSectionPosition(); i++) { diff --git a/worldedit-bukkit/adapters/adapter-26.3/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v26_3/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-26.3/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v26_3/PaperweightGetBlocks.java index 82db6c917e..75b3147983 100644 --- a/worldedit-bukkit/adapters/adapter-26.3/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v26_3/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-26.3/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v26_3/PaperweightGetBlocks.java @@ -523,19 +523,24 @@ protected > T internalCall( synchronized (lock) { lock.acquire(); // Wait until we have the lock lock.release(); + // Lock order: a section's lock (sectionLocks[i]) comes before sectionLock. Readers take them in + // that order (CharBlocks EMPTY.get -> update -> getSections). reset() takes every section's lock, + // so calling it while holding sectionLock deadlocks against a reader. Defer it until after unlock. + boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = null; - this.reset(); + resetAll = true; } else if (existingSection != getSections(false)[getSectionIndex]) { this.sections[getSectionIndex] = existingSection; - this.reset(); + resetAll = true; } else if (!Arrays.equals( update(getSectionIndex, new char[4096], true), load(layerNo) )) { + // Safe under sectionLock: it only takes this section's lock, which we already hold. this.reset(layerNo); /*} else if (lock.isModified()) { this.reset(layerNo);*/ @@ -543,6 +548,11 @@ protected > T internalCall( } finally { sectionLock.writeLock().unlock(); } + // Deferred from above: sectionLock is released, so taking every section's lock here is safe. + // Still runs before newChunkSection(this::load, ...) reads the cache below. + if (resetAll) { + this.reset(); + } PalettedContainer> biomeData = setBiomesToPalettedContainer( biomes, @@ -780,13 +790,16 @@ private void updateGet( char[] arr, int layer ) { + // reset() takes every section's lock, and a section's lock must come before sectionLock (see internalCall). + // Defer it until sectionLock is released to avoid deadlocking against a reader. + boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = new LevelChunkSection[chunkSections.length]; System.arraycopy(chunkSections, 0, this.sections, 0, chunkSections.length); - this.reset(); + resetAll = true; } if (this.sections == null) { this.sections = new LevelChunkSection[chunkSections.length]; @@ -799,6 +812,10 @@ private void updateGet( } finally { sectionLock.writeLock().unlock(); } + // Deferred reset (see above). Must run before blocks[layer] is set, or it would clear the new array. + if (resetAll) { + this.reset(); + } this.blocks[layer] = arr; } @@ -1032,11 +1049,13 @@ public boolean trim(boolean aggressive) { blockLight = new DataLayer[getSectionCount()]; sections = null; levelChunk = null; - return super.trim(true); } } finally { sectionLock.writeLock().unlock(); } + // super.trim(true) takes every section's lock, and a section's lock must come before sectionLock (see + // internalCall). Run it after sectionLock is released to avoid deadlocking against a reader. + return super.trim(true); } synchronized (this) { for (int i = getMinSectionPosition(); i <= getMaxSectionPosition(); i++) { From 87291bf3c4bf1cd3de6541c660e7a6f5dcc33dec Mon Sep 17 00:00:00 2001 From: Matt <4009945+MattBDev@users.noreply.github.com> Date: Sun, 8 Nov 2026 18:12:09 -0500 Subject: [PATCH 2/5] Revert "fix: avoid section lock-order deadlock in PaperweightGetBlocks" This reverts commit dbb8da04d329ef6dbf93111c0a6dc154721c4b98. --- .../fawe/v1_21_R1/PaperweightGetBlocks.java | 27 +++---------------- .../fawe/v1_21_11/PaperweightGetBlocks.java | 27 +++---------------- .../fawe/v1_21_4/PaperweightGetBlocks.java | 27 +++---------------- .../fawe/v1_21_5/PaperweightGetBlocks.java | 27 +++---------------- .../fawe/v1_21_6/PaperweightGetBlocks.java | 27 +++---------------- .../fawe/v1_21_9/PaperweightGetBlocks.java | 27 +++---------------- .../impl/fawe/v26_1/PaperweightGetBlocks.java | 27 +++---------------- .../impl/fawe/v26_2/PaperweightGetBlocks.java | 27 +++---------------- .../impl/fawe/v26_3/PaperweightGetBlocks.java | 27 +++---------------- 9 files changed, 36 insertions(+), 207 deletions(-) diff --git a/worldedit-bukkit/adapters/adapter-1_21/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_R1/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-1_21/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_R1/PaperweightGetBlocks.java index d96706e6d8..05bcded740 100644 --- a/worldedit-bukkit/adapters/adapter-1_21/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_R1/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-1_21/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_R1/PaperweightGetBlocks.java @@ -529,21 +529,16 @@ protected > T internalCall( synchronized (lock) { lock.acquire(); // Wait until we have the lock lock.release(); - // Lock order: a section's lock (sectionLocks[i]) comes before sectionLock. Readers take them in - // that order (CharBlocks EMPTY.get -> update -> getSections). reset() takes every section's lock, - // so calling it while holding sectionLock deadlocks against a reader. Defer it until after unlock. - boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = null; - resetAll = true; + this.reset(); } else if (existingSection != getSections(false)[getSectionIndex]) { this.sections[getSectionIndex] = existingSection; - resetAll = true; + this.reset(); } else if (!Arrays.equals(update(getSectionIndex, new char[4096], true), load(layerNo))) { - // Safe under sectionLock: it only takes this section's lock, which we already hold. this.reset(layerNo); /*} else if (lock.isModified()) { this.reset(layerNo);*/ @@ -551,11 +546,6 @@ protected > T internalCall( } finally { sectionLock.writeLock().unlock(); } - // Deferred from above: sectionLock is released, so taking every section's lock here is safe. - // Still runs before newChunkSection(this::load, ...) reads the cache below. - if (resetAll) { - this.reset(); - } PalettedContainer> biomeData = setBiomesToPalettedContainer( biomes, @@ -834,16 +824,13 @@ private void updateGet( char[] arr, int layer ) { - // reset() takes every section's lock, and a section's lock must come before sectionLock (see internalCall). - // Defer it until sectionLock is released to avoid deadlocking against a reader. - boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = new LevelChunkSection[chunkSections.length]; System.arraycopy(chunkSections, 0, this.sections, 0, chunkSections.length); - resetAll = true; + this.reset(); } if (this.sections == null) { this.sections = new LevelChunkSection[chunkSections.length]; @@ -856,10 +843,6 @@ private void updateGet( } finally { sectionLock.writeLock().unlock(); } - // Deferred reset (see above). Must run before blocks[layer] is set, or it would clear the new array. - if (resetAll) { - this.reset(); - } this.blocks[layer] = arr; } @@ -1093,13 +1076,11 @@ public boolean trim(boolean aggressive) { blockLight = new DataLayer[getSectionCount()]; sections = null; levelChunk = null; + return super.trim(true); } } finally { sectionLock.writeLock().unlock(); } - // super.trim(true) takes every section's lock, and a section's lock must come before sectionLock (see - // internalCall). Run it after sectionLock is released to avoid deadlocking against a reader. - return super.trim(true); } synchronized (this) { for (int i = getMinSectionPosition(); i <= getMaxSectionPosition(); i++) { diff --git a/worldedit-bukkit/adapters/adapter-1_21_11/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_11/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-1_21_11/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_11/PaperweightGetBlocks.java index 1d36112927..5704a1ebbb 100644 --- a/worldedit-bukkit/adapters/adapter-1_21_11/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_11/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-1_21_11/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_11/PaperweightGetBlocks.java @@ -521,24 +521,19 @@ protected > T internalCall( synchronized (lock) { lock.acquire(); // Wait until we have the lock lock.release(); - // Lock order: a section's lock (sectionLocks[i]) comes before sectionLock. Readers take them in - // that order (CharBlocks EMPTY.get -> update -> getSections). reset() takes every section's lock, - // so calling it while holding sectionLock deadlocks against a reader. Defer it until after unlock. - boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = null; - resetAll = true; + this.reset(); } else if (existingSection != getSections(false)[getSectionIndex]) { this.sections[getSectionIndex] = existingSection; - resetAll = true; + this.reset(); } else if (!Arrays.equals( update(getSectionIndex, new char[4096], true), load(layerNo) )) { - // Safe under sectionLock: it only takes this section's lock, which we already hold. this.reset(layerNo); /*} else if (lock.isModified()) { this.reset(layerNo);*/ @@ -546,11 +541,6 @@ protected > T internalCall( } finally { sectionLock.writeLock().unlock(); } - // Deferred from above: sectionLock is released, so taking every section's lock here is safe. - // Still runs before newChunkSection(this::load, ...) reads the cache below. - if (resetAll) { - this.reset(); - } PalettedContainer> biomeData = setBiomesToPalettedContainer( biomes, @@ -785,16 +775,13 @@ private void updateGet( char[] arr, int layer ) { - // reset() takes every section's lock, and a section's lock must come before sectionLock (see internalCall). - // Defer it until sectionLock is released to avoid deadlocking against a reader. - boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = new LevelChunkSection[chunkSections.length]; System.arraycopy(chunkSections, 0, this.sections, 0, chunkSections.length); - resetAll = true; + this.reset(); } if (this.sections == null) { this.sections = new LevelChunkSection[chunkSections.length]; @@ -807,10 +794,6 @@ private void updateGet( } finally { sectionLock.writeLock().unlock(); } - // Deferred reset (see above). Must run before blocks[layer] is set, or it would clear the new array. - if (resetAll) { - this.reset(); - } this.blocks[layer] = arr; } @@ -1044,13 +1027,11 @@ public boolean trim(boolean aggressive) { blockLight = new DataLayer[getSectionCount()]; sections = null; levelChunk = null; + return super.trim(true); } } finally { sectionLock.writeLock().unlock(); } - // super.trim(true) takes every section's lock, and a section's lock must come before sectionLock (see - // internalCall). Run it after sectionLock is released to avoid deadlocking against a reader. - return super.trim(true); } synchronized (this) { for (int i = getMinSectionPosition(); i <= getMaxSectionPosition(); i++) { diff --git a/worldedit-bukkit/adapters/adapter-1_21_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_4/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-1_21_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_4/PaperweightGetBlocks.java index 5f04b5fee3..d5ad3fb9bf 100644 --- a/worldedit-bukkit/adapters/adapter-1_21_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_4/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-1_21_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_4/PaperweightGetBlocks.java @@ -521,24 +521,19 @@ protected > T internalCall( synchronized (lock) { lock.acquire(); // Wait until we have the lock lock.release(); - // Lock order: a section's lock (sectionLocks[i]) comes before sectionLock. Readers take them in - // that order (CharBlocks EMPTY.get -> update -> getSections). reset() takes every section's lock, - // so calling it while holding sectionLock deadlocks against a reader. Defer it until after unlock. - boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = null; - resetAll = true; + this.reset(); } else if (existingSection != getSections(false)[getSectionIndex]) { this.sections[getSectionIndex] = existingSection; - resetAll = true; + this.reset(); } else if (!Arrays.equals( update(getSectionIndex, new char[4096], true), load(layerNo) )) { - // Safe under sectionLock: it only takes this section's lock, which we already hold. this.reset(layerNo); /*} else if (lock.isModified()) { this.reset(layerNo);*/ @@ -546,11 +541,6 @@ protected > T internalCall( } finally { sectionLock.writeLock().unlock(); } - // Deferred from above: sectionLock is released, so taking every section's lock here is safe. - // Still runs before newChunkSection(this::load, ...) reads the cache below. - if (resetAll) { - this.reset(); - } PalettedContainer> biomeData = setBiomesToPalettedContainer( biomes, @@ -780,16 +770,13 @@ private void updateGet( char[] arr, int layer ) { - // reset() takes every section's lock, and a section's lock must come before sectionLock (see internalCall). - // Defer it until sectionLock is released to avoid deadlocking against a reader. - boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = new LevelChunkSection[chunkSections.length]; System.arraycopy(chunkSections, 0, this.sections, 0, chunkSections.length); - resetAll = true; + this.reset(); } if (this.sections == null) { this.sections = new LevelChunkSection[chunkSections.length]; @@ -802,10 +789,6 @@ private void updateGet( } finally { sectionLock.writeLock().unlock(); } - // Deferred reset (see above). Must run before blocks[layer] is set, or it would clear the new array. - if (resetAll) { - this.reset(); - } this.blocks[layer] = arr; } @@ -1039,13 +1022,11 @@ public boolean trim(boolean aggressive) { blockLight = new DataLayer[getSectionCount()]; sections = null; levelChunk = null; + return super.trim(true); } } finally { sectionLock.writeLock().unlock(); } - // super.trim(true) takes every section's lock, and a section's lock must come before sectionLock (see - // internalCall). Run it after sectionLock is released to avoid deadlocking against a reader. - return super.trim(true); } synchronized (this) { for (int i = getMinSectionPosition(); i <= getMaxSectionPosition(); i++) { diff --git a/worldedit-bukkit/adapters/adapter-1_21_5/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_5/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-1_21_5/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_5/PaperweightGetBlocks.java index 5f1fdf7f09..ca76b87977 100644 --- a/worldedit-bukkit/adapters/adapter-1_21_5/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_5/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-1_21_5/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_5/PaperweightGetBlocks.java @@ -521,24 +521,19 @@ protected > T internalCall( synchronized (lock) { lock.acquire(); // Wait until we have the lock lock.release(); - // Lock order: a section's lock (sectionLocks[i]) comes before sectionLock. Readers take them in - // that order (CharBlocks EMPTY.get -> update -> getSections). reset() takes every section's lock, - // so calling it while holding sectionLock deadlocks against a reader. Defer it until after unlock. - boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = null; - resetAll = true; + this.reset(); } else if (existingSection != getSections(false)[getSectionIndex]) { this.sections[getSectionIndex] = existingSection; - resetAll = true; + this.reset(); } else if (!Arrays.equals( update(getSectionIndex, new char[4096], true), load(layerNo) )) { - // Safe under sectionLock: it only takes this section's lock, which we already hold. this.reset(layerNo); /*} else if (lock.isModified()) { this.reset(layerNo);*/ @@ -546,11 +541,6 @@ protected > T internalCall( } finally { sectionLock.writeLock().unlock(); } - // Deferred from above: sectionLock is released, so taking every section's lock here is safe. - // Still runs before newChunkSection(this::load, ...) reads the cache below. - if (resetAll) { - this.reset(); - } PalettedContainer> biomeData = setBiomesToPalettedContainer( biomes, @@ -779,16 +769,13 @@ private void updateGet( char[] arr, int layer ) { - // reset() takes every section's lock, and a section's lock must come before sectionLock (see internalCall). - // Defer it until sectionLock is released to avoid deadlocking against a reader. - boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = new LevelChunkSection[chunkSections.length]; System.arraycopy(chunkSections, 0, this.sections, 0, chunkSections.length); - resetAll = true; + this.reset(); } if (this.sections == null) { this.sections = new LevelChunkSection[chunkSections.length]; @@ -801,10 +788,6 @@ private void updateGet( } finally { sectionLock.writeLock().unlock(); } - // Deferred reset (see above). Must run before blocks[layer] is set, or it would clear the new array. - if (resetAll) { - this.reset(); - } this.blocks[layer] = arr; } @@ -1038,13 +1021,11 @@ public boolean trim(boolean aggressive) { blockLight = new DataLayer[getSectionCount()]; sections = null; levelChunk = null; + return super.trim(true); } } finally { sectionLock.writeLock().unlock(); } - // super.trim(true) takes every section's lock, and a section's lock must come before sectionLock (see - // internalCall). Run it after sectionLock is released to avoid deadlocking against a reader. - return super.trim(true); } synchronized (this) { for (int i = getMinSectionPosition(); i <= getMaxSectionPosition(); i++) { diff --git a/worldedit-bukkit/adapters/adapter-1_21_6/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_6/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-1_21_6/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_6/PaperweightGetBlocks.java index 31e4734bb1..fa7e8c1f22 100644 --- a/worldedit-bukkit/adapters/adapter-1_21_6/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_6/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-1_21_6/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_6/PaperweightGetBlocks.java @@ -525,24 +525,19 @@ protected > T internalCall( synchronized (lock) { lock.acquire(); // Wait until we have the lock lock.release(); - // Lock order: a section's lock (sectionLocks[i]) comes before sectionLock. Readers take them in - // that order (CharBlocks EMPTY.get -> update -> getSections). reset() takes every section's lock, - // so calling it while holding sectionLock deadlocks against a reader. Defer it until after unlock. - boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = null; - resetAll = true; + this.reset(); } else if (existingSection != getSections(false)[getSectionIndex]) { this.sections[getSectionIndex] = existingSection; - resetAll = true; + this.reset(); } else if (!Arrays.equals( update(getSectionIndex, new char[4096], true), load(layerNo) )) { - // Safe under sectionLock: it only takes this section's lock, which we already hold. this.reset(layerNo); /*} else if (lock.isModified()) { this.reset(layerNo);*/ @@ -550,11 +545,6 @@ protected > T internalCall( } finally { sectionLock.writeLock().unlock(); } - // Deferred from above: sectionLock is released, so taking every section's lock here is safe. - // Still runs before newChunkSection(this::load, ...) reads the cache below. - if (resetAll) { - this.reset(); - } PalettedContainer> biomeData = setBiomesToPalettedContainer( biomes, @@ -787,16 +777,13 @@ private void updateGet( char[] arr, int layer ) { - // reset() takes every section's lock, and a section's lock must come before sectionLock (see internalCall). - // Defer it until sectionLock is released to avoid deadlocking against a reader. - boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = new LevelChunkSection[chunkSections.length]; System.arraycopy(chunkSections, 0, this.sections, 0, chunkSections.length); - resetAll = true; + this.reset(); } if (this.sections == null) { this.sections = new LevelChunkSection[chunkSections.length]; @@ -809,10 +796,6 @@ private void updateGet( } finally { sectionLock.writeLock().unlock(); } - // Deferred reset (see above). Must run before blocks[layer] is set, or it would clear the new array. - if (resetAll) { - this.reset(); - } this.blocks[layer] = arr; } @@ -1046,13 +1029,11 @@ public boolean trim(boolean aggressive) { blockLight = new DataLayer[getSectionCount()]; sections = null; levelChunk = null; + return super.trim(true); } } finally { sectionLock.writeLock().unlock(); } - // super.trim(true) takes every section's lock, and a section's lock must come before sectionLock (see - // internalCall). Run it after sectionLock is released to avoid deadlocking against a reader. - return super.trim(true); } synchronized (this) { for (int i = getMinSectionPosition(); i <= getMaxSectionPosition(); i++) { diff --git a/worldedit-bukkit/adapters/adapter-1_21_9/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_9/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-1_21_9/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_9/PaperweightGetBlocks.java index 64314784fc..861673840b 100644 --- a/worldedit-bukkit/adapters/adapter-1_21_9/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_9/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-1_21_9/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_9/PaperweightGetBlocks.java @@ -525,24 +525,19 @@ protected > T internalCall( synchronized (lock) { lock.acquire(); // Wait until we have the lock lock.release(); - // Lock order: a section's lock (sectionLocks[i]) comes before sectionLock. Readers take them in - // that order (CharBlocks EMPTY.get -> update -> getSections). reset() takes every section's lock, - // so calling it while holding sectionLock deadlocks against a reader. Defer it until after unlock. - boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = null; - resetAll = true; + this.reset(); } else if (existingSection != getSections(false)[getSectionIndex]) { this.sections[getSectionIndex] = existingSection; - resetAll = true; + this.reset(); } else if (!Arrays.equals( update(getSectionIndex, new char[4096], true), load(layerNo) )) { - // Safe under sectionLock: it only takes this section's lock, which we already hold. this.reset(layerNo); /*} else if (lock.isModified()) { this.reset(layerNo);*/ @@ -550,11 +545,6 @@ protected > T internalCall( } finally { sectionLock.writeLock().unlock(); } - // Deferred from above: sectionLock is released, so taking every section's lock here is safe. - // Still runs before newChunkSection(this::load, ...) reads the cache below. - if (resetAll) { - this.reset(); - } PalettedContainer> biomeData = setBiomesToPalettedContainer( biomes, @@ -792,16 +782,13 @@ private void updateGet( char[] arr, int layer ) { - // reset() takes every section's lock, and a section's lock must come before sectionLock (see internalCall). - // Defer it until sectionLock is released to avoid deadlocking against a reader. - boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = new LevelChunkSection[chunkSections.length]; System.arraycopy(chunkSections, 0, this.sections, 0, chunkSections.length); - resetAll = true; + this.reset(); } if (this.sections == null) { this.sections = new LevelChunkSection[chunkSections.length]; @@ -814,10 +801,6 @@ private void updateGet( } finally { sectionLock.writeLock().unlock(); } - // Deferred reset (see above). Must run before blocks[layer] is set, or it would clear the new array. - if (resetAll) { - this.reset(); - } this.blocks[layer] = arr; } @@ -1051,13 +1034,11 @@ public boolean trim(boolean aggressive) { blockLight = new DataLayer[getSectionCount()]; sections = null; levelChunk = null; + return super.trim(true); } } finally { sectionLock.writeLock().unlock(); } - // super.trim(true) takes every section's lock, and a section's lock must come before sectionLock (see - // internalCall). Run it after sectionLock is released to avoid deadlocking against a reader. - return super.trim(true); } synchronized (this) { for (int i = getMinSectionPosition(); i <= getMaxSectionPosition(); i++) { diff --git a/worldedit-bukkit/adapters/adapter-26.1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v26_1/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-26.1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v26_1/PaperweightGetBlocks.java index 101fd7c792..b2bd05ee5d 100644 --- a/worldedit-bukkit/adapters/adapter-26.1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v26_1/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-26.1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v26_1/PaperweightGetBlocks.java @@ -521,24 +521,19 @@ protected > T internalCall( synchronized (lock) { lock.acquire(); // Wait until we have the lock lock.release(); - // Lock order: a section's lock (sectionLocks[i]) comes before sectionLock. Readers take them in - // that order (CharBlocks EMPTY.get -> update -> getSections). reset() takes every section's lock, - // so calling it while holding sectionLock deadlocks against a reader. Defer it until after unlock. - boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = null; - resetAll = true; + this.reset(); } else if (existingSection != getSections(false)[getSectionIndex]) { this.sections[getSectionIndex] = existingSection; - resetAll = true; + this.reset(); } else if (!Arrays.equals( update(getSectionIndex, new char[4096], true), load(layerNo) )) { - // Safe under sectionLock: it only takes this section's lock, which we already hold. this.reset(layerNo); /*} else if (lock.isModified()) { this.reset(layerNo);*/ @@ -546,11 +541,6 @@ protected > T internalCall( } finally { sectionLock.writeLock().unlock(); } - // Deferred from above: sectionLock is released, so taking every section's lock here is safe. - // Still runs before newChunkSection(this::load, ...) reads the cache below. - if (resetAll) { - this.reset(); - } PalettedContainer> biomeData = setBiomesToPalettedContainer( biomes, @@ -785,16 +775,13 @@ private void updateGet( char[] arr, int layer ) { - // reset() takes every section's lock, and a section's lock must come before sectionLock (see internalCall). - // Defer it until sectionLock is released to avoid deadlocking against a reader. - boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = new LevelChunkSection[chunkSections.length]; System.arraycopy(chunkSections, 0, this.sections, 0, chunkSections.length); - resetAll = true; + this.reset(); } if (this.sections == null) { this.sections = new LevelChunkSection[chunkSections.length]; @@ -807,10 +794,6 @@ private void updateGet( } finally { sectionLock.writeLock().unlock(); } - // Deferred reset (see above). Must run before blocks[layer] is set, or it would clear the new array. - if (resetAll) { - this.reset(); - } this.blocks[layer] = arr; } @@ -1044,13 +1027,11 @@ public boolean trim(boolean aggressive) { blockLight = new DataLayer[getSectionCount()]; sections = null; levelChunk = null; + return super.trim(true); } } finally { sectionLock.writeLock().unlock(); } - // super.trim(true) takes every section's lock, and a section's lock must come before sectionLock (see - // internalCall). Run it after sectionLock is released to avoid deadlocking against a reader. - return super.trim(true); } synchronized (this) { for (int i = getMinSectionPosition(); i <= getMaxSectionPosition(); i++) { diff --git a/worldedit-bukkit/adapters/adapter-26.2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v26_2/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-26.2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v26_2/PaperweightGetBlocks.java index 8ed7c286b2..342afb5921 100644 --- a/worldedit-bukkit/adapters/adapter-26.2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v26_2/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-26.2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v26_2/PaperweightGetBlocks.java @@ -523,24 +523,19 @@ protected > T internalCall( synchronized (lock) { lock.acquire(); // Wait until we have the lock lock.release(); - // Lock order: a section's lock (sectionLocks[i]) comes before sectionLock. Readers take them in - // that order (CharBlocks EMPTY.get -> update -> getSections). reset() takes every section's lock, - // so calling it while holding sectionLock deadlocks against a reader. Defer it until after unlock. - boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = null; - resetAll = true; + this.reset(); } else if (existingSection != getSections(false)[getSectionIndex]) { this.sections[getSectionIndex] = existingSection; - resetAll = true; + this.reset(); } else if (!Arrays.equals( update(getSectionIndex, new char[4096], true), load(layerNo) )) { - // Safe under sectionLock: it only takes this section's lock, which we already hold. this.reset(layerNo); /*} else if (lock.isModified()) { this.reset(layerNo);*/ @@ -548,11 +543,6 @@ protected > T internalCall( } finally { sectionLock.writeLock().unlock(); } - // Deferred from above: sectionLock is released, so taking every section's lock here is safe. - // Still runs before newChunkSection(this::load, ...) reads the cache below. - if (resetAll) { - this.reset(); - } PalettedContainer> biomeData = setBiomesToPalettedContainer( biomes, @@ -790,16 +780,13 @@ private void updateGet( char[] arr, int layer ) { - // reset() takes every section's lock, and a section's lock must come before sectionLock (see internalCall). - // Defer it until sectionLock is released to avoid deadlocking against a reader. - boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = new LevelChunkSection[chunkSections.length]; System.arraycopy(chunkSections, 0, this.sections, 0, chunkSections.length); - resetAll = true; + this.reset(); } if (this.sections == null) { this.sections = new LevelChunkSection[chunkSections.length]; @@ -812,10 +799,6 @@ private void updateGet( } finally { sectionLock.writeLock().unlock(); } - // Deferred reset (see above). Must run before blocks[layer] is set, or it would clear the new array. - if (resetAll) { - this.reset(); - } this.blocks[layer] = arr; } @@ -1049,13 +1032,11 @@ public boolean trim(boolean aggressive) { blockLight = new DataLayer[getSectionCount()]; sections = null; levelChunk = null; + return super.trim(true); } } finally { sectionLock.writeLock().unlock(); } - // super.trim(true) takes every section's lock, and a section's lock must come before sectionLock (see - // internalCall). Run it after sectionLock is released to avoid deadlocking against a reader. - return super.trim(true); } synchronized (this) { for (int i = getMinSectionPosition(); i <= getMaxSectionPosition(); i++) { diff --git a/worldedit-bukkit/adapters/adapter-26.3/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v26_3/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-26.3/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v26_3/PaperweightGetBlocks.java index 75b3147983..82db6c917e 100644 --- a/worldedit-bukkit/adapters/adapter-26.3/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v26_3/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-26.3/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v26_3/PaperweightGetBlocks.java @@ -523,24 +523,19 @@ protected > T internalCall( synchronized (lock) { lock.acquire(); // Wait until we have the lock lock.release(); - // Lock order: a section's lock (sectionLocks[i]) comes before sectionLock. Readers take them in - // that order (CharBlocks EMPTY.get -> update -> getSections). reset() takes every section's lock, - // so calling it while holding sectionLock deadlocks against a reader. Defer it until after unlock. - boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = null; - resetAll = true; + this.reset(); } else if (existingSection != getSections(false)[getSectionIndex]) { this.sections[getSectionIndex] = existingSection; - resetAll = true; + this.reset(); } else if (!Arrays.equals( update(getSectionIndex, new char[4096], true), load(layerNo) )) { - // Safe under sectionLock: it only takes this section's lock, which we already hold. this.reset(layerNo); /*} else if (lock.isModified()) { this.reset(layerNo);*/ @@ -548,11 +543,6 @@ protected > T internalCall( } finally { sectionLock.writeLock().unlock(); } - // Deferred from above: sectionLock is released, so taking every section's lock here is safe. - // Still runs before newChunkSection(this::load, ...) reads the cache below. - if (resetAll) { - this.reset(); - } PalettedContainer> biomeData = setBiomesToPalettedContainer( biomes, @@ -790,16 +780,13 @@ private void updateGet( char[] arr, int layer ) { - // reset() takes every section's lock, and a section's lock must come before sectionLock (see internalCall). - // Defer it until sectionLock is released to avoid deadlocking against a reader. - boolean resetAll = false; try { sectionLock.writeLock().lock(); if (this.getChunk() != nmsChunk) { this.levelChunk = nmsChunk; this.sections = new LevelChunkSection[chunkSections.length]; System.arraycopy(chunkSections, 0, this.sections, 0, chunkSections.length); - resetAll = true; + this.reset(); } if (this.sections == null) { this.sections = new LevelChunkSection[chunkSections.length]; @@ -812,10 +799,6 @@ private void updateGet( } finally { sectionLock.writeLock().unlock(); } - // Deferred reset (see above). Must run before blocks[layer] is set, or it would clear the new array. - if (resetAll) { - this.reset(); - } this.blocks[layer] = arr; } @@ -1049,13 +1032,11 @@ public boolean trim(boolean aggressive) { blockLight = new DataLayer[getSectionCount()]; sections = null; levelChunk = null; + return super.trim(true); } } finally { sectionLock.writeLock().unlock(); } - // super.trim(true) takes every section's lock, and a section's lock must come before sectionLock (see - // internalCall). Run it after sectionLock is released to avoid deadlocking against a reader. - return super.trim(true); } synchronized (this) { for (int i = getMinSectionPosition(); i <= getMaxSectionPosition(); i++) { From 491f10eb1f756a1d09f62ea5f56310130d5db094 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:19:33 +0000 Subject: [PATCH 3/5] Update dependency com.palmergames.bukkit.towny:towny to v0.103.2.15 (#3723) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> --- gradle/libs.versions.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 5c09711be1..9fb74549a8 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -16,7 +16,7 @@ worldguard-bukkit = "7.0.17" griefprevention = "18.0.0" griefdefender = "2.1.0-SNAPSHOT" residence = "6.0.2.3" -towny = "0.103.2.14" +towny = "0.103.2.15" plotsquared = "7.6.0" # Third party From eaefc3b1bb0666cc9583eb53569083511c863e1c Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:20:17 +0000 Subject: [PATCH 4/5] Update dependency at.yawk.lz4:lz4-java to v1.14.0 (#3724) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> --- gradle/libs.versions.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 9fb74549a8..fefbc82e95 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -34,7 +34,7 @@ antlr4 = "4.13.2" json-simple = "1.1.1" jlibnoise = "1.0.0" jchronic = "0.2.4a" -lz4-java = "1.12.1" +lz4-java = "1.14.0" commons-cli = "1.11.0" paster = "1.1.7" vault = "1.7.1" From 94a3a0a37a1db90cef8c469f550a764e7ead45a4 Mon Sep 17 00:00:00 2001 From: Matt <4009945+MattBDev@users.noreply.github.com> Date: Sun, 8 Nov 2026 22:14:59 -0500 Subject: [PATCH 5/5] simplify variable declarations in HistorySubCommands --- .../com/sk89q/worldedit/command/HistorySubCommands.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/HistorySubCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/HistorySubCommands.java index 38fc184150..7c982769fc 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/HistorySubCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/HistorySubCommands.java @@ -414,7 +414,7 @@ public synchronized void find( top = top.clampY(world.getMinY(), world.getMaxY()); long minTime = System.currentTimeMillis() - timeDiff; - Iterable> edits = database.getEdits(other, minTime, bot, top, false, false); + var edits = database.getEdits(other, minTime, bot, top, false, false); history = Lists.newArrayList(edits); player.setMeta(pageCommand, new SoftReference<>(history)); page = 1; @@ -450,11 +450,7 @@ public void distr( SimpleChangeSetSummary summary = edit.summarize(RegionWrapper.GLOBAL(), false); if (summary != null) { List> distr = summary.getBlockDistributionWithData(); - SelectionCommands.BlockDistributionResult distrPages = new SelectionCommands.BlockDistributionResult( - distr, - true, - pageCommand - ); + var distrPages = new SelectionCommands.BlockDistributionResult(distr, true, pageCommand); pages = new PaginationBox.MergedPaginationBox("Block Distribution", pageCommand, pages, distrPages); player.setMeta(pageCommand, new SoftReference<>(pages)); }