From e5e61dfa3f8bbddee5512d2054b59409b9c029af Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 6 Mar 2024 18:03:31 +0100 Subject: [PATCH] Ignore scheduled task exceptions after shutdown Includes suppression after logging, not propagating exceptions to the thread itself. Closes gh-32381 See gh-32298 --- .../concurrent/SimpleAsyncTaskScheduler.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/scheduling/concurrent/SimpleAsyncTaskScheduler.java b/spring-context/src/main/java/org/springframework/scheduling/concurrent/SimpleAsyncTaskScheduler.java index 6d1b066b756c..a4b5a0a3c757 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/concurrent/SimpleAsyncTaskScheduler.java +++ b/spring-context/src/main/java/org/springframework/scheduling/concurrent/SimpleAsyncTaskScheduler.java @@ -27,6 +27,8 @@ import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import org.apache.commons.logging.LogFactory; + import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationListener; @@ -183,12 +185,21 @@ protected void doExecute(Runnable task) { } } + private Runnable taskOnSchedulerThread(Runnable task) { + return new DelegatingErrorHandlingRunnable(task, TaskUtils.getDefaultErrorHandler(true)); + } + private Runnable scheduledTask(Runnable task) { - return () -> execute(new DelegatingErrorHandlingRunnable(task, TaskUtils.LOG_AND_PROPAGATE_ERROR_HANDLER)); + return () -> execute(new DelegatingErrorHandlingRunnable(task, this::shutdownAwareErrorHandler)); } - private Runnable taskOnSchedulerThread(Runnable task) { - return new DelegatingErrorHandlingRunnable(task, TaskUtils.getDefaultErrorHandler(true)); + private void shutdownAwareErrorHandler(Throwable ex) { + if (this.scheduledExecutor.isTerminated()) { + LogFactory.getLog(getClass()).debug("Ignoring scheduled task exception after shutdown", ex); + } + else { + TaskUtils.getDefaultErrorHandler(true).handleError(ex); + } }