Add task startup overrun warning to ThreadPoolTaskScheduler#36963
Open
itsmehotpants wants to merge 1 commit into
Open
Add task startup overrun warning to ThreadPoolTaskScheduler#36963itsmehotpants wants to merge 1 commit into
itsmehotpants wants to merge 1 commit into
Conversation
Closes spring-projects#33856 Adds a configurable `taskStartupOverrunThreshold` property to ThreadPoolTaskScheduler. When set, the scheduler checks in `beforeExecute()` whether a RunnableScheduledFuture is starting significantly later than its intended fire time, and logs a WARN message that includes the actual overrun in milliseconds and the configured threshold. This makes it straightforward to diagnose thread starvation caused by a single-threaded pool (the common default) being held by a long-running task, preventing subsequent tasks from starting on time. Changes ------- * New `taskStartupOverrunThreshold` volatile field (null by default, meaning warnings are disabled unless opted in). * `setTaskStartupOverrunThreshold(Duration)` setter with full Javadoc. * `getTaskStartupOverrunThreshold()` accessor. * `beforeExecute(Thread, Runnable)` override that inspects the task's remaining delay via `RunnableScheduledFuture.getDelay(NANOSECONDS)` and logs a WARN when the overrun meets or exceeds the threshold. * 7 JUnit 5 integration tests covering: default/null threshold, getter/setter round-trip, reset to null, normal execution with and without threshold, overrun detection under thread starvation, and no spurious warnings when threads are sufficient.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this PR does
Closes #33856
Adds a configurable
taskStartupOverrunThresholdproperty toThreadPoolTaskScheduler. When set, the scheduler emits aWARN-level log message whenever a scheduled task starts significantly later than its intended fire time, making it straightforward to diagnose thread starvation caused by a too-small thread pool or long-running tasks holding scheduler threads.Motivation
A common Spring Boot gotcha:
spring.task.scheduling.pool.sizedefaults to1. If any scheduled task runs long, all subsequent tasks queue up behind it and start late — sometimes by seconds or minutes. Currently this is silent; there is no built-in diagnostic. Users discover the problem only after significant delay or by reading the documentation carefully.This PR introduces an opt-in warning threshold so the issue surfaces immediately in logs.
Changes
ThreadPoolTaskSchedulertaskStartupOverrunThresholdfieldvolatile @Nullable Duration,nullby default (warnings disabled unless opted in)setTaskStartupOverrunThreshold(Duration)nullto disablegetTaskStartupOverrunThreshold()beforeExecute(Thread, Runnable)overrideRunnableScheduledFuture.getDelay(NANOSECONDS); logs WARN when overrun ≥ thresholdThe hook uses the already-wired
beforeExecutecallback that the anonymousScheduledThreadPoolExecutorsubclass created increateExecutor()delegates to, so no changes to the executor creation logic are needed.ThreadPoolTaskSchedulerOverrunTests(new)7 JUnit 5 tests covering:
nullnullUsage
When a task starts 1 450 ms after its scheduled time:
Checklist
nullthreshold = no change in behaviour for existing users)loggerfromExecutorConfigurationSupport(no new dependency)