how deadlocks occur?
- when ApplicationContext call refresh() method, main thread will hold startupShutdownMonitor monitor;
- then call onRefresh() method, it will load the external WebServer and create it;
- Let's say I implemented this WebServer, i will check license before created it, when the certification fails, I call System.exit(1), want to exit the process;
- System.exit(1) will runHooks, ApplicationContext's shutdownHook will be call, It runs in another thread, name of 'SpringContextShutdownHook', it will wait the monitor startupShutdownMonitor, but the monitor still hold by main thread, to make matters worse, main thread blocking in onRefresh method, wait WebServer created.
- so, deadlock occur!!
Fix it:
shutdownHook does not need to acquire a monitor on startupShutdownMonitor , and doClose method is thread safe, it is enough to call it.
Comment From: snicoll
Thanks for the PR @uyong. Unfortunately, this is not easy to make up our mind based on a one liner change like this. Would you be able to provide a small app that reproduces the behavior you've described?
Comment From: snicoll
Closing due to lack of requested feedback. If the feedback is provided we can consider reopening.
Comment From: uyong
@snicoll @sbrannen I'm sorry, but I'm just following this message now; this is app spring-bug-verify
Comment From: snicoll
@uyong thanks for the sample. We don't think the proposed change is safe, I've created #31811 to revisit this part of the code. We'll make sure to test that against your sample.
Comment From: snicoll
@uyong with the latest change in #31811, that sample exits as you expect.
Comment From: uyong
@snicoll thank you