From 052b84ffe0cb6c506a7f31cba50079d877d43e14 Mon Sep 17 00:00:00 2001 From: Algent Date: Fri, 4 Sep 2026 14:04:27 +0200 Subject: [PATCH] fix: preserve packet scheduler callback mutations --- docs/migration/HISTORY.md | 11 +++ .../test/PacketSchedulerFunctionalTest.java | 74 ++++++++++++++++++- .../multipart/PacketScheduler.java | 68 +++++++++-------- 3 files changed, 121 insertions(+), 32 deletions(-) diff --git a/docs/migration/HISTORY.md b/docs/migration/HISTORY.md index ab763c5..1879e65 100644 --- a/docs/migration/HISTORY.md +++ b/docs/migration/HISTORY.md @@ -1266,3 +1266,14 @@ differences belong in the [divergence ledger](../../JAVA_MIGRATION_DIVERGENCES.m Scala shell/models; keep opcode fixes separate. The client manual row now names Stone/Glass covers and ProjectRed Inverted White Lamp microblocks, including a server rejoin. Headless probes do not establish actual client generation, GPU output or full-pack correctness; those checks remain unrecorded release gates. + +### 2026-09-04 — Packet scheduler callback regression + +- Restored the original Scala mutable hash-map `foreach` traversal behind `PacketScheduler`'s Java API. Java's + fail-fast iterator threw `ConcurrentModificationException` when a write callback scheduled another part. Keeping + the reference collection also preserves live mask updates, hash-bucket visitation and clearing after a successful + flush; no new batching or callback timing policy is introduced. +- Added two Forge regression cases for scheduling a new part during a flush and merging masks into an existing + pending entry. The first failed with the reported exception before the fix. The new-entry hash placement was + checked against the original Scala implementation under Java 8. +- Formatting, test checkstyle, all four packet-interface JVM tests and all 237 Forge server tests pass. diff --git a/src/functionalTest/java/codechicken/multipart/test/PacketSchedulerFunctionalTest.java b/src/functionalTest/java/codechicken/multipart/test/PacketSchedulerFunctionalTest.java index 5cfba80..34a2b93 100644 --- a/src/functionalTest/java/codechicken/multipart/test/PacketSchedulerFunctionalTest.java +++ b/src/functionalTest/java/codechicken/multipart/test/PacketSchedulerFunctionalTest.java @@ -52,6 +52,61 @@ class PacketSchedulerFunctionalTest { assertEquals(Collections.singletonList(0x5L), part.written); } + @Test + void callbacksCanScheduleAnotherPartDuringTheFlush() { + RecordingPart first = boundPart(1, 0); + RecordingPart second = boundPart(1, 1); + // These hashes place the new entry in a bucket still ahead of Scala's original traversal. + RecordingPart added = boundPart(1, 3); + first.onWrite = () -> PacketScheduler.schedulePacket(added, 0x4L); + second.onWrite = first.onWrite; + try { + PacketScheduler.schedulePacket(first, 0x1L); + PacketScheduler.schedulePacket(second, 0x2L); + sendScheduled(); + + assertEquals(Collections.singletonList(0x1L), first.written); + assertEquals(Collections.singletonList(0x2L), second.written); + assertEquals(Collections.singletonList(0x4L), added.written); + sendScheduled(); + assertEquals(1, first.written.size()); + assertEquals(1, second.written.size()); + assertEquals(1, added.written.size()); + } finally { + first.tile_$eq(null); + second.tile_$eq(null); + added.tile_$eq(null); + sendScheduled(); + } + } + + @Test + void callbacksMergeMasksIntoEntriesThatHaveNotBeenWrittenYet() { + RecordingPart first = boundPart(1, 0); + RecordingPart second = boundPart(1, 1); + first.onWrite = () -> PacketScheduler.schedulePacket(second, 0x4L); + second.onWrite = () -> PacketScheduler.schedulePacket(first, 0x8L); + try { + PacketScheduler.schedulePacket(first, 0x1L); + PacketScheduler.schedulePacket(second, 0x2L); + sendScheduled(); + + assertEquals(1, first.written.size()); + assertEquals(1, second.written.size()); + assertTrue( + first.written.get(0) == 0x9L && second.written.get(0) == 0x2L + || first.written.get(0) == 0x1L && second.written.get(0) == 0x6L, + "Whichever part is visited second must receive the mask added by the first callback"); + sendScheduled(); + assertEquals(1, first.written.size()); + assertEquals(1, second.written.size()); + } finally { + first.tile_$eq(null); + second.tile_$eq(null); + sendScheduled(); + } + } + @Test void aPartThatLostItsTileIsSkippedButStillCleared() { RecordingPart part = boundPart(1); @@ -98,7 +153,11 @@ class PacketSchedulerFunctionalTest { } private static RecordingPart boundPart(int maskWidth) { - RecordingPart part = new RecordingPart(maskWidth); + return boundPart(maskWidth, 0); + } + + private static RecordingPart boundPart(int maskWidth, int hash) { + RecordingPart part = new RecordingPart(maskWidth, hash); TileMultipart tile = MultipartHelper.createTileFromParts(Collections.singletonList(part)); assertNotNull(tile); World world = MinecraftServer.getServer().worldServers[0]; @@ -113,10 +172,18 @@ class PacketSchedulerFunctionalTest { private static final class RecordingPart extends TMultiPart implements IScheduledPacketPart { private final int maskWidth; + private final int hash; final List written = new ArrayList<>(); + Runnable onWrite; - RecordingPart(int maskWidth) { + RecordingPart(int maskWidth, int hash) { this.maskWidth = maskWidth; + this.hash = hash; + } + + @Override + public int hashCode() { + return hash; } @Override @@ -132,6 +199,9 @@ class PacketSchedulerFunctionalTest { @Override public void writeScheduled(long mask, MCDataOutput packet) { written.add(mask); + if (onWrite != null) { + onWrite.run(); + } } @Override diff --git a/src/main/scala/codechicken/multipart/PacketScheduler.java b/src/main/scala/codechicken/multipart/PacketScheduler.java index b71efb7..2b4113b 100644 --- a/src/main/scala/codechicken/multipart/PacketScheduler.java +++ b/src/main/scala/codechicken/multipart/PacketScheduler.java @@ -1,9 +1,11 @@ package codechicken.multipart; -import java.util.HashMap; -import java.util.Map; - import codechicken.lib.data.MCDataOutput; +import scala.Option; +import scala.Tuple2; +import scala.collection.mutable.HashMap; +import scala.runtime.AbstractFunction1; +import scala.runtime.BoxedUnit; /** * Static class for packing update data. When a specific property of a part changes and needs sending to the client, a @@ -12,7 +14,7 @@ import codechicken.lib.data.MCDataOutput; */ public final class PacketScheduler { - private static final Map map = new HashMap<>(); + private static final HashMap map = new HashMap<>(); private PacketScheduler() {} @@ -22,8 +24,8 @@ public final class PacketScheduler { throw new IllegalArgumentException("Cannot use PacketScheduler on a client world"); } - Long current = map.get(part); - map.put(part, (current == null ? 0L : current) | mask); + Option current = map.get(part); + map.put(part, (current.isEmpty() ? 0L : current.get()) | mask); } /** @@ -33,32 +35,38 @@ public final class PacketScheduler { * scope, so it is public. */ public static void sendScheduled() { - for (Map.Entry e : map.entrySet()) { - TMultiPart part = e.getKey(); - long mask = e.getValue(); - if (part.tile() != null) { - IScheduledPacketPart ipart = (IScheduledPacketPart) part; - MCDataOutput w = part.getWriteStream(); - switch (ipart.maskWidth()) { - case 1: - w.writeByte((int) mask); - break; - case 2: - w.writeShort((int) mask); - break; - case 4: - w.writeInt((int) mask); - break; - case 8: - w.writeLong(mask); - break; - default: - throw new IllegalArgumentException("Invalid maskWidth: " + ipart.maskWidth()); - } + // Callbacks may modify the schedule. Keep Scala's original traversal and visibility of pending masks. + map.foreach(new AbstractFunction1, BoxedUnit>() { - ipart.writeScheduled(mask, w); + @Override + public BoxedUnit apply(Tuple2 entry) { + TMultiPart part = entry._1(); + long mask = entry._2(); + if (part.tile() != null) { + IScheduledPacketPart ipart = (IScheduledPacketPart) part; + MCDataOutput w = part.getWriteStream(); + switch (ipart.maskWidth()) { + case 1: + w.writeByte((int) mask); + break; + case 2: + w.writeShort((int) mask); + break; + case 4: + w.writeInt((int) mask); + break; + case 8: + w.writeLong(mask); + break; + default: + throw new IllegalArgumentException("Invalid maskWidth: " + ipart.maskWidth()); + } + + ipart.writeScheduled(mask, w); + } + return BoxedUnit.UNIT; } - } + }); map.clear(); } }