-
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix deadlock in world gen threads on world unload
- Loading branch information
Showing
6 changed files
with
268 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
...pongepowered/common/accessor/world/level/chunk/storage/IOWorker$PendingStoreAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* This file is part of Sponge, licensed under the MIT License (MIT). | ||
* | ||
* Copyright (c) SpongePowered <https://www.spongepowered.org> | ||
* Copyright (c) contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.spongepowered.common.accessor.world.level.chunk.storage; | ||
|
||
import net.minecraft.nbt.CompoundTag; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.gen.Accessor; | ||
|
||
@Mixin(targets = "net/minecraft/world/level/chunk/storage/IOWorker$PendingStore") | ||
public interface IOWorker$PendingStoreAccessor { | ||
|
||
@Accessor("data") @Nullable CompoundTag accessor$data(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/org/spongepowered/common/world/level/chunk/SpongeUnloadedChunkException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* This file is part of Sponge, licensed under the MIT License (MIT). | ||
* | ||
* Copyright (c) SpongePowered <https://www.spongepowered.org> | ||
* Copyright (c) contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.spongepowered.common.world.level.chunk; | ||
|
||
public final class SpongeUnloadedChunkException extends Exception { | ||
|
||
public static final SpongeUnloadedChunkException INSTANCE = new SpongeUnloadedChunkException(); | ||
|
||
private SpongeUnloadedChunkException() { | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
src/mixins/java/org/spongepowered/common/mixin/core/world/level/chunk/ChunkStatusMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* This file is part of Sponge, licensed under the MIT License (MIT). | ||
* | ||
* Copyright (c) SpongePowered <https://www.spongepowered.org> | ||
* Copyright (c) contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.spongepowered.common.mixin.core.world.level.chunk; | ||
|
||
import com.mojang.datafixers.util.Either; | ||
import net.minecraft.server.level.ChunkHolder; | ||
import net.minecraft.server.level.ServerLevel; | ||
import net.minecraft.server.level.ThreadedLevelLightEngine; | ||
import net.minecraft.server.level.WorldGenRegion; | ||
import net.minecraft.world.level.chunk.ChunkAccess; | ||
import net.minecraft.world.level.chunk.ChunkGenerator; | ||
import net.minecraft.world.level.chunk.ChunkStatus; | ||
import net.minecraft.world.level.chunk.ProtoChunk; | ||
import net.minecraft.world.level.levelgen.blending.Blender; | ||
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplateManager; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Overwrite; | ||
import org.spongepowered.common.world.level.chunk.SpongeUnloadedChunkException; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Executor; | ||
import java.util.function.Function; | ||
|
||
@Mixin(ChunkStatus.class) | ||
public abstract class ChunkStatusMixin { | ||
|
||
/** | ||
* @author aromaa - December 17th, 2023 - 1.19.4 | ||
* @reason Fixes a deadlock when the world is unloading/unloaded. | ||
* The Blender#of method calls to the IOWorker#isOldChunkAround which | ||
* submits a task to the main thread while blocking the current thread | ||
* to wait for a response. This fails when the IOWorker has been closed | ||
* as it no longer responds to further work and causes the thread to block | ||
* indefinitely. Fixes this by special casing the IOWorker#isOldChunkAround | ||
* to throw a special exception when the IOWorker has finished up its work | ||
* and catches it here to convert it to a ChunkLoadingFailure. | ||
* | ||
* See IOWorkerMixin#createOldDataForRegion | ||
*/ | ||
@Overwrite | ||
private static CompletableFuture<Either<ChunkAccess, ChunkHolder.ChunkLoadingFailure>> lambda$static$6( | ||
final ChunkStatus $$0, final Executor $$1, final ServerLevel $$2, final ChunkGenerator $$3, final StructureTemplateManager $$4, | ||
final ThreadedLevelLightEngine $$5, final Function<ChunkAccess, CompletableFuture<Either<ChunkAccess, ChunkHolder.ChunkLoadingFailure>>> $$6, | ||
final List<ChunkAccess> $$7, final ChunkAccess $$8, final boolean $$9) { | ||
if (!$$9 && $$8.getStatus().isOrAfter($$0)) { | ||
return CompletableFuture.completedFuture(Either.left($$8)); | ||
} else { | ||
final WorldGenRegion $$10 = new WorldGenRegion($$2, $$7, $$0, -1); | ||
try { //Sponge: Add try | ||
return $$3.createBiomes($$1, $$2.getChunkSource().randomState(), Blender.of($$10), $$2.structureManager().forWorldGenRegion($$10), $$8) | ||
.thenApply($$1x -> { | ||
if ($$1x instanceof ProtoChunk) { | ||
((ProtoChunk) $$1x).setStatus($$0); | ||
} | ||
|
||
return Either.left($$1x); | ||
}); | ||
} catch (final Exception e) { //Sponge start: Add catch | ||
if (e.getCause() != SpongeUnloadedChunkException.INSTANCE) { | ||
throw e; | ||
} | ||
|
||
return ChunkHolder.UNLOADED_CHUNK_FUTURE; | ||
} //Sponge end | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters