You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Race condition in DefaultHeaderChannelRegistry during application shutdown causes error log #10547
In what version(s) of Spring Integration are you seeing this issue?
6.5.2
Describe the bug
When stopping an application, the following error log is generated:
ERROR Unexpected error occurred in scheduled taskorg.springframework.core.task.TaskRejectedException: ExecutorService in shutdown state did not accept task: bean 'integrationHeaderChannelRegistry'
at org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler.schedule(ThreadPoolTaskScheduler.java:430)
Caused by: java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@7bc9f638[Not completed, task = java.util.concurrent.Executors$RunnableAdapter@3ecb6f85[Wrapped task = DelegatingErrorHandlingRunnable for bean 'integrationHeaderChannelRegistry']] rejected from org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler$1@57715118[Shutting down, pool size = 1, active threads = 1, queued tasks = 0, completed tasks = 1225]
Since this happens during application shutdown, it does not appear to have any side effects. However, because this message is logged at the error level, it can cause uncertainty about whether the application has shut down correctly, or if something has actually failed.
Upon reviewing the affected class, DefaultHeaderChannelRegistry, it seems possible that a race condition occurs between the call to stop() (executed by the lifecycle processor) and a scheduled invocation of the run() method of the reaper.
Perhaps, in the run() method, scheduling of the next execution could be skipped if explicitlyStopped==true, to prevent this situation from occurring, in which the task scheduler is already stopping and no longer accepting tasks.
To Reproduce (Sometimes not always)
Start a Spring Integration application using DefaultHeaderChannelRegistry.
Shut down the application, triggering the lifecycle processor to call stop().
Observe the logs for the error at shutdown.
Expected behavior
No error-level log should be generated by the scheduled reaper when the application is being shut down intentionally.
Reacted by Fernando Blanch Calvete
Reacted by Julio Gómez Díaz and Jordi Martínez Vicent
Well, we don't control a TaskScheduler from the DefaultHeaderChannelRegistry, and fully rely on whatever was injected.
Therefore, if you somehow shutdown that TaskScheduler externally, there is nothing we can do in the running DefaultHeaderChannelRegistry but fail same way as you reported originally.
If everything is controlled by the Spring application context, then the stop() called first for all the beans. And destroy() (that would be a shutdown for the managed TaskScheduler) only after that.
Therefore checking for isRunning() to avoid extra scheduling is enough.
I'm not sure, @francisco-bru , what is your request.
What you think has to be fixed yet?
Whatever here in Spring Integration or there in Spring Cloud Stream.
Hello, @artembilan,
I believe you should analyse the issue in Spring Cloud Stream, as this change is causing a change in the start-up behaviour of applications. The current issue is only a problem related to this change in behaviour and the solution to it may affect this.
Right. I read that report, but cannot compile it in my head.
Could you please, rephrase (over there) what exactly the problem and where to look?
It also would be great if you share some minimal example we can run on our side and reproduce.
That way we could debug it and spot the problematic piece of code.
I've changed here not start-up behavior but reaction to the stopped state from a scheduled task.
How is that related to lifecycle, please?
Regarding your race condition concern.
See my comment above: #10547 (comment).
It does not matter if it is DefaultHeaderChannelRegistry or not. Anything using that shared TaskScheduler would suffer from the same error when it would try to schedule a new task into already shut-downed TaskScheduler.
And that is fully expected behavior and will be the same everywhere.
I don't think we need to think about fixing it since raised error is fully enough to understand in what state your system is.
Something would need to be adjusted in the target project to avoid shutting down TaskScheduler too early.
Activity
artembilan commentedon Nov 4, 2025
Good catch, @francisco-bru !
I think
isRunning()has to be checked before a new schedule in thatrun()implementation.Please, let us know if you can contribute the fix today.
Since release is today.
Thank you!
artembilan commentedon Nov 4, 2025
I'll do it myself since we need to be ready for the release as soon as possible.
GH-10547: DefaultHeaderChannelRegistry: Fix race condition
GH-10547: DefaultHeaderChannelRegistry: Fix race condition
GH-10547: DefaultHeaderChannelRegistry: Fix race condition
francisco-bru commentedon Nov 4, 2025
@artembilan, I don't think that will solve the problem because the
Taskschedulermay be in shutdown state andrun()is called beforestop().artembilan commentedon Nov 4, 2025
Well, we don't control a
TaskSchedulerfrom theDefaultHeaderChannelRegistry, and fully rely on whatever was injected.Therefore, if you somehow shutdown that
TaskSchedulerexternally, there is nothing we can do in the runningDefaultHeaderChannelRegistrybut fail same way as you reported originally.If everything is controlled by the Spring application context, then the
stop()called first for all the beans. Anddestroy()(that would be a shutdown for the managedTaskScheduler) only after that.Therefore checking for
isRunning()to avoid extra scheduling is enough.francisco-bru commentedon Nov 5, 2025
hi @artembilan,
I think this is the initial problem.
artembilan commentedon Nov 5, 2025
I'm not sure, @francisco-bru , what is your request.
What you think has to be fixed yet?
Whatever here in Spring Integration or there in Spring Cloud Stream.
Thanks
francisco-bru commentedon Nov 5, 2025
Hello, @artembilan,
I believe you should analyse the issue in Spring Cloud Stream, as this change is causing a change in the start-up behaviour of applications. The current issue is only a problem related to this change in behaviour and the solution to it may affect this.
artembilan commentedon Nov 5, 2025
Right. I read that report, but cannot compile it in my head.
Could you please, rephrase (over there) what exactly the problem and where to look?
It also would be great if you share some minimal example we can run on our side and reproduce.
That way we could debug it and spot the problematic piece of code.
I've changed here not start-up behavior but reaction to the stopped state from a scheduled task.
How is that related to lifecycle, please?
artembilan commentedon Nov 6, 2025
Regarding your race condition concern.
See my comment above: #10547 (comment).
It does not matter if it is
DefaultHeaderChannelRegistryor not. Anything using that sharedTaskSchedulerwould suffer from the same error when it would try to schedule a new task into already shut-downedTaskScheduler.And that is fully expected behavior and will be the same everywhere.
I don't think we need to think about fixing it since raised error is fully enough to understand in what state your system is.
Something would need to be adjusted in the target project to avoid shutting down
TaskSchedulertoo early.