fix: preserve packet scheduler callback mutations

This commit is contained in:
Algent committed 2026-09-04 14:04:27 +02:00
1 parent fa41ae0609
commit 052b84ffe0
3 files changed
+121 -32

No files matched your search

+11
View File
@@ -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.
@@ -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.<TMultiPart>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<Long> 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
@@ -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<TMultiPart, Long> map = new HashMap<>();
private static final HashMap<TMultiPart, Long> 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<Long> 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<TMultiPart, Long> 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<Tuple2<TMultiPart, Long>, BoxedUnit>() {
ipart.writeScheduled(mask, w);
@Override
public BoxedUnit apply(Tuple2<TMultiPart, Long> 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();
}
}