From 963b19c5995b14f8980a42f13b1bd693a5b411d1 Mon Sep 17 00:00:00 2001 From: Geoff Crossland Date: Fri, 11 Aug 2017 17:27:33 +1000 Subject: [PATCH] Fix a few chunk saving race conditions * ChunkRegionLoader.c() picks an entry in the save queue, removes that entry from the save queue and then actually writes the entry to the region file. So, between the last two steps, the entry is neither in the save queue nor is it in the region file; if somebody loads the chunk again (with ChunkRegionLoader.loadChunk()) in that gap, they'll get old data. I've delayed the removal until the saving is done. * ChunkRegionLoader.c() also records the coords of the chunks it's currently saving in this.c. ChunkRegionLoader.a(ChunkCoordIntPair, NBTTagCompound), which adds an entry to the save queue, stops the addition of an entry if its coords are in this.c. Now, I'm guessing that Mojang's intended purpose for this mechanism was to prevent multiple parallel writes for the same chunk. The "stops the addition" bit above should then be something like "block until it's no longer in c"; in fact, the vanilla implementation is "discard the new state of the chunk". I've taken the easy route to solving this, by just making ChunkRegionLoader.c() synchronized (since, in normal use, only the chunk saving thread is in here). --- nms-patches/ChunkRegionLoader.patch | 87 +++++++++++++++++++++++------ 1 file changed, 70 insertions(+), 17 deletions(-) diff --git a/nms-patches/ChunkRegionLoader.patch b/nms-patches/ChunkRegionLoader.patch index d4b0ba4916..bf874a1b77 100644 --- a/nms-patches/ChunkRegionLoader.patch +++ b/nms-patches/ChunkRegionLoader.patch @@ -1,6 +1,20 @@ --- a/net/minecraft/server/ChunkRegionLoader.java +++ b/net/minecraft/server/ChunkRegionLoader.java -@@ -29,19 +29,35 @@ +@@ -19,29 +19,47 @@ + + private static final Logger a = LogManager.getLogger(); + private final Map b = Maps.newConcurrentMap(); +- private final Set c = Collections.newSetFromMap(Maps.newConcurrentMap()); ++ // CraftBukkit ++ // private final Set c = Collections.newSetFromMap(Maps.newConcurrentMap()); + private final File d; + private final DataConverterManager e; +- private boolean f; ++ // private boolean f; ++ // CraftBukkit + + public ChunkRegionLoader(File file, DataConverterManager dataconvertermanager) { + this.d = file; this.e = dataconvertermanager; } @@ -39,7 +53,7 @@ } return this.a(world, i, j, nbttagcompound); -@@ -55,7 +71,7 @@ +@@ -55,7 +73,7 @@ } @Nullable @@ -48,7 +62,7 @@ if (!nbttagcompound.hasKeyOfType("Level", 10)) { ChunkRegionLoader.a.error("Chunk file at {},{} is missing level data, skipping", Integer.valueOf(i), Integer.valueOf(j)); return null; -@@ -72,10 +88,28 @@ +@@ -72,10 +90,28 @@ ChunkRegionLoader.a.error("Chunk file at {},{} is in the wrong location; relocating. (Expected {}, {}, got {}, {})", Integer.valueOf(i), Integer.valueOf(j), Integer.valueOf(i), Integer.valueOf(j), Integer.valueOf(chunk.locX), Integer.valueOf(chunk.locZ)); nbttagcompound1.setInt("xPos", i); nbttagcompound1.setInt("zPos", j); @@ -78,16 +92,32 @@ } } } -@@ -106,20 +140,27 @@ +@@ -98,7 +134,9 @@ + } + + protected void a(ChunkCoordIntPair chunkcoordintpair, NBTTagCompound nbttagcompound) { +- if (!this.c.contains(chunkcoordintpair)) { ++ // CraftBukkit ++ // if (!this.c.contains(chunkcoordintpair)) ++ { + this.b.put(chunkcoordintpair, nbttagcompound); + } + +@@ -106,20 +144,32 @@ } public boolean a() { - if (this.b.isEmpty()) { +- if (this.f) { + // CraftBukkit start ++ return this.processSaveQueueEntry(false); ++ } ++ ++ private synchronized boolean processSaveQueueEntry(boolean logCompletion) { + Iterator> iter = this.b.entrySet().iterator(); + if (!iter.hasNext()) { -+ // CraftBukkit end - if (this.f) { ++ if (logCompletion) { ++ // CraftBukkit end ChunkRegionLoader.a.info("ThreadedAnvilChunkStorage ({}): All chunks are saved", this.d.getName()); } @@ -96,20 +126,31 @@ - ChunkCoordIntPair chunkcoordintpair = (ChunkCoordIntPair) this.b.keySet().iterator().next(); + // CraftBukkit start + Map.Entry entry = iter.next(); -+ iter.remove(); // Pop single entry + ChunkCoordIntPair chunkcoordintpair = entry.getKey(); ++ NBTTagCompound nbttagcompound = entry.getValue(); + // CraftBukkit end boolean flag; try { - this.c.add(chunkcoordintpair); +- this.c.add(chunkcoordintpair); - NBTTagCompound nbttagcompound = (NBTTagCompound) this.b.remove(chunkcoordintpair); -+ NBTTagCompound nbttagcompound = (NBTTagCompound) entry.getValue(); // CraftBukkit ++ // this.c.add(chunkcoordintpair); ++ // NBTTagCompound nbttagcompound = (NBTTagCompound) this.b.remove(chunkcoordintpair); ++ // CraftBukkit if (nbttagcompound != null) { try { -@@ -139,10 +180,14 @@ +@@ -131,7 +181,7 @@ + + flag = true; + } finally { +- this.c.remove(chunkcoordintpair); ++ this.b.remove(chunkcoordintpair, nbttagcompound); // CraftBukkit + } + + return flag; +@@ -139,10 +189,14 @@ } private void b(ChunkCoordIntPair chunkcoordintpair, NBTTagCompound nbttagcompound) throws IOException { @@ -125,15 +166,27 @@ } public void b(World world, Chunk chunk) throws IOException {} -@@ -157,6 +202,7 @@ - if (this.a()) { +@@ -151,15 +205,16 @@ + + public void c() { + try { +- this.f = true; ++ // this.f = true; // CraftBukkit + + while (true) { +- if (this.a()) { ++ if (this.processSaveQueueEntry(true)) { // CraftBukkit continue; } + break; // CraftBukkit - Fix infinite loop when saving chunks } } finally { - this.f = false; -@@ -334,6 +380,13 @@ +- this.f = false; ++ // this.f = false; // CraftBukkit + } + + } +@@ -334,6 +389,13 @@ chunk.a(nbttagcompound.getByteArray("Biomes")); } @@ -147,7 +200,7 @@ NBTTagList nbttaglist1 = nbttagcompound.getList("Entities", 10); for (int l = 0; l < nbttaglist1.size(); ++l) { -@@ -371,7 +424,7 @@ +@@ -371,7 +433,7 @@ } } @@ -156,7 +209,7 @@ } @Nullable -@@ -399,14 +452,20 @@ +@@ -399,14 +461,20 @@ } @Nullable @@ -178,7 +231,7 @@ return null; } else { if (nbttagcompound.hasKeyOfType("Passengers", 9)) { -@@ -435,8 +494,14 @@ +@@ -435,8 +503,14 @@ } }