From f607b831caa943e493c31bd5046366cf4a711fa9 Mon Sep 17 00:00:00 2001 From: Philipp Wagner Date: Wed, 18 Nov 2026 20:08:07 +0100 Subject: [PATCH] Keep a running switch's wizard request when a second rerun is refused rerunSetupWizard set the one-shot request before it tried to obtain the switch, and cleared it when the switch was refused. A second confirmed call during the first one's window teardown therefore removed the first call's request: the session ended and the profile restarted without the wizard. The request is now set only by the call that owns the switch. Written-by: Claude Fable 5.2 --- .../view/ApplicationRuntimeLauncher.java | 93 ++++++++++++------- ...icationRuntimeLauncherSetupWizardTest.java | 30 +++++- 2 files changed, 85 insertions(+), 38 deletions(-) diff --git a/app-desktop/src/main/java/kst4contest/view/ApplicationRuntimeLauncher.java b/app-desktop/src/main/java/kst4contest/view/ApplicationRuntimeLauncher.java index 11b904cf..a5ae69f8 100644 --- a/app-desktop/src/main/java/kst4contest/view/ApplicationRuntimeLauncher.java +++ b/app-desktop/src/main/java/kst4contest/view/ApplicationRuntimeLauncher.java @@ -111,6 +111,11 @@ public final class ApplicationRuntimeLauncher { * other way round: a runtime shutting down flushes its pending layout write, and that * must not land on the file the wizard has just written.

* + *

The wizard is requested only once this call owns the switch. A call refused + * because a switch is already under way therefore leaves that switch's request alone: + * clearing it would let the running switch end the session and then start without the + * wizard it was made for.

+ * * @return true if the new runtime was built */ public static boolean rerunSetupWizard() { @@ -120,16 +125,25 @@ public final class ApplicationRuntimeLauncher { return false; } - requestSetupWizard(); + if (!beginSwitch()) { + return false; + } + try { - boolean switched = switchProfile(active.getProfile()); - if (!switched) { - consumeSetupWizardRequest(); + requestSetupWizard(); + + boolean switched = false; + try { + switched = performSwitch(active.getProfile()); + } finally { + if (!switched) { + // The request is this call's own, and no start consumed it. + consumeSetupWizardRequest(); + } } return switched; - } catch (RuntimeException e) { - consumeSetupWizardRequest(); - throw e; + } finally { + endSwitch(); } } @@ -137,7 +151,8 @@ public final class ApplicationRuntimeLauncher { * Replaces the running runtime with one bound to another operator profile. * * @param targetProfile profile to activate - * @return true if the new runtime was built + * @return true if the new runtime was built; false also when another switch is + * already under way */ public static boolean switchProfile(final OperatorProfile targetProfile) { @@ -146,36 +161,42 @@ public final class ApplicationRuntimeLauncher { } try { - if (targetProfile == null) { - return false; - } - - new OperatorProfileStore().recordLastUsed(targetProfile.getProfileId()); - - if (currentRuntime != null) { - currentRuntime.shutdownRuntime(); - } - - ActiveOperatorProfile.set(OperatorProfilePaths.resolve(targetProfile)); - - Kst4ContestApplication nextRuntime = new Kst4ContestApplication(); - - try { - nextRuntime.startRuntime(); - } catch (Exception e) { - // The previous runtime is already gone, so there is nothing left to return to. - LOGGER.log(Level.SEVERE, "Could not start the selected operator profile", e); - Kst4ContestApplication.alertWindowEvent( - "The operator profile could not be started: " + e.getMessage() - + "\n\nKST4Contest has to be closed."); - System.exit(1); - return false; - } - - setCurrent(nextRuntime); - return true; + return performSwitch(targetProfile); } finally { endSwitch(); } } + + /** The switch itself. The caller holds the switch-in-progress mark. */ + private static boolean performSwitch(final OperatorProfile targetProfile) { + + if (targetProfile == null) { + return false; + } + + new OperatorProfileStore().recordLastUsed(targetProfile.getProfileId()); + + if (currentRuntime != null) { + currentRuntime.shutdownRuntime(); + } + + ActiveOperatorProfile.set(OperatorProfilePaths.resolve(targetProfile)); + + Kst4ContestApplication nextRuntime = new Kst4ContestApplication(); + + try { + nextRuntime.startRuntime(); + } catch (Exception e) { + // The previous runtime is already gone, so there is nothing left to return to. + LOGGER.log(Level.SEVERE, "Could not start the selected operator profile", e); + Kst4ContestApplication.alertWindowEvent( + "The operator profile could not be started: " + e.getMessage() + + "\n\nKST4Contest has to be closed."); + System.exit(1); + return false; + } + + setCurrent(nextRuntime); + return true; + } } diff --git a/app-desktop/src/test/java/kst4contest/view/ApplicationRuntimeLauncherSetupWizardTest.java b/app-desktop/src/test/java/kst4contest/view/ApplicationRuntimeLauncherSetupWizardTest.java index 1aa2b58e..06b438ad 100644 --- a/app-desktop/src/test/java/kst4contest/view/ApplicationRuntimeLauncherSetupWizardTest.java +++ b/app-desktop/src/test/java/kst4contest/view/ApplicationRuntimeLauncherSetupWizardTest.java @@ -43,15 +43,41 @@ class ApplicationRuntimeLauncherSetupWizardTest { } @Test - void rerunSetupWizardWhileSwitchInProgressReturnsFalseAndClearsRequest() { + void aRefusedRerunLeavesTheRequestOfTheRunningSwitchAlone() { OperatorProfileSelection previous = ActiveOperatorProfile.get(); OperatorProfile profile = new OperatorProfile("wizard-guard-test", "Wizard Guard Test", false, false); ActiveOperatorProfile.set(new OperatorProfileSelection(profile, "preferences.xml", "worked.db", false)); + + // A first rerun is under way: it holds the guard and its request waits for startRuntime. + assertTrue(ApplicationRuntimeLauncher.beginSwitch()); + ApplicationRuntimeLauncher.requestSetupWizard(); + try { + // A second one arrives while the first tears its windows down, and is refused. + assertFalse(ApplicationRuntimeLauncher.rerunSetupWizard()); + + assertTrue(ApplicationRuntimeLauncher.consumeSetupWizardRequest(), + "the refused rerun took the running switch's request with it: that switch " + + "would end the session and then start without the wizard"); + } finally { + ApplicationRuntimeLauncher.endSwitch(); + ApplicationRuntimeLauncher.consumeSetupWizardRequest(); + ActiveOperatorProfile.set(previous); + } + } + + @Test + void aRefusedRerunLeavesNoRequestOfItsOwn() { + OperatorProfileSelection previous = ActiveOperatorProfile.get(); + OperatorProfile profile = new OperatorProfile("wizard-guard-test", "Wizard Guard Test", false, false); + ActiveOperatorProfile.set(new OperatorProfileSelection(profile, "preferences.xml", "worked.db", false)); + + // A plain profile switch is under way; nobody asked for the wizard. assertTrue(ApplicationRuntimeLauncher.beginSwitch()); try { assertFalse(ApplicationRuntimeLauncher.rerunSetupWizard()); + assertFalse(ApplicationRuntimeLauncher.consumeSetupWizardRequest(), - "a refused rerun must not leave a wizard request behind"); + "a refused rerun must not hand its wizard to the switch that refused it"); } finally { ApplicationRuntimeLauncher.endSwitch(); ApplicationRuntimeLauncher.consumeSetupWizardRequest();