-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Shutdownhook not firing with native #465
Comments
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: import java.util.concurrent.CountDownLatch;
import sun.misc.Signal;
public class Agent {
private final CountDownLatch shutdownLatch;
public Agent() {
this.shutdownLatch = new CountDownLatch(1);
}
public void run() throws InterruptedException {
// Register a signal handler for Ctrl-C that runs the shutdown hooks
Signal.handle(new Signal("INT"), sig -> System.exit(0));
System.out.println("Running");
// Add a shutdown hook
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
shutdown();
}));
try {
shutdownLatch.await();
System.out.println("Latch released");
} catch (InterruptedException e) {
System.out.println("InterruptedException");
}
}
public synchronized void shutdown() {
System.out.println("Shutdown called");
shutdownLatch.countDown();
}
public static void main(String[] args) throws Exception {
new Agent().run();
}
} Invoking Note that the output |
Thanks. This should work for my use-case. |
@christianwimmer given this is a difference between running with normal |
@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. |
Quoting here (evicting one more click):
|
Firing off a CTRL C will not call the shutdown hook in the following example when running as native. It will fire when running with
java jar
The text was updated successfully, but these errors were encountered: