You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This boils down to the question "how much should we do during the startup of an application"? We want to do as little as possible during startup, but registering a signal handler would require us to start a new thread that handles the signal and invokes the shutdown hooks.
In addition, we want to be embedding-friendly: we do not want to register any signal handlers unless they are registered explicitly by the user. So even registering a signal handler when the first shutdown hook is installed feels intrusive.
Luckily, you can easily register the signal handler explicitly yourself if you want:
importjava.util.concurrent.CountDownLatch;
importsun.misc.Signal;
publicclassAgent {
privatefinalCountDownLatchshutdownLatch;
publicAgent() {
this.shutdownLatch = newCountDownLatch(1);
}
publicvoidrun() throwsInterruptedException {
// Register a signal handler for Ctrl-C that runs the shutdown hooksSignal.handle(newSignal("INT"), sig -> System.exit(0));
System.out.println("Running");
// Add a shutdown hookRuntime.getRuntime().addShutdownHook(newThread(() -> {
shutdown();
}));
try {
shutdownLatch.await();
System.out.println("Latch released");
} catch (InterruptedExceptione) {
System.out.println("InterruptedException");
}
}
publicsynchronizedvoidshutdown() {
System.out.println("Shutdown called");
shutdownLatch.countDown();
}
publicstaticvoidmain(String[] args) throwsException {
newAgent().run();
}
}
Invoking System.exit on SIGINT gives you the behavior you want, because shutdown hooks run as part of System.exit.
Note that the output Latch released of your application is not guaranteed to happen. It depends on thread scheduling, i.e., the main thread needs to resume in the short time window between the countDown of the latch and the actual exit of the application. On the Java HotSpot VM, shutdown seems to be "slow enough" so that Latch released is printed reliably on my machine, while with a native image shutdown is faster and therefore it is not printed.
Activity
christianwimmer commentedon Jun 14, 2018
This boils down to the question "how much should we do during the startup of an application"? We want to do as little as possible during startup, but registering a signal handler would require us to start a new thread that handles the signal and invokes the shutdown hooks.
In addition, we want to be embedding-friendly: we do not want to register any signal handlers unless they are registered explicitly by the user. So even registering a signal handler when the first shutdown hook is installed feels intrusive.
Luckily, you can easily register the signal handler explicitly yourself if you want:
Invoking
System.exitonSIGINTgives you the behavior you want, because shutdown hooks run as part ofSystem.exit.Note that the output
Latch releasedof your application is not guaranteed to happen. It depends on thread scheduling, i.e., the main thread needs to resume in the short time window between thecountDownof the latch and the actual exit of the application. On the Java HotSpot VM, shutdown seems to be "slow enough" so thatLatch releasedis printed reliably on my machine, while with a native image shutdown is faster and therefore it is not printed.rroller commentedon Jun 15, 2018
Thanks. This should work for my use-case.
Fix `shutdownhook not firing with native`
olpaw commentedon Apr 28, 2020
#1592 (comment)
paul-bjorkstrand commentedon Aug 18, 2020
@christianwimmer given this is a difference between running with normal
javaand with anative-imagebinary, do you think it would be worthwhile to put a note in https://github.com/oracle/graal/blob/master/substratevm/LIMITATIONS.md mentioning the bit about registering a signal handler, perhaps underFeatures That May Operate Differently in Native Image?olyagpl commentedon Oct 16, 2020
@paul-bjorkstrand, information on signal handlers is available on https://github.com/oracle/graal/blob/master/substratevm/Limitations.md#signal-handlers and https://github.com/oracle/graal/blob/master/substratevm/README.md#build-a-native-image pages.
Thanks!
mageddo commentedon Mar 12, 2023
Quoting here (evicting one more click):
native-imagenow supports out of box signal handlers since20.1, disabled by default, use--install-exit-handlers, see #1592 (comment)