Skip to content

Commit

Permalink
fix: clear player's history away from main thread if lock locked (#2457)
Browse files Browse the repository at this point in the history
* fix: clear player's history away from main thread if lock locked
 - addresses crashing of #2448

* Correct lock usage

* remove possibility for race condition
  • Loading branch information
dordsor21 authored Oct 23, 2023
1 parent 9fff77c commit 50cc9bc
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.fastasyncworldedit.core.util.BrushCache;
import com.fastasyncworldedit.core.util.MainUtil;
import com.fastasyncworldedit.core.util.StringMan;
import com.fastasyncworldedit.core.util.TaskManager;
import com.fastasyncworldedit.core.util.TextureHolder;
import com.fastasyncworldedit.core.util.TextureUtil;
import com.fastasyncworldedit.core.wrappers.WorldWrapper;
Expand Down Expand Up @@ -143,7 +144,7 @@ public Object remove(int index) {
}
});
private transient volatile Integer historyNegativeIndex;
private transient final Lock historyWriteLock = new ReentrantLock(true);
private transient final ReentrantLock historyWriteLock = new ReentrantLock(true);
private final transient Int2ObjectOpenHashMap<Tool> tools = new Int2ObjectOpenHashMap<>(0);
private transient Mask sourceMask;
private transient TextureUtil texture;
Expand Down Expand Up @@ -405,6 +406,19 @@ public void setTimezone(ZoneId timezone) {
*/
public void clearHistory() {
//FAWE start
if (Fawe.isMainThread() && !historyWriteLock.tryLock()) {
// Do not make main thread wait if we cannot immediately clear history (on player logout usually)
TaskManager.taskManager().async(this::clearHistoryTask);
return;
}
try {
clearHistoryTask();
} finally {
historyWriteLock.unlock();
}
}

private void clearHistoryTask() {
historyWriteLock.lock();
try {
// Ensure that changesets are properly removed
Expand All @@ -420,8 +434,8 @@ public void clearHistory() {
save();
historySize = 0;
currentWorld = null;
//FAWE end
}
//FAWE end

/**
* Remember an edit session for the undo history. If the history maximum
Expand Down

0 comments on commit 50cc9bc

Please sign in to comment.