Affects: 5.2.5.RELEASE
Reproduction steps
- Create a command-line tool with
CommandLineRunner
. - Add a Spring bean that:
a. Starts a background thread in the constructor.
b. Implements
Closeable
and terminate the background thread when calling itsclose()
. - Run the command-line tool.
Expected results
Spring will call the shutdown hooks after the main
function exits, call close()
on the bean, stop the background thread, and terminate the whole command-line tool.
Actual results
The command will continue running with the background thread until the user pressing ctrl+C.
A simple demo for this problem is at https://github.com/johnlinp/misc-demo/tree/master/unstoppable-spring-boot-command-line-runner. Problem description and logs are attached in the README.
Comment From: wilkinsona
Thanks for the sample. It's the JVM rather than Spring that calls the shutdown hooks. The JVM calls the hooks when it is shutting down. It will shutdown when all of the threads that are alive are daemon threads. The background thread that you have started is a non-daemon thread which prevents the JVM from shutting down and, therefore, prevents the shutdown hooks from being called.
Comment From: johnlinp
I see. Thank you so much for the information!