Skip to content

Commit 81fde5e

Browse files
committed
Ensure that MethodParameter.findParameterIndex() is thread-safe
Prior to this commit, parallel invocations of MethodParameter.findParameterIndex() (invoked indirectly via SynthesizingMethodParameter.forParameter() and MethodParameter.forParameter()) could intermittently lead to an IllegalArgumentException being thrown 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 introducing a fallback for-loop that iterates over the candidate parameters a second time using equality checks instead of identity checks. Issue: SPR-17534
1 parent aa7f69a commit 81fde5e

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

spring-core/src/main/java/org/springframework/core/MethodParameter.java

+8
Original file line numberDiff line numberDiff line change
@@ -733,11 +733,19 @@ public static MethodParameter forParameter(Parameter parameter) {
733733
protected static int findParameterIndex(Parameter parameter) {
734734
Executable executable = parameter.getDeclaringExecutable();
735735
Parameter[] allParams = executable.getParameters();
736+
// Try first with identity checks for greater performance.
736737
for (int i = 0; i < allParams.length; i++) {
737738
if (parameter == allParams[i]) {
738739
return i;
739740
}
740741
}
742+
// Potentially try again with object equality checks in order to avoid race
743+
// conditions while invoking java.lang.reflect.Executable.getParameters().
744+
for (int i = 0; i < allParams.length; i++) {
745+
if (parameter.equals(allParams[i])) {
746+
return i;
747+
}
748+
}
741749
throw new IllegalArgumentException("Given parameter [" + parameter +
742750
"] does not match any parameter in the declaring executable");
743751
}

0 commit comments

Comments
 (0)