From 1f277ef7d1eb270b66ebfd482e328a1a7d5c19bf Mon Sep 17 00:00:00 2001 From: ThomasKrieger Date: Mon, 22 Sep 2025 17:30:08 +0200 Subject: [PATCH 1/2] Test for Issue: Race Condition MapStepRegistry unregisterStepsFromJob and getStep Signed-off-by: ThomasKrieger --- pom.xml | 1 + spring-batch-core/pom.xml | 29 ++++++++++ .../support/MapStepRegistryCT.java | 56 +++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/MapStepRegistryCT.java diff --git a/pom.xml b/pom.xml index 06b2eb92d1..6cd0ba8b96 100644 --- a/pom.xml +++ b/pom.xml @@ -127,6 +127,7 @@ 9.4.13.0 6.8.0.RELEASE 6.1.0 + 1.2.14 ${spring-amqp.version} diff --git a/spring-batch-core/pom.xml b/spring-batch-core/pom.xml index 048a1e24f1..cefcb2f5b3 100644 --- a/spring-batch-core/pom.xml +++ b/spring-batch-core/pom.xml @@ -402,6 +402,12 @@ + + com.vmlens + api + ${vmlens.version} + test + @@ -412,4 +418,27 @@ + + + + com.vmlens + vmlens-maven-plugin + ${vmlens.version} + + + test + + test + + + + + + **/*CT.java + + + + + + diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/MapStepRegistryCT.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/MapStepRegistryCT.java new file mode 100644 index 0000000000..e3dfd158f1 --- /dev/null +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/MapStepRegistryCT.java @@ -0,0 +1,56 @@ +/* + * Copyright 2006-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.core.configuration.support; + +import com.vmlens.api.AllInterleavings; +import org.junit.jupiter.api.Test; +import org.springframework.batch.core.configuration.DuplicateJobException; +import org.springframework.batch.core.configuration.StepRegistry; +import org.springframework.batch.core.launch.NoSuchJobException; +import org.springframework.batch.core.step.tasklet.TaskletStep; + +import java.util.List; + +/** + * @author Thomas Krieger + */ +public class MapStepRegistryCT { + + @Test + public void unregisterAndGet() throws DuplicateJobException, InterruptedException { + try (AllInterleavings allInterleavings = new AllInterleavings("MapStepRegistryCT.unregisterAndGet")) { + while (allInterleavings.hasNext()) { + StepRegistry registry = new MapStepRegistry(); + registry.register("foo", List.of(new TaskletStep("first"))); + Thread first = new Thread() { + @Override + public void run() { + registry.unregisterStepsFromJob("foo"); + } + }; + first.start(); + try { + registry.getStep("foo", "first"); + } + catch (NoSuchJobException e) { + + } + first.join(); + } + } + } + +} From afa57b1eaaf5787e0e3d081c52f18323594a4dfc Mon Sep 17 00:00:00 2001 From: ThomasKrieger Date: Mon, 22 Sep 2025 18:25:57 +0200 Subject: [PATCH 2/2] replaced map.containsKey with get and != null in MapStepRegistry Resolves #4981 Signed-off-by: Thomas Krieger thomas.krieger@vmlens.com Signed-off-by: ThomasKrieger --- .../batch/core/configuration/support/MapStepRegistry.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/MapStepRegistry.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/MapStepRegistry.java index 051a44edd0..c26f925763 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/MapStepRegistry.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/MapStepRegistry.java @@ -65,11 +65,11 @@ public void unregisterStepsFromJob(String jobName) { public Step getStep(String jobName, String stepName) throws NoSuchJobException { Assert.notNull(jobName, "The job name cannot be null."); Assert.notNull(stepName, "The step name cannot be null."); - if (!map.containsKey(jobName)) { + final Map jobSteps = map.get(jobName); + if (jobSteps == null) { throw new NoSuchJobException("No job configuration with the name [" + jobName + "] was registered"); } else { - final Map jobSteps = map.get(jobName); if (jobSteps.containsKey(stepName)) { return jobSteps.get(stepName); }