Skip to content

Commit

Permalink
Ensure that parameter resolution in SpringExtension is thread-safe
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sbrannen committed Nov 23, 2018
1 parent 39925c3 commit cd67b28
Showing 1 changed file with 2 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ static Object resolveDependency(
Autowired autowired = AnnotatedElementUtils.findMergedAnnotation(annotatedParameter, Autowired.class);
boolean required = (autowired == null || autowired.required());

MethodParameter methodParameter = SynthesizingMethodParameter.forParameter(parameter);
MethodParameter methodParameter = SynthesizingMethodParameter.forExecutable(
parameter.getDeclaringExecutable(), parameterIndex);
DependencyDescriptor descriptor = new DependencyDescriptor(methodParameter, required);
descriptor.setContainingClass(containingClass);
return applicationContext.getAutowireCapableBeanFactory().resolveDependency(descriptor, null);
Expand Down

0 comments on commit cd67b28

Please sign in to comment.