From 728961d19f31761957f5c9404b623c813cbe563a Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Sun, 8 Oct 2023 01:02:14 +0200 Subject: [PATCH] Don't call System.exit() from shutdown hook thread --- .../main/java/fr/pandacube/lib/cli/CLIApplication.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pandalib-cli/src/main/java/fr/pandacube/lib/cli/CLIApplication.java b/pandalib-cli/src/main/java/fr/pandacube/lib/cli/CLIApplication.java index 68f2f7b..dde3fc9 100644 --- a/pandalib-cli/src/main/java/fr/pandacube/lib/cli/CLIApplication.java +++ b/pandalib-cli/src/main/java/fr/pandacube/lib/cli/CLIApplication.java @@ -52,7 +52,7 @@ public abstract class CLIApplication { new CommandAdmin(); new CommandStop(); - Runtime.getRuntime().addShutdownHook(new Thread(this::stop)); + Runtime.getRuntime().addShutdownHook(shutdownThread); cli.start(); // actually starts the CLI thread @@ -71,12 +71,13 @@ public abstract class CLIApplication { } + private final Thread shutdownThread = new Thread(this::stop); + private final AtomicBoolean stopping = new AtomicBoolean(false); /** * Stops this application. */ - @SuppressWarnings("finally") public final void stop() { synchronized (stopping) { if (stopping.get()) @@ -88,10 +89,10 @@ public abstract class CLIApplication { end(); } catch (Throwable t) { Log.severe("Error stopping application " + getName() + " version " + getClass().getPackage().getImplementationVersion(), t); - System.exit(1); } finally { Log.info("Bye bye."); - System.exit(0); + if (!Thread.currentThread().equals(shutdownThread)) + System.exit(0); } }