fix: clear player's history away from main thread if lock locked (#2457)

* fix: clear player's history away from main thread if lock locked
 - addresses crashing of #2448

* Correct lock usage

* remove possibility for race condition
This commit is contained in:
Jordan 2023-10-23 15:40:38 +01:00 committed by GitHub
parent 9fff77c088
commit 50cc9bc528
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -33,6 +33,7 @@ import com.fastasyncworldedit.core.limit.FaweLimit;
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;
@ -143,7 +144,7 @@ public class LocalSession implements TextureHolder {
}
});
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;
@ -405,6 +406,19 @@ public class LocalSession implements TextureHolder {
*/
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
@ -420,8 +434,8 @@ public class LocalSession implements TextureHolder {
save();
historySize = 0;
currentWorld = null;
//FAWE end
}
//FAWE end
/**
* Remember an edit session for the undo history. If the history maximum