Skip to content

Rare ConcurrentModificationException #11

@TheDGOfficial

Description

@TheDGOfficial
catching
java.util.ConcurrentModificationException
	at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:911) ~[?:1.8.0_452]
	at java.util.ArrayList$Itr.next(ArrayList.java:861) ~[?:1.8.0_452]
	at dev.desco.watersolver.handler.PuzzleHandler$onClientTick$1.invokeSuspend(PuzzleHandler.kt:61) ~[PuzzleHandler$onClientTick$1.class:?]
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) [Essential%20(forge_1.8.9).processed.jar:?]
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104) [Essential%20(forge_1.8.9).processed.jar:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1147) [?:1.8.0_452]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_452]
	at java.lang.Thread.run(Thread.java:750) [?:1.8.0_452]

Will make a PR when I have free time with the following patch after verifying it fixes the issue.

diff --git a/src/main/kotlin/dev/desco/watersolver/handler/PuzzleHandler.kt b/src/main/kotlin/dev/desco/watersolver/handler/PuzzleHandler.kt
index 9b0c4fd..f8eec27 100644
--- a/src/main/kotlin/dev/desco/watersolver/handler/PuzzleHandler.kt
+++ b/src/main/kotlin/dev/desco/watersolver/handler/PuzzleHandler.kt
@@ -41,6 +41,7 @@ object PuzzleHandler {
         val world = Minecraft.getMinecraft().theWorld ?: return
         if (ticks % 20 == 0) {
             if (variant == -1 && (job == null || job?.isCancelled == true || job?.isCompleted == true)) {
+                val tileEntities = world.loadedTileEntityList.toList() // creates a new list referencing the same elements (except that the entries will not be removed or added to this list while we are iterating, unlike the loadedTileEntityList), need this to avoid ConcurrentModificationException since we will be on the job thread below when iterating it.
                 job = WaterSolverMod.launch {
                     prevInWaterRoom = inWaterRoom
                     inWaterRoom = false
@@ -58,7 +59,7 @@ object PuzzleHandler {
 
                     val xRange = player.posX.toInt() - 25..player.posX.toInt() + 25
                     val zRange = player.posZ.toInt() - 25..player.posZ.toInt() + 25
-                    world.loadedTileEntityList.find {
+                    tileEntities.find {
                         it is TileEntityChest && it.pos.y == 56 && it.numPlayersUsing == 0 &&
                                 it.pos.x in xRange && it.pos.z in zRange &&
                                 world.getBlockState(it.pos.down()).block == Blocks.stone &&
@@ -290,4 +291,4 @@ object PuzzleHandler {
                 }
             }
     }
-}
\ No newline at end of file
+}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions