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 | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 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 6d1b066b75..a4b5a0a3c7 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.ScheduledFuture; 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,14 +185,23 @@ public class SimpleAsyncTaskScheduler extends SimpleAsyncTaskExecutor implements } } - private Runnable scheduledTask(Runnable task) { - return () -> execute(new DelegatingErrorHandlingRunnable(task, TaskUtils.LOG_AND_PROPAGATE_ERROR_HANDLER)); - } - private Runnable taskOnSchedulerThread(Runnable task) { return new DelegatingErrorHandlingRunnable(task, TaskUtils.getDefaultErrorHandler(true)); } + private Runnable scheduledTask(Runnable task) { + return () -> execute(new DelegatingErrorHandlingRunnable(task, this::shutdownAwareErrorHandler)); + } + + 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); + } + } + @Override @Nullable