Skip to content

Multi-threaded worker step hangs during shutdown in remote partitioning #4431

Closed

Description

@reluxa

Bug description
I have an application which uses remote partitioned batch jobs which are sent to the workers via JMS.
I also have ThreadPoolTaskExecutor configured on the worker side, so the chunks can be processed in parallel.
I was testing the graceful shutdown behavior on the worker side.

One of the testcase was to test what is happening when the processing time of a step on remote side takes longer than the graceful period.
The expected scenario in this case that after the graceful period expires then the partition step terminates end the step state is going to be STOPPED in the database.

In my case, the application just starts hanging, Spring is not able to fully close the spring context in this scenario. It's hanging in an endless loop in RepeatTemplate.executeInternal(). This calls TaskExecutorRepeatTemplate.getNextResult() there it tries calls runnable.expect() which calls queue.expect();. Since spring already tries to Interrupt everything this call will fail with an InterruptedException which then will be translated to a RepeatException.

try {
result = getNextResult(context, callback, state);
executeAfterInterceptors(context, result);
}
catch (Throwable throwable) {
doHandle(throwable, context, deferred);
}
// N.B. the order may be important here:
if (isComplete(context, result) || isMarkedComplete(context) || !deferred.isEmpty()) {
running = false;
}

Here couple of things can fail:

if (isComplete(context, result) || isMarkedComplete(context) || !deferred.isEmpty()) {
running = false;
}

I would expect running to be set to false, however it won't happen the RepeatContext is still not complete.

Environment
Please provide as many details as possible: Spring Batch version, Java version, which database you use if any, etc

  • openjdk version "17.0.7" 2023-04-18 LTS
  • Spring Batch 5.0.2
  • Spring Boot 3.1.1
  • PostrgeSQL 15.3

Steps to reproduce
See above

Expected behavior

  • after the graceful period Spring shall be able to forcefully close the ApplicationContext
  • no NPE or other exception is expected to be thrown.
  • the related step state shall be saved using STOPPED state in the datatabase.

Minimal Complete Reproducible example
TBD, I will try to create a minimalistic example for this.
springbatchissue.zip
Steps to reproduce:

  • unzip
  • execute ./gradlew jibDockerBuild to create a docker image
  • start the stack using docker-compose up
  • check the logs for the worker, immediately after the first message is received by the worker execute kill -15 1 to kill it

You will see the app won't terminate after the graceful period ends. execute kill -3 1 and you will see that it's hanging in an endless loop

Activity

  1. added
    status: waiting-for-triage
    on Aug 10, 2023
  2. mdallos commented on Feb 26, 2024

    @mdallos

    Is there any update on the issue?

  3. vishnumurthy-nd commented on Aug 7, 2025

    @vishnumurthy-nd

    is there any update on this issue? I am also encountering similar issue, when EC2 machine shutdown is intiated I want to ensure all the running jobs will stop executing gracefully so that data processing will be continued by the new machine upon restarting the job

  4. fmbenhassine commented on Aug 8, 2025

    @fmbenhassine
    Contributor

    It's hanging in an endless loop in RepeatTemplate.executeInternal(). This calls TaskExecutorRepeatTemplate.getNextResult() there it tries calls runnable.expect() which calls queue.expect();.

    I believe this will be fixed with the new implementation of the chunk-oriented processing model that won't rely on TaskExecutorRepeatTemplate anymore (see #3950)

    I will plan this for v6 and check if the new implementation fixes the issue, otherwise I will debug the case and fix it as needed.

  5. added
    has: minimal-example
    and removed
    status: waiting-for-triage
    on Aug 8, 2025
  6. added this to the 6.0.0 milestone on Aug 8, 2025
  7. modified the milestones: 6.0.0, 6.0.0-M4 on Sep 16, 2025
  8. modified the milestones: 6.0.0-M4, 6.0.0 on Oct 9, 2025
  9. changed the title [-]Handling Graceful Shutdown in SpringBatch[/-] [+]Handling graceful shutdown on remote workers side[/+] on Oct 20, 2025
  10. fmbenhassine commented on Oct 20, 2025

    @fmbenhassine
    Contributor

    I just wanted to provide a quick update on this. First, @reluxa Thank you for reporting this issue and for providing an example!

    Here couple of things can fail:

    I came to the same conclusions when debugging your sample with the current implementation of the chunk-oriented processing model. As mentioned previously, I believe this will be improved with the new implementation (ie correctly handling interruption signals in the step), but we still need a couple of updates to properly handle the entire scenario: 1) provide a shutdown hook and 2) provide a way to register it on the worker side.

    FTR, we have introduced some updates to support graceful shutdown on the manager side (see #4965, #4023, #5028). It is now possible to register a job execution shutdown hook provided by Spring Batch to gracefully stop a job execution. However, in a remote partitioning/chunking setup, there is no job execution running on the worker side, so we need a step execution shutdown hook. This is what I am experimenting with in this branch: 8c51363. This relies on the new StoppableStep interface that is already implemented by ChunkOrientedStep. Open point: what is the best time/place to register that shutdown hook?

    I did not have time to update your sample to Spring Batch 6 yet, but I will add a comment here when I have further updates. In the meantime, I would be grateful if you could give it a try on your side as well.

  11. removed this from the 6.0.0 milestone on Nov 13, 2025
  12. fmbenhassine commented on Nov 2, 2026

    @fmbenhassine
    Contributor

    I did not have time to update your sample to Spring Batch 6 yet, but I will add a comment here when I have further updates.

    I updated the JMS worker example to Spring Batch 6, using the new chunk-oriented implementation. The original endless loop did not recur, but shutdown still saved the worker steps as FAILED instead of STOPPED.

    That remaining issue is now fixed in cacd319. When shutdown interrupts concurrent processing, the step now rolls back the current chunk, cancels outstanding processing tasks, and finishes as STOPPED. The enclosing flow also finishes as STOPPED.

  13. added a commit that references this issue on Nov 2, 2026
    7c50a58
  14. added
    for: backport-to-6.0.x
    on Nov 2, 2026
  15. added this to the 6.1.0 milestone on Nov 2, 2026
  16. changed the title [-]Handling graceful shutdown on remote workers side[/-] [+]Multi-threaded worker step hangs during shutdown in remote partitioning[/+] on Nov 2, 2026
  17. added a commit that references this issue on Nov 2, 2026
    cacd319
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: backport-to-6.0.xhas: minimal-examplein: coretype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions