Prevent CME when unloading InstanceContainer chunks

This commit is contained in:
themode 2020-12-18 01:45:45 +01:00
parent 943aae7397
commit c6d7dea2da
2 changed files with 8 additions and 4 deletions

View File

@ -58,7 +58,7 @@ public class InstanceContainer extends Instance {
private ChunkGenerator chunkGenerator;
// (chunk index -> chunk) map, contains all the chunks in the instance
private final Long2ObjectMap<Chunk> chunks = Long2ObjectMaps.synchronize(new Long2ObjectOpenHashMap<>());
// contains all the chunks to remove during the next instance tick
// contains all the chunks to remove during the next instance tick, should be synchronized
protected final Set<Chunk> scheduledChunksToRemove = new HashSet<>();
private final ReadWriteLock changingBlockLock = new ReentrantReadWriteLock();
@ -804,7 +804,7 @@ public class InstanceContainer extends Instance {
* Unsafe because it has to be done on the same thread as the instance/chunks tick update.
*/
protected void UNSAFE_unloadChunks() {
synchronized (this.scheduledChunksToRemove) {
synchronized (scheduledChunksToRemove) {
for (Chunk chunk : scheduledChunksToRemove) {
final int chunkX = chunk.getChunkX();
final int chunkZ = chunk.getChunkZ();

View File

@ -129,8 +129,12 @@ public final class InstanceManager {
// Unload all chunks
if (instance instanceof InstanceContainer) {
InstanceContainer instanceContainer = (InstanceContainer) instance;
instanceContainer.scheduledChunksToRemove.addAll(instanceContainer.getChunks());
instanceContainer.UNSAFE_unloadChunks();
Set<Chunk> scheduledChunksToRemove = instanceContainer.scheduledChunksToRemove;
synchronized (scheduledChunksToRemove) {
scheduledChunksToRemove.addAll(instanceContainer.getChunks());
instanceContainer.UNSAFE_unloadChunks();
}
}
instance.setRegistered(false);