From 4db1853bea3edcc6b7f4d975c598599b74327fc3 Mon Sep 17 00:00:00 2001 From: piexel Date: Tue, 10 Nov 2026 00:58:56 +0100 Subject: [PATCH] Wait for the server shutdown boundary and for replacement staging before runtime cleanup The post-stop finisher joined the server thread, which breaks Folia ordering, and server-stop quiescence bypassed mantle stages for queued generation. Cleanup now waits for the shutdown boundary (a missing boundary retains the runtime), queued generation keeps its mantle stages, and plugin disable waits for a replacement that is staging and refuses new ones. Upstream: 8c0b37c0b 2690e0878 --- .../src/main/java/art/arcane/iris/Iris.java | 43 +++--------- .../core/PendingWorldReplacementManager.java | 59 +++++++++++++++- .../arcane/iris/IrisShutdownOrderingTest.java | 31 +++++--- .../PendingWorldReplacementAdmissionTest.java | 70 +++++++++++++++++++ .../iris/core/link/IrisPapiLifecycleTest.java | 2 +- 5 files changed, 160 insertions(+), 45 deletions(-) create mode 100644 adapters/bukkit/plugin/src/test/java/art/arcane/iris/core/PendingWorldReplacementAdmissionTest.java diff --git a/adapters/bukkit/plugin/src/main/java/art/arcane/iris/Iris.java b/adapters/bukkit/plugin/src/main/java/art/arcane/iris/Iris.java index 34bb5769f..0ad5d7632 100644 --- a/adapters/bukkit/plugin/src/main/java/art/arcane/iris/Iris.java +++ b/adapters/bukkit/plugin/src/main/java/art/arcane/iris/Iris.java @@ -202,7 +202,6 @@ public class Iris extends VolmitPlugin implements Listener, ReloadAware { private final PendingWorldDeleteQueue pendingWorldDeletes = new PendingWorldDeleteQueue(this); private final PendingWorldReplacementManager pendingWorldReplacements = new PendingWorldReplacementManager(this); private volatile SettingsHotloadWatch settingsHotloadWatch; - private volatile Thread serverLifecycleThread; public static VolmitSender getSender() { if (sender == null) { @@ -734,7 +733,6 @@ public class Iris extends VolmitPlugin implements Listener, ReloadAware { public void addShutdownHook() { removeShutdownHook(); - serverLifecycleThread = Thread.currentThread(); shutdownHook = new Thread(this::runShutdownHook, "Iris-ShutdownHook"); try { Runtime.getRuntime().addShutdownHook(shutdownHook); @@ -865,6 +863,7 @@ public class Iris extends VolmitPlugin implements Listener, ReloadAware { } public void onDisable() { + pendingWorldReplacements.awaitStagingForShutdown(); teardownPapi(); boolean serverStopping = IrisToolbelt.isServerStopping(); boolean restartingAtStartupBoundary = startupBoundaryRestart.get(); @@ -982,7 +981,7 @@ public class Iris extends VolmitPlugin implements Listener, ReloadAware { if (generator == null) { continue; } - IrisToolbelt.beginWorldMaintenance(world, reason, true); + IrisToolbelt.beginWorldMaintenance(world, reason); if (!deferredShutdownGenerators.contains(generator)) { deferredShutdownGenerators.add(generator); } @@ -994,51 +993,31 @@ public class Iris extends VolmitPlugin implements Listener, ReloadAware { if (!postStopFinisherStarted.compareAndSet(false, true)) { return; } - Thread activeServerThread = serverLifecycleThread; - if (activeServerThread == null) { - Iris.warn("Iris could not start its post-stop runtime finisher because the server lifecycle thread is unavailable."); - return; - } - - Thread finisher = new Thread(() -> { - if (!awaitServerThreadTermination(activeServerThread)) { - return; - } - finishDeferredRuntimeTeardown("post-server-stop", 30L); - }, "Iris-PostStop-Finisher"); + Thread finisher = new Thread(() -> finishRuntimeAfterServerShutdown("post-server-stop"), + "Iris-PostStop-Finisher"); finisher.setDaemon(false); finisher.start(); } - static boolean awaitServerThreadTermination(Thread serverThread) { - if (serverThread == null || serverThread == Thread.currentThread()) { - return false; - } - try { - serverThread.join(); - return true; - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - Iris.reportError("Iris post-stop runtime finisher was interrupted.", e); - return false; - } - } - private void runShutdownHook() { if (startupBoundaryRestart.get()) { finishDeferredRuntimeTeardown("startup-boundary-restart-hook", 30L); return; } + finishRuntimeAfterServerShutdown("shutdown-hook"); + } + + private void finishRuntimeAfterServerShutdown(String reason) { if (!awaitServerShutdownBoundary()) { - Iris.warn("Iris skipped JVM-hook runtime teardown because Paper did not reach its post-world-close boundary."); + Iris.warn("Iris retained its runtime and plugin class loader because the server did not reach its post-world-close boundary."); return; } - finishDeferredRuntimeTeardown("shutdown-hook", 30L); + finishDeferredRuntimeTeardown(reason, 30L); } private boolean awaitServerShutdownBoundary() { if (!INMS.isBound()) { - return true; + return false; } try { return INMS.get().awaitServerShutdownBoundary( diff --git a/adapters/bukkit/plugin/src/main/java/art/arcane/iris/core/PendingWorldReplacementManager.java b/adapters/bukkit/plugin/src/main/java/art/arcane/iris/core/PendingWorldReplacementManager.java index 0cde80b27..c7c0384e0 100644 --- a/adapters/bukkit/plugin/src/main/java/art/arcane/iris/core/PendingWorldReplacementManager.java +++ b/adapters/bukkit/plugin/src/main/java/art/arcane/iris/core/PendingWorldReplacementManager.java @@ -55,6 +55,7 @@ import java.util.Set; import java.util.UUID; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -73,6 +74,9 @@ public final class PendingWorldReplacementManager implements Listener { private volatile UUID overworldEntryAwaitingVerification; private volatile World overworldEntryWorld; private volatile boolean paperEntryListenerRegistered; + private final Object stagingMonitor = new Object(); + private boolean stagingAdmissionOpen = true; + private StagingAdmission activeStaging; public PendingWorldReplacementManager(Iris plugin) { this.plugin = Objects.requireNonNull(plugin, "plugin"); @@ -135,7 +139,7 @@ public final class PendingWorldReplacementManager implements Listener { LifecycleOperationCoordinator.Domain.WORLD_MUTATION, LifecycleOperationCoordinator.OperationKind.WORLD_REPLACE, requiredWorldKey.toString() - )) { + ); StagingAdmission stagingAdmission = admitStaging(requiredWorldKey)) { if (findTransaction(requiredWorldSlotKey) != null) { throw new IOException("A replacement is already pending for " + requiredWorldKey + "."); } @@ -251,6 +255,29 @@ public final class PendingWorldReplacementManager implements Listener { } } + public void awaitStagingForShutdown() { + StagingAdmission staging; + synchronized (stagingMonitor) { + stagingAdmissionOpen = false; + staging = activeStaging; + } + if (staging == null) { + return; + } + Iris.warn("Waiting for the replacement of " + staging.worldKey + " to finish staging before Iris shuts down."); + boolean interrupted = false; + while (staging.finished.getCount() > 0L) { + try { + staging.finished.await(); + } catch (InterruptedException e) { + interrupted = true; + } + } + if (interrupted) { + Thread.currentThread().interrupt(); + } + } + public synchronized void processPendingStartupReplacements() { ArrayList failures = new ArrayList<>(); ArrayList restartBoundaries = new ArrayList<>(); @@ -993,6 +1020,17 @@ public final class PendingWorldReplacementManager implements Listener { return plugin.getDataFolder().toPath().toAbsolutePath().normalize(); } + StagingAdmission admitStaging(NamespacedKey worldKey) throws IOException { + synchronized (stagingMonitor) { + if (!stagingAdmissionOpen) { + throw new IOException("Iris is shutting down. Run the replacement again after the server starts."); + } + StagingAdmission staging = new StagingAdmission(worldKey); + activeStaging = staging; + return staging; + } + } + private synchronized void loadOverworldEntryGuard() throws IOException { ExactWorldSlotPathPolicy.Target target = resolveTarget(OVERWORLD_KEY); Optional loaded = WorldReplacementEntryGuard.load(target.worldDirectory()); @@ -1157,6 +1195,25 @@ public final class PendingWorldReplacementManager implements Listener { } } + final class StagingAdmission implements AutoCloseable { + private final NamespacedKey worldKey; + private final CountDownLatch finished = new CountDownLatch(1); + + private StagingAdmission(NamespacedKey worldKey) { + this.worldKey = worldKey; + } + + @Override + public void close() { + synchronized (stagingMonitor) { + if (activeStaging == this) { + activeStaging = null; + } + } + finished.countDown(); + } + } + private static final class RestartBoundaryRequired extends IOException { private RestartBoundaryRequired(String message) { super(message); diff --git a/adapters/bukkit/plugin/src/test/java/art/arcane/iris/IrisShutdownOrderingTest.java b/adapters/bukkit/plugin/src/test/java/art/arcane/iris/IrisShutdownOrderingTest.java index d6b38919e..c12300f44 100644 --- a/adapters/bukkit/plugin/src/test/java/art/arcane/iris/IrisShutdownOrderingTest.java +++ b/adapters/bukkit/plugin/src/test/java/art/arcane/iris/IrisShutdownOrderingTest.java @@ -24,9 +24,10 @@ public class IrisShutdownOrderingTest { String source = Files.readString(Path.of(System.getProperty("iris.startupSource"))); String onDisable = section(source, "public void onDisable()", "public void onPreUnload"); String quiesce = section(source, "private void quiesceRuntimeForServerShutdown", "private void startPostStopFinisher"); - String shutdownHook = section(source, "private void runShutdownHook()", "private boolean awaitServerShutdownBoundary"); - String finisher = section(source, "private void startPostStopFinisher()", "static boolean awaitServerThreadTermination"); - String serverJoin = section(source, "static boolean awaitServerThreadTermination", "private void runShutdownHook"); + String shutdownHook = section(source, "private void runShutdownHook()", "private void finishRuntimeAfterServerShutdown"); + String boundaryFinish = section(source, "private void finishRuntimeAfterServerShutdown", "private boolean awaitServerShutdownBoundary"); + String boundaryAwait = section(source, "private boolean awaitServerShutdownBoundary", "private void finishDeferredRuntimeTeardown"); + String finisher = section(source, "private void startPostStopFinisher()", "private void runShutdownHook"); String generatorSource = Files.readString(Path.of(System.getProperty("iris.bukkitChunkGeneratorSource"))); String generatorQuiesce = section(generatorSource, "public void quiesceForServerShutdown()", "public boolean isStudio()"); @@ -43,17 +44,19 @@ public class IrisShutdownOrderingTest { "startupBoundaryRestart.get()", "finishDeferredRuntimeTeardown(\"startup-boundary-restart-hook\", 30L)", "return;", - "awaitServerShutdownBoundary()", - "finishDeferredRuntimeTeardown(\"shutdown-hook\", 30L)"); + "finishRuntimeAfterServerShutdown(\"shutdown-hook\")"); assertOrdered(finisher, + "finishRuntimeAfterServerShutdown(\"post-server-stop\")", "finisher.setDaemon(false)", "finisher.start()"); - assertOrdered(finisher, - "awaitServerThreadTermination(activeServerThread)", - "finishDeferredRuntimeTeardown(\"post-server-stop\", 30L)"); - assertOrdered(serverJoin, - "serverThread == Thread.currentThread()", - "serverThread.join()"); + assertOrdered(boundaryFinish, + "awaitServerShutdownBoundary()", + "return;", + "finishDeferredRuntimeTeardown(reason, 30L)"); + assertTrue("A missing shutdown boundary must retain the runtime", + boundaryAwait.contains("if (!INMS.isBound()) {\n return false;")); + assertFalse("Runtime cleanup must wait for the shutdown boundary, not the server thread", + source.contains("serverThread.join()") || source.contains("serverLifecycleThread")); assertTrue("Server-stop quiescence must leave queued Paper generation admitted", quiesce.contains("generator.quiesceForServerShutdown()")); assertOrdered(quiesce, @@ -63,6 +66,12 @@ public class IrisShutdownOrderingTest { assertOrdered(onDisable, "quiesceRuntimeForServerShutdown(\"onDisable\")", "super.onDisable()"); + assertTrue("Server-stop quiescence must keep queued generation running its mantle stages", + quiesce.contains("IrisToolbelt.beginWorldMaintenance(world, reason);")); + assertTrue("Disable must wait for a replacement that is staging before it tears anything down", + onDisable.indexOf("pendingWorldReplacements.awaitStagingForShutdown()") >= 0 + && onDisable.indexOf("pendingWorldReplacements.awaitStagingForShutdown()") + < onDisable.indexOf("teardownPapi()")); assertFalse("Server-stop quiescence must not begin generator close before Paper's boundary", generatorQuiesce.contains("closing = true")); assertFalse("Server-stop quiescence must not dispatch generator close before Paper's boundary", diff --git a/adapters/bukkit/plugin/src/test/java/art/arcane/iris/core/PendingWorldReplacementAdmissionTest.java b/adapters/bukkit/plugin/src/test/java/art/arcane/iris/core/PendingWorldReplacementAdmissionTest.java new file mode 100644 index 000000000..a10a6c2c6 --- /dev/null +++ b/adapters/bukkit/plugin/src/test/java/art/arcane/iris/core/PendingWorldReplacementAdmissionTest.java @@ -0,0 +1,70 @@ +package art.arcane.iris.core; + +import art.arcane.iris.Iris; +import org.bukkit.NamespacedKey; +import org.junit.Test; + +import java.io.IOException; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +public class PendingWorldReplacementAdmissionTest { + private static final long STATE_TIMEOUT_SECONDS = 5L; + private static final long JOIN_TIMEOUT_SECONDS = 10L; + private static final long STATE_POLL_MILLIS = 5L; + + private final NamespacedKey worldKey = NamespacedKey.minecraft("overworld"); + + @Test + public void shutdownDoesNotWaitWhenNothingIsStaging() throws Exception { + PendingWorldReplacementManager manager = new PendingWorldReplacementManager(mock(Iris.class)); + Thread shutdown = new Thread(manager::awaitStagingForShutdown); + + shutdown.start(); + shutdown.join(TimeUnit.SECONDS.toMillis(JOIN_TIMEOUT_SECONDS)); + + assertFalse(shutdown.isAlive()); + } + + @Test + public void shutdownRejectsReplacementsThatStartAfterIt() throws Exception { + PendingWorldReplacementManager manager = new PendingWorldReplacementManager(mock(Iris.class)); + manager.awaitStagingForShutdown(); + + IOException rejected = assertThrows(IOException.class, () -> manager.admitStaging(worldKey)); + + assertTrue(rejected.getMessage().contains("shutting down")); + } + + @Test + public void shutdownWaitsForTheReplacementBeingStaged() throws Exception { + PendingWorldReplacementManager manager = new PendingWorldReplacementManager(mock(Iris.class)); + PendingWorldReplacementManager.StagingAdmission staging = manager.admitStaging(worldKey); + Thread shutdown = new Thread(manager::awaitStagingForShutdown); + + shutdown.start(); + try { + awaitState(shutdown, Thread.State.WAITING); + assertTrue(shutdown.isAlive()); + } finally { + staging.close(); + } + shutdown.join(TimeUnit.SECONDS.toMillis(JOIN_TIMEOUT_SECONDS)); + + assertFalse(shutdown.isAlive()); + } + + private static void awaitState(Thread thread, Thread.State state) throws InterruptedException { + long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(STATE_TIMEOUT_SECONDS); + while (thread.getState() != state) { + if (System.nanoTime() > deadline) { + throw new AssertionError(thread.getName() + " never reached " + state + "; it is " + thread.getState()); + } + Thread.sleep(STATE_POLL_MILLIS); + } + } +} diff --git a/adapters/bukkit/plugin/src/test/java/art/arcane/iris/core/link/IrisPapiLifecycleTest.java b/adapters/bukkit/plugin/src/test/java/art/arcane/iris/core/link/IrisPapiLifecycleTest.java index 193e614fa..03d5cf792 100644 --- a/adapters/bukkit/plugin/src/test/java/art/arcane/iris/core/link/IrisPapiLifecycleTest.java +++ b/adapters/bukkit/plugin/src/test/java/art/arcane/iris/core/link/IrisPapiLifecycleTest.java @@ -85,7 +85,7 @@ public class IrisPapiLifecycleTest { String source = source(); assertTrue("onDisable must tear the expansion down", - source.contains("public void onDisable() {\n teardownPapi();")); + source.contains("public void onDisable() {\n pendingWorldReplacements.awaitStagingForShutdown();\n teardownPapi();")); assertTrue("the BileTools pre-unload hook must tear the expansion down", source.contains("public void onPreUnload(ReloadAware.PreUnloadReason reason) {\n teardownPapi();")); }