Close cached world packs with their generator

This commit is contained in:
piexel committed 2026-11-25 00:38:27 +01:00
1 parent a8e7abf852
commit 958f52fbbe
3 files changed
+73

No files matched your search

@@ -201,6 +201,23 @@ public class IrisData implements ExclusionStrategy, TypeAdapterFactory {
return Optional.ofNullable(dataLoaders.get(dataFolder));
}
/**
* Closes every cached loader for a pack inside the given folder. A world's generation packs
* live in its own folder and get cached by metadata reads; without this, each closed studio
* world would leave its loader in the cache for the rest of the server's life.
*/
public static void closeLoadedUnder(File root) {
if (root == null) {
return;
}
Path normalizedRoot = root.toPath().toAbsolutePath().normalize();
for (IrisData data : dataLoaders.values()) {
if (data.getDataFolder().toPath().toAbsolutePath().normalize().startsWith(normalizedRoot)) {
data.close();
}
}
}
public static void invalidateLoadedAuthoringResources(File source) {
Path requested = dataFolderIdentity(Objects.requireNonNull(source, "authoring pack source"));
for (Map.Entry<File, IrisData> entry : dataLoaders.entrySet()) {
@@ -273,6 +273,13 @@ public class BukkitChunkGenerator extends ChunkGenerator implements PlatformChun
}
}
private void closeCachedWorldPacks() {
if (world == null) {
return;
}
IrisData.closeLoadedUnder(world.worldFolder());
}
private void closeStartupTarget() {
EngineTarget target = startupTarget;
startupTarget = null;
@@ -952,6 +959,7 @@ public class BukkitChunkGenerator extends ChunkGenerator implements PlatformChun
folder.clear();
populators.clear();
closeStartupTarget();
closeCachedWorldPacks();
startupReady.completeExceptionally(new IllegalStateException("Iris generator closed during startup."));
});
dispatchAccepted.set(true);
@@ -0,0 +1,48 @@
package art.arcane.iris.pack.loading;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
public class IrisDataCloseLoadedUnderTest {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Test
public void closesOnlyLoadersInsideTheWorldFolder() throws IOException {
Path dimensions = temporaryFolder.newFolder("dimensions").toPath();
Path world = dimensions.resolve("studio");
Path epochPack = createPack(world.resolve("iris/generation/epochs/a/pack"));
Path prefixSiblingPack = createPack(dimensions.resolve("studio-other/iris/pack"));
Path sourcePack = createPack(temporaryFolder.newFolder("packs").toPath().resolve("source"));
IrisData epochData = IrisData.get(epochPack.toFile());
IrisData prefixSiblingData = IrisData.get(prefixSiblingPack.toFile());
IrisData sourceData = IrisData.get(sourcePack.toFile());
try {
IrisData.closeLoadedUnder(world.toFile());
assertTrue(IrisData.getLoaded(epochPack.toFile()).isEmpty());
assertTrue(epochData.isClosed());
assertSame(prefixSiblingData, IrisData.getLoaded(prefixSiblingPack.toFile()).orElse(null));
assertSame(sourceData, IrisData.getLoaded(sourcePack.toFile()).orElse(null));
} finally {
IrisData.getLoaded(epochPack.toFile()).ifPresent(IrisData::close);
IrisData.getLoaded(prefixSiblingPack.toFile()).ifPresent(IrisData::close);
IrisData.getLoaded(sourcePack.toFile()).ifPresent(IrisData::close);
}
}
private static Path createPack(Path pack) throws IOException {
Files.createDirectories(pack.resolve("dimensions"));
Files.writeString(pack.resolve("dimensions/" + pack.getFileName() + ".json"), "{}");
return pack;
}
}