Skip to content

Commit cd67b28

Browse files
committed
Ensure that parameter resolution in SpringExtension is thread-safe
Prior to this commit, parallel execution of @beforeeach and @AfterEach methods that accepted @Autowired arguments would fail intermittently due to a race condition in the internal implementation of the JDK's java.lang.reflect.Executable.getParameters() method. This commit addresses this issue by creating instances of SynthesizingMethodParameter via SynthesizingMethodParameter.forExecutable(Executable, int) instead of SynthesizingMethodParameter.forParameter(Parameter), since the latter looks up the parameter index by iterating over the array returned by Executable.getParameters() (which is not thread-safe). Issue: SPR-17533
1 parent 39925c3 commit cd67b28

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

spring-test/src/main/java/org/springframework/test/context/junit/jupiter/ParameterAutowireUtils.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ static Object resolveDependency(
118118
Autowired autowired = AnnotatedElementUtils.findMergedAnnotation(annotatedParameter, Autowired.class);
119119
boolean required = (autowired == null || autowired.required());
120120

121-
MethodParameter methodParameter = SynthesizingMethodParameter.forParameter(parameter);
121+
MethodParameter methodParameter = SynthesizingMethodParameter.forExecutable(
122+
parameter.getDeclaringExecutable(), parameterIndex);
122123
DependencyDescriptor descriptor = new DependencyDescriptor(methodParameter, required);
123124
descriptor.setContainingClass(containingClass);
124125
return applicationContext.getAutowireCapableBeanFactory().resolveDependency(descriptor, null);

0 commit comments

Comments
 (0)