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
SimpleAsyncTaskScheduler: task with fixed delay stops working after unhandled exception #31749
Comments
@Scheduled
task stop working after unhandled exception when virtual threads are enabled
Also @Bean
public ThreadPoolTaskSchedulerCustomizer threadPoolTaskSchedulerCustomizer() {
return taskScheduler -> {
taskScheduler.setErrorHandler(t -> LoggerFactory.getLogger(ThreadPoolTaskScheduler.class).error("Unexpected error occurred in {}scheduled task.", Thread.currentThread().isVirtual() ? "virtual " : "", t));
};
} |
Thanks for the report and sample project. The difference in behaviour is due to differences in Spring Framework's I don't think there's anything we can do about this in Spring Boot as there's no way that I am aware of for us to combine the simple non-pooling behaviour of
This is to be expected as there's no |
If you use virtual threads, If you use platform threads, |
Ok, thanks for clarifications. Maybe it should be mentioned in release notes or documentation (if not already), so users are aware that if they enable virtual threads they should also handle all exceptions in |
I suppose this problem specifically materializes with fixed-delay tasks which we need to execute on the main scheduler thread... and which we therefore need to wrap in a defensive error handler. |
I have tested with |
Good to hear! Thanks for testing the snapshot. |
Is it possible to declare exception handler for |
When using virtual threads all
@Scheduled
executors stop working after first exception on any@Scheduled
executor and no error is logged. With disabled virtual threads@Scheduled
executors continue working and exception is logged withTaskUtils$LoggingErrorHandler
andSimpleAsyncUncaughtExceptionHandler
.Demo is here: https://github.com/ztomic/scheduling-demo
In this demo
DemoEvent
is generated every 5 seconds with@Scheduled
task, and there are two listeners, one is@Async
and other is synchronous and both of them are throwing exception on every second invocation. Also, there is one@Scheduled
task which throws exception on every second invocation.With disabled virtual threads, all
@Scheduled
tasks continue working after exception.Run application with virtual threads disabled and after that with virtual threads enabled in
application.yml
.The text was updated successfully, but these errors were encountered: