Release interrupted DH metadata reads without cancelling shared futures

This commit is contained in:
Ludovic Marie
2026-10-04 02:49:54 +02:00
parent 1f97a8e487
commit 9a1b22aa56
16 changed files with 358 additions and 6 deletions
+6
View File
@@ -65,6 +65,8 @@ jobs:
run: python -c "from tools.build_mod import build_tick_dedup_fixture; build_tick_dedup_fixture()"
- name: Compile the private culling whitelist fixture
run: python -c "from tools.build_mod import build_culling_empty_fixture; build_culling_empty_fixture()"
- name: Compile the private terrain read shutdown fixture
run: python -c "from tools.build_mod import build_integration_fixture; build_integration_fixture('terrain-read', 'dev.horizonlab.dispatch.TerrainReadIntegration')"
- name: Install headless graphics libraries for applied-hook checks
if: runner.os == 'Linux'
run: sudo apt-get update && sudo apt-get install -y xvfb libgl1 libegl1 libvulkan1 mesa-vulkan-drivers libasound2t64 libxkbcommon0
@@ -77,6 +79,9 @@ jobs:
- name: Verify live culling whitelist registration and empty iteration
if: runner.os == 'Linux'
run: python tools/test_culling_empty.py
- name: Verify interrupted DH reads and preserved shared futures
if: runner.os == 'Linux'
run: python tools/test_terrain_read.py
- name: Upload original mod and pack manifests only
uses: actions/upload-artifact@v4
with:
@@ -91,3 +96,4 @@ jobs:
dist/dispatch-integration.json
dist/tick-dedup-integration.json
dist/culling-empty-integration.json
dist/terrain-read-integration.json
+3 -1
View File
@@ -38,7 +38,9 @@ The optional `queue-wait` pack yields during pending distant-terrain debounce re
Version 0.14 adds an isolated [empty culling whitelist experiment](docs/culling-empty.md)
that avoids allocating iterators while there are no dynamic handlers. Live
registration and nonempty callbacks retain upstream behavior. Balanced keeps it
off; whole-game gains still need hardware comparisons.
off; whole-game gains still need hardware comparisons. DH profiles also request
a scoped [interruptible blending-read wait](docs/terrain-read-wait.md) to let
background workers leave a pending metadata read during shutdown.
Version 0.13 adds an isolated [tick membership experiment](docs/tick-membership.md)
that reuses compact storage for DH's private per-tick membership checks. The
+7 -2
View File
@@ -29,8 +29,13 @@ A local warmed loop of four million empty checks allocated 128,001,248 bytes
upstream versus 1,248 with the hook. CPU was 41 ms versus 33 ms in that single
observation. These counts isolate this allocation path; JIT escape analysis can
produce different results in other calling contexts. They are not game FPS,
whole-game RAM, statistical speed estimates or Mac GPU results. Live culling
thread profiles, matching workloads and repeated hardware tests remain needed.
whole-game RAM, statistical speed estimates or Mac GPU results. Repeated matching workloads and real hardware tests remain needed. A fixed-order
Linux software-rendered pair observed about 97,445 versus 35,128 allocated bytes
per second on `CullThread`, over separate 64-second counter spans. The captured
graphics matched and both recordings excluded zero frames, but this one pair is
not a repeatable FPS or retained-RAM result. The candidate also encountered an
intermittent shutdown wait after its world had saved; the control exited normally.
See the [terrain read shutdown check](terrain-read-wait.md).
For local world smoke checks, the baseline and candidate use separate copies of
the same peaceful demo-world snapshot. An earlier capture was interrupted by a
+36
View File
@@ -0,0 +1,36 @@
# Release a blocked terrain read during shutdown
A local world test saved its chunks and closed the DH databases, then hit the
client shutdown watchdog. Its remaining non-daemon DH world-generation worker
was waiting in `IOWorker.isOldChunkAround` on a cached blending-metadata future.
The unchanged control exited normally, so the failure is intermittent; this
observation does not attribute it to the culling optimization.
The scoped redirect uses an interruptible wait only for pending blending reads
on threads named `DH-World Gen Thread[...]`. The pinned DH 3.3.4 pool uses this
name and interrupts remaining workers during shutdown. Ordinary Minecraft
callers keep `CompletableFuture.join` behavior. Completed results use the original
join path. Failed reads retain join's exception identity and wrapping behavior.
An interrupted DH waiter restores its interrupt flag and throws a completion
exception whose interrupted cause is recognized by DH's existing shutdown
handling. It never cancels or completes the cached future, which belongs to other
callers too. Chunk stores, saves, storage closure and world difficulty are untouched.
This changes cancellation of an interrupted background task, rather than terrain
results from an uninterrupted task.
Profiles with distant terrain request the fix; baseline and low-memory profiles
without DH do not. It is restricted to Minecraft 26.3 and DH 3.3.4. To opt out,
set `enabled=false` in `config/horizon-terrain-read-wait.properties` and restart.
Frame reports record `interruptible_terrain_read`, and comparison tools disclose
changes in that state.
`python tools/test_terrain_read.py` checks the actual transformed game IOWorker
inside Fabric, with controlled cached metadata futures. It forces an interrupted
pending DH read, verifies the shared future remains available to a later caller,
and checks that ordinary callers remain blocked until completion. It also
checks an interrupt already set before a pending read, successful completed
reads with an interrupt already set, cancelled
futures, exceptional completion identity, and recognition by DH's shutdown
exception utility. The private fixture does not enter release packs. This is a
correctness check, not an FPS, throughput or Mac shutdown benchmark.
@@ -0,0 +1,27 @@
package dev.horizonlab.budget;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
/** Allow the pinned DH generation pool's shutdown interrupt to release a read-only wait. */
public final class TerrainReadWait {
private TerrainReadWait() {}
public static <T> T join(CompletableFuture<T> future) {
// Ordinary game callers retain join's interrupt behavior. Completed reads use the original path.
if (future.isDone() || !Thread.currentThread().getName().startsWith("DH-World Gen Thread["))
return future.join();
try {
return future.get();
} catch (InterruptedException shutdown) {
Thread.currentThread().interrupt();
// DH recognizes this cause as shutdown. The cached future belongs to other callers too.
throw new CompletionException(shutdown);
} catch (ExecutionException | CancellationException failure) {
// Now completed: retain join's original exception identity and wrapping semantics.
return future.join();
}
}
}
@@ -98,6 +98,7 @@ public final class HorizonClient implements ClientModInitializer {
context.put("dispatch_retry_backoff", dev.horizonlab.mixin.HorizonMixinPlugin.dispatchRetryEnabled());
context.put("tick_membership_dedup", dev.horizonlab.mixin.HorizonMixinPlugin.tickDedupEnabled());
context.put("empty_culling_whitelist", dev.horizonlab.mixin.HorizonMixinPlugin.cullingEmptyEnabled());
context.put("interruptible_terrain_read", dev.horizonlab.mixin.HorizonMixinPlugin.terrainReadWaitEnabled());
context.put("adaptive_target_fps", targetFps);
context.putAll(visualSettings(client));
var device = RenderSystem.getDevice().getDeviceInfo();
@@ -0,0 +1,18 @@
package dev.horizonlab.mixin;
import dev.horizonlab.budget.TerrainReadWait;
import java.util.concurrent.CompletableFuture;
import net.minecraft.world.level.chunk.storage.IOWorker;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
/** Only blending metadata reads on pinned DH workers become interruptible; saves stay upstream. */
@Mixin(IOWorker.class)
public abstract class DhTerrainReadWaitMixin {
@Redirect(method = "isOldChunkAround", at = @At(value = "INVOKE",
target = "Ljava/util/concurrent/CompletableFuture;join()Ljava/lang/Object;"))
private Object horizon$waitForBlendingMetadata(CompletableFuture<?> future) {
return TerrainReadWait.join(future);
}
}
@@ -17,6 +17,7 @@ public final class HorizonMixinPlugin implements IMixinConfigPlugin {
private static boolean dispatchRetry;
private static boolean tickDedup;
private static boolean cullingEmpty;
private static boolean terrainReadWait;
@Override public void onLoad(String mixinPackage) {
var loader = FabricLoader.getInstance();
@@ -95,6 +96,24 @@ public final class HorizonMixinPlugin implements IMixinConfigPlugin {
} catch (Exception error) {
LoggerFactory.getLogger("horizonlab").warn("Empty culling whitelist iteration disabled: invalid configuration", error);
}
try {
Properties properties = new Properties();
var readFile = loader.getConfigDir().resolve("horizon-terrain-read-wait.properties");
if (Files.exists(readFile)) {
try (var reader = Files.newBufferedReader(readFile)) { properties.load(reader); }
}
boolean requested = Boolean.parseBoolean(properties.getProperty("enabled", "false"));
boolean supported = loader.getModContainer("distanthorizons")
.map(mod -> mod.getMetadata().getVersion().getFriendlyString().equals("3.3.4")).orElse(false)
&& loader.getModContainer("minecraft")
.map(mod -> mod.getMetadata().getVersion().getFriendlyString().equals("26.3")).orElse(false);
terrainReadWait = requested && supported;
if (requested) LoggerFactory.getLogger("horizonlab").info(
"Interruptible DH blending metadata reads: {} (requires Minecraft 26.3 and DH 3.3.4)",
terrainReadWait ? "enabled" : "unavailable");
} catch (Exception error) {
LoggerFactory.getLogger("horizonlab").warn("Interruptible terrain read disabled: invalid configuration", error);
}
}
public static boolean workerPacingEnabled() { return workerPacing; }
@@ -102,12 +121,14 @@ public final class HorizonMixinPlugin implements IMixinConfigPlugin {
public static boolean dispatchRetryEnabled() { return dispatchRetry; }
public static boolean tickDedupEnabled() { return tickDedup; }
public static boolean cullingEmptyEnabled() { return cullingEmpty; }
public static boolean terrainReadWaitEnabled() { return terrainReadWait; }
@Override public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
if (mixinClassName.endsWith("DhWorkerPacingMixin")) return workerPacing;
if (mixinClassName.endsWith("DhDebounceWaitMixin")) return debounceWait;
if (mixinClassName.endsWith("DhPriorityDispatchMixin")) return dispatchRetry;
if (mixinClassName.endsWith("DhTickDedupMixin")) return tickDedup;
if (mixinClassName.endsWith("CullingEmptyWhitelistMixin")) return cullingEmpty;
if (mixinClassName.endsWith("DhTerrainReadWaitMixin")) return terrainReadWait;
return true;
}
@Override public String getRefMapperConfig() { return null; }
@@ -4,6 +4,6 @@
"plugin": "dev.horizonlab.mixin.HorizonMixinPlugin",
"compatibilityLevel": "JAVA_25",
"mixins": ["ServerTickMixin"],
"client": ["FrameMixin", "DhWorkerPacingMixin", "DhDebounceWaitMixin", "DhPriorityDispatchMixin", "DhTickDedupMixin", "CullingEmptyWhitelistMixin"],
"client": ["FrameMixin", "DhWorkerPacingMixin", "DhDebounceWaitMixin", "DhPriorityDispatchMixin", "DhTickDedupMixin", "CullingEmptyWhitelistMixin", "DhTerrainReadWaitMixin"],
"injectors": {"defaultRequire": 1}
}
@@ -0,0 +1,155 @@
package dev.horizonlab.dispatch;
import com.seibel.distanthorizons.core.util.ExceptionUtil;
import dev.horizonlab.mixin.HorizonMixinPlugin;
import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.BitSet;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.chunk.storage.IOWorker;
import net.minecraft.world.level.chunk.storage.RegionStorageInfo;
/** Applies the redirect to a real game IOWorker, using controlled cached read futures. */
public final class TerrainReadIntegration implements ClientModInitializer {
private static final class Worker extends IOWorker {
Worker(Path path) { super(new RegionStorageInfo("fixture", Level.OVERWORLD, "horizon-read-fixture"), path, false); }
}
private record Outcome(Boolean result, Throwable error, boolean interrupted) {}
private record Call(Thread thread, CountDownLatch finished, AtomicReference<Outcome> outcome) {}
@Override public void onInitializeClient() {
ClientLifecycleEvents.CLIENT_STARTED.register(client -> {
try { exercise(); }
catch (Exception failure) { throw new AssertionError("Terrain read integration failed", failure); }
if (Boolean.getBoolean("horizon.fixture.exit")) System.exit(0);
});
}
@SuppressWarnings("unchecked")
private static void cache(Worker worker, CompletableFuture<BitSet> future) throws Exception {
var field = IOWorker.class.getDeclaredField("regionCacheForBlender");
field.setAccessible(true);
var map = (Long2ObjectLinkedOpenHashMap<CompletableFuture<BitSet>>) field.get(worker);
synchronized (map) { map.clear(); map.put(ChunkPos.pack(0, 0), future); }
}
private static Call start(Worker worker, String name, boolean initiallyInterrupted) {
var result = new AtomicReference<Outcome>();
var finished = new CountDownLatch(1);
Thread thread = new Thread(() -> {
if (initiallyInterrupted) Thread.currentThread().interrupt();
Boolean value = null; Throwable error = null;
try { value = worker.isOldChunkAround(new ChunkPos(0, 0), 0); }
catch (Throwable failure) { error = failure; }
result.set(new Outcome(value, error, Thread.currentThread().isInterrupted()));
finished.countDown();
}, name);
thread.setDaemon(true); // Fixture failures cannot strand the test JVM.
thread.start();
return new Call(thread, finished, result);
}
private static Outcome finish(Call call) throws Exception {
if (!call.finished.await(4, TimeUnit.SECONDS)) throw new AssertionError("Worker did not finish");
call.thread.join(1000);
return call.outcome.get();
}
private static void awaitPending(Call call) throws Exception {
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(4);
while (call.thread.getState() != Thread.State.WAITING) {
if (!call.thread.isAlive() || System.nanoTime() > deadline) throw new AssertionError("Read never waited");
Thread.sleep(1);
}
}
private static void pending(Worker worker, String name, boolean cancellable) throws Exception {
var future = new CompletableFuture<BitSet>();
cache(worker, future);
var call = start(worker, name, false);
try {
awaitPending(call);
call.thread.interrupt();
if (cancellable) {
var outcome = finish(call);
if (!(outcome.error instanceof CompletionException) || !ExceptionUtil.isShutdownException(outcome.error)
|| !outcome.interrupted || future.isDone()) throw new AssertionError("Shutdown changed shared future or interrupt");
} else {
if (call.finished.await(100, TimeUnit.MILLISECONDS)) throw new AssertionError("Ordinary join became interruptible");
}
future.complete(new BitSet());
var outcome = finish(call);
if (!cancellable && (!Boolean.FALSE.equals(outcome.result) || outcome.error != null || !outcome.interrupted))
throw new AssertionError("Original join result or interrupt changed");
// A later caller can still consume the shared read after cancellation of only one waiter.
if (worker.isOldChunkAround(new ChunkPos(0, 0), 0)) throw new AssertionError("Shared read was damaged");
} finally { future.complete(new BitSet()); call.thread.join(4000); }
}
private static void exercise() throws Exception {
boolean enabled = HorizonMixinPlugin.terrainReadWaitEnabled();
Path path = Files.createTempDirectory("horizon-read-fixture-");
try (var worker = new Worker(path)) {
pending(worker, "DH-World Gen Thread[fixture]", enabled);
pending(worker, "Server thread fixture", false);
pending(worker, "DH-LOD Builder Thread[fixture]", false);
var waiting = new CompletableFuture<BitSet>();
cache(worker, waiting);
var initialCall = start(worker, "DH-World Gen Thread[fixture]", true);
try {
if (enabled) {
var preInterrupted = finish(initialCall);
if (!(preInterrupted.error instanceof CompletionException) || !preInterrupted.interrupted
|| waiting.isDone() || !ExceptionUtil.isShutdownException(preInterrupted.error))
throw new AssertionError("Pending read ignored an existing shutdown interrupt");
} else {
awaitPending(initialCall);
if (initialCall.finished.await(100, TimeUnit.MILLISECONDS))
throw new AssertionError("Original pending join honored an existing interrupt");
waiting.complete(new BitSet());
var preInterrupted = finish(initialCall);
if (!Boolean.FALSE.equals(preInterrupted.result) || preInterrupted.error != null || !preInterrupted.interrupted)
throw new AssertionError("Original pre-interrupted join changed");
}
} finally { waiting.complete(new BitSet()); initialCall.thread.join(4000); }
BitSet bits = new BitSet(); bits.set(0);
cache(worker, CompletableFuture.completedFuture(bits));
var complete = finish(start(worker, "DH-World Gen Thread[fixture]", true));
if (!Boolean.TRUE.equals(complete.result) || complete.error != null || !complete.interrupted)
throw new AssertionError("Completed read changed result or interrupt");
var failed = new CompletableFuture<BitSet>();
var original = new CompletionException(new IllegalStateException("fixture failure"));
cache(worker, failed);
var call = start(worker, "DH-World Gen Thread[fixture]", false);
awaitPending(call); failed.completeExceptionally(original);
if (finish(call).error != original) throw new AssertionError("Exceptional completion identity changed");
var pendingCancelled = new CompletableFuture<BitSet>();
cache(worker, pendingCancelled);
var cancelCall = start(worker, "DH-World Gen Thread[fixture]", false);
awaitPending(cancelCall); pendingCancelled.cancel(false);
Throwable expectedCancellation = null;
try { pendingCancelled.join(); } catch (java.util.concurrent.CancellationException expected) { expectedCancellation = expected; }
var actualCancellation = finish(cancelCall).error;
if (!(actualCancellation instanceof java.util.concurrent.CancellationException)
|| !java.util.Objects.equals(actualCancellation.getMessage(), expectedCancellation.getMessage())
|| actualCancellation.getCause() != expectedCancellation.getCause())
throw new AssertionError("Pending cancellation changed join's message or cause");
var cancelled = new CompletableFuture<BitSet>(); cancelled.cancel(false);
cache(worker, cancelled);
if (!(finish(start(worker, "DH-World Gen Thread[fixture]", false)).error instanceof java.util.concurrent.CancellationException))
throw new AssertionError("Cancelled completion changed");
System.out.println("HORIZON_TERRAIN_READ_RESULT {\"enabled\":" + enabled
+ ",\"applied_io_worker_checked\":true,\"ordinary_join_checked\":true,\"shared_future_preserved\":true"
+ ",\"exception_identity_checked\":true,\"completed_interrupt_checked\":true,\"cancelled_completion_checked\":true,\"preinterrupted_pending_checked\":true}");
} finally { Files.deleteIfExists(path); }
}
}
+6
View File
@@ -158,3 +158,9 @@ and field of view at capture boundaries. If visual settings changed, the viewer
flags the run. Older reports without these fields cannot establish quality
equivalence. Snapshots cannot detect a change that was restored before the end,
and do not prove that all requested distant terrain finished rendering.
Since 0.14, profiles with Distant Horizons request an interruptible blending-read
wait for its generation workers during shutdown. Ordinary Minecraft callers and
chunk saves keep their original behavior. To disable, set enabled=false in
config/horizon-terrain-read-wait.properties and restart. This is a scoped shutdown
correctness fix, not a claim of greater FPS or faster terrain generation.
+6
View File
@@ -99,6 +99,12 @@ class PackTests(unittest.TestCase):
"config/horizon-dispatch-retry.properties", "config/horizon-tick-dedup.properties"]:
self.assertEqual(original[filename], candidate[filename])
def test_terrain_read_shutdown_fix_only_requested_with_distant_terrain(self):
for name, profile in build_pack.PROFILES.items():
files = build_pack.overrides(name, b"fixture")
self.assertEqual(files["config/horizon-terrain-read-wait.properties"],
b"enabled=true\n" if profile["lod"] else b"enabled=false\n")
def test_wrong_game_or_download_path_is_rejected(self):
for field, value in [("game_versions", ["1.21"]), ("filename", "../bad.jar"), ("url", "http://unsafe.invalid")]:
lock = copy.deepcopy(build_pack.read_lock())
+2 -1
View File
@@ -66,7 +66,7 @@ function compareReports(a,b){
else if(JSON.stringify(a[key])!==JSON.stringify(b[key]))warnings.push('JVM '+key+' differs. Treat it as an explicit experiment variable.');
}
if(a.kind==='frames'){
for(const key of ['adaptive_budget_enabled','adaptive_target_fps','precise_worker_pacing','debounce_queue_wait','wakeable_idle_wait','dispatch_retry_backoff','tick_membership_dedup','empty_culling_whitelist','dh_thread_count','dh_runtime_ratio']){
for(const key of ['adaptive_budget_enabled','adaptive_target_fps','precise_worker_pacing','debounce_queue_wait','wakeable_idle_wait','dispatch_retry_backoff','tick_membership_dedup','empty_culling_whitelist','interruptible_terrain_read','dh_thread_count','dh_runtime_ratio']){
if(key in a&&key in b&&JSON.stringify(a[key])!==JSON.stringify(b[key]))warnings.push('Experiment '+key+' differs.');
}
for(const [label,r] of [['Baseline',a],['Candidate',b]]){
@@ -127,6 +127,7 @@ function renderDetails(side){
fields.push(['Wakeable queue idle', r.wakeable_idle_wait===true?'on':r.wakeable_idle_wait===false?'off':'not captured']);
if(r.kind==='frames')fields.push(['Tick membership deduplication',r.tick_membership_dedup===true?'on':r.tick_membership_dedup===false?'off':'not captured']);
if(r.kind==='frames')fields.push(['Empty culling whitelist',r.empty_culling_whitelist===true?'on':r.empty_culling_whitelist===false?'off':'not captured']);
if(r.kind==='frames')fields.push(['Interruptible terrain read',r.interruptible_terrain_read===true?'on':r.interruptible_terrain_read===false?'off':'not captured']);
for(const [label,value]of fields)target.append(text('dt',label),text('dd',value));
}
async function loadFiles(side,files){
+2
View File
@@ -95,6 +95,8 @@ def overrides(profile_name, mod_bytes):
f"enabled={str(profile.get('tick_dedup', False)).lower()}\n").encode()
files["config/horizon-culling-empty.properties"] = (
f"enabled={str(profile.get('culling_empty', False)).lower()}\n").encode()
files["config/horizon-terrain-read-wait.properties"] = (
f"enabled={str(bool(profile['lod'])).lower()}\n").encode()
if profile["lod"]:
if not config.exists():
raise FileNotFoundError(f"Missing verified DH profile config: {config}")
+1 -1
View File
@@ -28,7 +28,7 @@ def compare(baseline, candidate):
warnings = ["World, route, warm-up, shader state and LOD cache must also match; these are not verified by this tool."]
if baseline["kind"] == "frames":
for field in ["adaptive_budget_enabled", "adaptive_target_fps", "precise_worker_pacing", "debounce_queue_wait",
"wakeable_idle_wait", "dispatch_retry_backoff", "tick_membership_dedup", "empty_culling_whitelist", "dh_thread_count", "dh_runtime_ratio"]:
"wakeable_idle_wait", "dispatch_retry_backoff", "tick_membership_dedup", "empty_culling_whitelist", "interruptible_terrain_read", "dh_thread_count", "dh_runtime_ratio"]:
if field in baseline and field in candidate and baseline[field] != candidate[field]:
warnings.append(f"Experiment {field} differs: {baseline[field]!r} vs {candidate[field]!r}.")
for name, report in [("baseline", baseline), ("candidate", candidate)]:
+66
View File
@@ -0,0 +1,66 @@
#!/usr/bin/env python3
"""Exercise interruptible blending reads on an actual transformed Minecraft IOWorker."""
import argparse
import hashlib
import json
import os
import platform
import shutil
import subprocess
import sys
try:
from . import build_mod, build_pack
except ImportError:
import build_mod
import build_pack
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--native-display", action="store_true")
args = parser.parse_args()
root = build_pack.ROOT
fixture = build_mod.build_integration_fixture("terrain-read", "dev.horizonlab.dispatch.TerrainReadIntegration")
mod = root / "dist" / f"horizon-lab-{build_pack.read_lock()['pack_version']}.jar"
original_sha256 = hashlib.sha256(mod.read_bytes()).hexdigest()
results = []
for enabled in [False, True]:
instance = root / ".local/runtime" / ("terrain-read-fixture-patched" if enabled else "terrain-read-fixture-upstream")
build_pack.prepare_instance("m2-balanced", instance, mod)
if hashlib.sha256((instance / "mods" / mod.name).read_bytes()).hexdigest() != original_sha256:
raise AssertionError("Runtime mod does not match the intended build")
shutil.copy2(fixture, instance / "mods/terrain-read-fixture.jar")
(instance / "config/horizon-terrain-read-wait.properties").write_text(f"enabled={str(enabled).lower()}\n")
command = [sys.executable, str(root / "tools/runtime.py"), "demo", "--instance", str(instance), "--heap", "3G", "--compact-headers"]
if platform.system() == "Linux" and not args.native_display:
command = ["xvfb-run", "-a", *command]
environment = os.environ.copy()
environment["_JAVA_OPTIONS"] = "-Dhorizon.fixture.exit=true"
run = subprocess.run(command, cwd=root, env=environment, text=True, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, timeout=240)
(instance / "fixture.log").write_text(run.stdout)
marker = "HORIZON_TERRAIN_READ_RESULT "
reports = [json.loads(line.split(marker, 1)[1]) for line in run.stdout.splitlines() if marker in line]
if run.returncode or len(reports) != 1:
raise RuntimeError(f"Terrain read fixture failed; inspect {instance / 'fixture.log'}")
report = reports[0]
if report["enabled"] != enabled or not all(report.get(key) for key in [
"applied_io_worker_checked", "ordinary_join_checked", "shared_future_preserved",
"exception_identity_checked", "completed_interrupt_checked", "cancelled_completion_checked", "preinterrupted_pending_checked"]):
raise AssertionError("Applied terrain read hook did not preserve the required outcomes")
results.append(report)
print(json.dumps(report), flush=True)
output = root / "dist/terrain-read-integration.json"
output.write_text(json.dumps({"schema": 1, "platform": platform.platform(), "architecture": platform.machine(),
"minecraft": "26.3", "distant_horizons": "3.3.4", "original_mod_sha256": original_sha256, "results": results,
"notes": ["Actual game IOWorker under Fabric, with controlled cached blending-read futures.",
"DH worker interruption releases only its waiter; the shared future remains consumable.",
"Ordinary callers retain join behavior; completed values, cancellation and exception identity are checked.",
"This is a shutdown correctness fixture, not an FPS or world-generation throughput benchmark."]}, indent=2) + "\n")
print(f"Applied terrain read checks passed: {output}")
if __name__ == "__main__":
main()