-
Notifications
You must be signed in to change notification settings - Fork 38.9k
Description
reproduced in 6.2.12 , same code in 7.0.x
A deadlock can occur as the main thread is running the initialization, and the shutdown hooks and contexts destroy.
This is rare, but quite reproducible on some apps, depending on how many Shutdown hooks are registered, and their order in the System IdentityHashMap holding them.
This prevents the JVM from exiting
The aim of our System.exit() was to detect a fatal precondition, exiting immediately and letting docker restart a new container. Blocking the exit() command led to a running application that was neither responsive neither shutting down.
This was raised in #31811, but the fix does not always work.
The code comments says "might be System.exit" / "very likely a System.exit call". This detection is not working well.
Reproduced it in a simple App that register many shutdown hooks to exhibit the problem:
https://github.com/ikucuze/spring-framework-issues-31811
Proposed correction by detecting that the System.exit() has been called, as this triggers the hooks and prevents further hooks registration.
private boolean isStartupShutdownThreadStuck() {
try {
Thread thread = new Thread();
Runtime.getRuntime().addShutdownHook(thread);
// clean up immediately just in case it did not raise an exception
Runtime.getRuntime().removeShutdownHook(thread);
} catch (IllegalStateException ex) {
// Shutdown in progress
return true;
}
return false;
}