Skip to content

Commit e5e61df

Browse files
committed
Ignore scheduled task exceptions after shutdown
Includes suppression after logging, not propagating exceptions to the thread itself. Closes gh-32381 See gh-32298
1 parent 988f363 commit e5e61df

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

spring-context/src/main/java/org/springframework/scheduling/concurrent/SimpleAsyncTaskScheduler.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import java.util.concurrent.ScheduledThreadPoolExecutor;
2828
import java.util.concurrent.TimeUnit;
2929

30+
import org.apache.commons.logging.LogFactory;
31+
3032
import org.springframework.context.ApplicationContext;
3133
import org.springframework.context.ApplicationContextAware;
3234
import org.springframework.context.ApplicationListener;
@@ -183,12 +185,21 @@ protected void doExecute(Runnable task) {
183185
}
184186
}
185187

188+
private Runnable taskOnSchedulerThread(Runnable task) {
189+
return new DelegatingErrorHandlingRunnable(task, TaskUtils.getDefaultErrorHandler(true));
190+
}
191+
186192
private Runnable scheduledTask(Runnable task) {
187-
return () -> execute(new DelegatingErrorHandlingRunnable(task, TaskUtils.LOG_AND_PROPAGATE_ERROR_HANDLER));
193+
return () -> execute(new DelegatingErrorHandlingRunnable(task, this::shutdownAwareErrorHandler));
188194
}
189195

190-
private Runnable taskOnSchedulerThread(Runnable task) {
191-
return new DelegatingErrorHandlingRunnable(task, TaskUtils.getDefaultErrorHandler(true));
196+
private void shutdownAwareErrorHandler(Throwable ex) {
197+
if (this.scheduledExecutor.isTerminated()) {
198+
LogFactory.getLog(getClass()).debug("Ignoring scheduled task exception after shutdown", ex);
199+
}
200+
else {
201+
TaskUtils.getDefaultErrorHandler(true).handleError(ex);
202+
}
192203
}
193204

194205

0 commit comments

Comments
 (0)