Spring 6.2.9
When Spring Boot auto-configures a SimpleAsyncTaskScheduler
(with virtual threads enabled) to be used for running @Scheduled
tasks, closing the application context will immediately interrupt the threads executing those tasks.
I was expecting the taskTerminationTimeout
to be respected before hard-killing running tasks.
The offending code is here (calling Future.cancel
with true
):
https://github.com/spring-projects/spring-framework/blob/09a5ca3e747af3dacd2bbb42ae4f356db26b57d3/spring-context/src/main/java/org/springframework/scheduling/concurrent/SimpleAsyncTaskScheduler.java#L397-L401
and here (explicitly calling Thread.interrupt()
, not allowing graceful completion):
https://github.com/spring-projects/spring-framework/blob/09a5ca3e747af3dacd2bbb42ae4f356db26b57d3/spring-core/src/main/java/org/springframework/core/task/SimpleAsyncTaskExecutor.java#L362-L380
Note how running threads are interrupted immediately, and then we wait for them to finish their work.
A similar issue was fixed in the past (#31019), but the fix made there doesn't help if the underlying TaskExecutor
is itself also interrupting threads.
The end result is that scheduled tasks currently waiting for the database or some other service will be interrupted and fail immediately, thus preventing graceful shutdown and making the taskTerminationTimeout
almost useless.