Terasology/Exoplanet

ConcurrentModificationException in Flora geneneration

Opened this issue · 0 comments

Was doing some testing and had a single chunk break due to a ConcurrentModificationException - looks like there might be some poor map handling around ExoplanetFloraRasterizer:86 - being that chunk gen is multithreaded this could happen, yet even without multiple threads maps can be sensitive when messed with during iteration.

Involved code snippet in method getRandomFlora:

        if (!flora.isEmpty()) {
            float rand = random.nextFloat(0, 1);
            float cumulativeProbability = 0.0f;

            for (Map.Entry<Block, Float> entry : flora.entrySet()) {
                cumulativeProbability += entry.getValue();
                if (rand <= cumulativeProbability) {
                    return entry.getKey();
                }
            }
        }

Error snippet:

ConcurrentModificationException: null
	at java.util.HashMap$HashIterator.nextNode(HashMap.java:1437)
	at java.util.HashMap$EntryIterator.next(HashMap.java:1471)
	at java.util.HashMap$EntryIterator.next(HashMap.java:1469)
	at org.terasology.exoplanet.generator.rasterizers.ExoplanetFloraRasterizer.getRandomFlora(ExoplanetFloraRasterizer.java:86)
	at org.terasology.exoplanet.generator.rasterizers.ExoplanetFloraRasterizer.generateChunk(ExoplanetFloraRasterizer.java:66)
	at org.terasology.world.generation.WorldImpl.rasterizeChunk(WorldImpl.java:64)
	at org.terasology.world.generation.BaseFacetedWorldGenerator.createChunk(BaseFacetedWorldGenerator.java:75)
	at org.terasology.world.chunks.localChunkProvider.LocalChunkProvider$1.run(LocalChunkProvider.java:653)
	at org.terasology.utilities.concurrency.TaskProcessor.run(TaskProcessor.java:50)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)

Otherwise the world still seems to work well, including the portal to get there. Not sure why this was never added to the stable module lineup, must have been missed, so I'm adding it now. Belated nice work @PutawanDE ! :-)