Skip to content

Race condition in DefaultHeaderChannelRegistry during application shutdown causes error log #10547

Closed

Description

@francisco-bru

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)

  1. Start a Spring Integration application using DefaultHeaderChannelRegistry.
  2. Shut down the application, triggering the lifecycle processor to call stop().
  3. 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.

Activity

  1. added
    status: waiting-for-triage
    on Nov 4, 2025
  2. artembilan commented on Nov 4, 2025

    @artembilan
    Member

    Good catch, @francisco-bru !

    I think isRunning() has to be checked before a new schedule in that run() implementation.

    Please, let us know if you can contribute the fix today.
    Since release is today.

    Thank you!

  3. added this to the 7.0.0-RC2 milestone on Nov 4, 2025
  4. self-assigned this
    on Nov 4, 2025
  5. artembilan commented on Nov 4, 2025

    @artembilan
    Member

    I'll do it myself since we need to be ready for the release as soon as possible.

  6. added a commit that references this issue on Nov 4, 2025
  7. added 2 commits that reference this issue on Nov 4, 2025
  8. francisco-bru commented on Nov 4, 2025

    @francisco-bru
    Author

    @artembilan, I don't think that will solve the problem because the Taskscheduler may be in shutdown state and run() is called before stop().

  9. artembilan commented on Nov 4, 2025

    @artembilan
    Member

    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.

  10. francisco-bru commented on Nov 5, 2025

    @francisco-bru
    Author

    hi @artembilan,
    I think this is the initial problem.

  11. artembilan commented on Nov 5, 2025

    @artembilan
    Member

    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

  12. francisco-bru commented on Nov 5, 2025

    @francisco-bru
    Author

    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.

  13. artembilan commented on Nov 5, 2025

    @artembilan
    Member

    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?

  14. artembilan commented on Nov 6, 2025

    @artembilan
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions