Don't call System.exit() from shutdown hook thread

This commit is contained in:
Marc Baloup 2023-10-08 01:02:14 +02:00
parent 8b6fe63df1
commit 728961d19f

View File

@ -52,7 +52,7 @@ public abstract class CLIApplication {
new CommandAdmin(); new CommandAdmin();
new CommandStop(); new CommandStop();
Runtime.getRuntime().addShutdownHook(new Thread(this::stop)); Runtime.getRuntime().addShutdownHook(shutdownThread);
cli.start(); // actually starts the CLI thread 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); private final AtomicBoolean stopping = new AtomicBoolean(false);
/** /**
* Stops this application. * Stops this application.
*/ */
@SuppressWarnings("finally")
public final void stop() { public final void stop() {
synchronized (stopping) { synchronized (stopping) {
if (stopping.get()) if (stopping.get())
@ -88,9 +89,9 @@ public abstract class CLIApplication {
end(); end();
} catch (Throwable t) { } catch (Throwable t) {
Log.severe("Error stopping application " + getName() + " version " + getClass().getPackage().getImplementationVersion(), t); Log.severe("Error stopping application " + getName() + " version " + getClass().getPackage().getImplementationVersion(), t);
System.exit(1);
} finally { } finally {
Log.info("Bye bye."); Log.info("Bye bye.");
if (!Thread.currentThread().equals(shutdownThread))
System.exit(0); System.exit(0);
} }
} }