From c802eb39c98cb932a0807dbf8cacf0d42d7a98d2 Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Thu, 9 Nov 2023 15:24:02 -0500 Subject: [PATCH 1/2] Wait for window initialization to complete in setupMinecraftWindow --- .../neoforged/fml/earlydisplay/DisplayWindow.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/earlydisplay/src/main/java/net/neoforged/fml/earlydisplay/DisplayWindow.java b/earlydisplay/src/main/java/net/neoforged/fml/earlydisplay/DisplayWindow.java index 85884c70f..a5da9f21c 100644 --- a/earlydisplay/src/main/java/net/neoforged/fml/earlydisplay/DisplayWindow.java +++ b/earlydisplay/src/main/java/net/neoforged/fml/earlydisplay/DisplayWindow.java @@ -40,11 +40,7 @@ import java.util.Objects; import java.util.Optional; import java.util.StringJoiner; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.Semaphore; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.BiConsumer; import java.util.function.Consumer; @@ -86,6 +82,7 @@ public class DisplayWindow implements ImmediateWindowProvider { private int framecount; private EarlyFramebuffer framebuffer; private ScheduledFuture windowTick; + private ScheduledFuture initializationFuture; private PerformanceInfo performanceInfo; private ScheduledFuture performanceTick; @@ -288,7 +285,7 @@ public Runnable start(@Nullable String mcVersion, final String forgeVersion) { return thread; }); initWindow(mcVersion); - renderScheduler.schedule(() -> initRender(mcVersion, forgeVersion), 1, TimeUnit.MILLISECONDS); + this.initializationFuture = renderScheduler.schedule(() -> initRender(mcVersion, forgeVersion), 1, TimeUnit.MILLISECONDS); return this::periodicTick; } @@ -519,6 +516,12 @@ private void handleLastGLFWError(BiConsumer handler) { * @return the Window we own. */ public long setupMinecraftWindow(final IntSupplier width, final IntSupplier height, final Supplier title, final LongSupplier monitorSupplier) { + // wait for the window to actually be initialized + try { + this.initializationFuture.get(); + } catch(InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } // we have to spin wait for the window ticker ImmediateWindowHandler.updateProgress("Initializing Game Graphics"); while (!this.windowTick.isDone()) { From f5b2449ba97d34cd42707f5e52fe8ebc7449f8bd Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Thu, 9 Nov 2023 15:26:20 -0500 Subject: [PATCH 2/2] Time out on window initialization after 30 seconds --- .../java/net/neoforged/fml/earlydisplay/DisplayWindow.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/earlydisplay/src/main/java/net/neoforged/fml/earlydisplay/DisplayWindow.java b/earlydisplay/src/main/java/net/neoforged/fml/earlydisplay/DisplayWindow.java index a5da9f21c..2bd08db92 100644 --- a/earlydisplay/src/main/java/net/neoforged/fml/earlydisplay/DisplayWindow.java +++ b/earlydisplay/src/main/java/net/neoforged/fml/earlydisplay/DisplayWindow.java @@ -518,9 +518,12 @@ private void handleLastGLFWError(BiConsumer handler) { public long setupMinecraftWindow(final IntSupplier width, final IntSupplier height, final Supplier title, final LongSupplier monitorSupplier) { // wait for the window to actually be initialized try { - this.initializationFuture.get(); + this.initializationFuture.get(30, TimeUnit.SECONDS); } catch(InterruptedException | ExecutionException e) { throw new RuntimeException(e); + } catch(TimeoutException e) { + Thread.dumpStack(); + crashElegantly("We seem to be having trouble initializing the window, waited for 30 seconds"); } // we have to spin wait for the window ticker ImmediateWindowHandler.updateProgress("Initializing Game Graphics");