Skip to content

Propagate step context to worker threads in ChunkTaskExecutorItemWriter#5400

Open
seonwooj0810 wants to merge 1 commit into
spring-projects:mainfrom
seonwooj0810:fix/issue-5398-stepscope-local-chunking
Open

Propagate step context to worker threads in ChunkTaskExecutorItemWriter#5400
seonwooj0810 wants to merge 1 commit into
spring-projects:mainfrom
seonwooj0810:fix/issue-5398-stepscope-local-chunking

Conversation

@seonwooj0810
Copy link
Copy Markdown

Summary

ChunkTaskExecutorItemWriter submits chunk processing to a TaskExecutor but did not register a StepContext on the worker thread. As a result, @StepScope beans accessed from inside the delegate ChunkProcessor fail with ScopeNotActiveException, and DisposableBean.destroy() is skipped for step-scoped beans.

This change registers the step execution with StepSynchronizationManager before the delegate handles the request, and closes it in a finally block — the same pattern applied to multi-threaded ChunkOrientedStep in #5183 (see commit 5642911).

Fixes #5398.

Changes

  • ChunkTaskExecutorItemWriter.write(Chunk): wrap the worker-thread callable in StepSynchronizationManager.register(stepExecution) / close().
  • New test ChunkTaskExecutorItemWriterTests.stepContextIsPropagatedToWorkerThread — verifies StepSynchronizationManager.getContext() returns the active StepContext from inside the delegate ChunkProcessor.

Test plan

  • ./mvnw -pl spring-batch-integration -am test — all 150 tests pass, including the new test
  • Confirmed the new test fails on main without this fix (StepContext should be available on the worker thread) and passes with it
  • No changes to public API; existing ChunkTaskExecutorItemWriter tests unaffected

ChunkTaskExecutorItemWriter submits chunk processing to a TaskExecutor,
but did not register a StepContext on the worker thread. As a result,
@StepScope beans accessed inside the delegate ChunkProcessor failed with
ScopeNotActiveException, and DisposableBean destruction was skipped.

Register the step execution with StepSynchronizationManager before the
delegate handles the request, and close it in a finally block. This
mirrors the fix applied to multi-threaded ChunkOrientedStep in spring-projects#5183.

Resolves spring-projects#5398

Signed-off-by: Seonwoo Jung <laborlawseon@kap.kr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ScopeNotActiveException with @StepScope and Local Chunking

1 participant