Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MethodParameter.findParameterIndex() is not thread-safe [SPR-17534] #22066

Closed
spring-issuemaster opened this issue Nov 23, 2018 · 1 comment
Assignees
Milestone

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Nov 23, 2018

Sam Brannen opened SPR-17534 and commented

Status Quo

As discussed in #22065, org.springframework.core.MethodParameter.findParameterIndex(Parameter) is not thread-safe due to the manner in which java.lang.reflect.Executable.getParameters() is implemented in the JDK.

Proposed Solution

The following change has been verified to result in thread-safe behavior.

Whether or not we want two iterations is up for debate.

protected static int findParameterIndex(Parameter parameter) {
	Executable executable = parameter.getDeclaringExecutable();
	Parameter[] allParams = executable.getParameters();
	
	// Try first with identity checks for greater performance.
	for (int i = 0; i < allParams.length; i++) {
		if (parameter == allParams[i]) {
			return i;
		}
	}
	// Potentially try again with object equality in order to avoid race
	// conditions while accessing java.lang.reflect.Executable.getParameters().
	for (int i = 0; i < allParams.length; i++) {
		if (parameter.equals(allParams[i])) {
			return i;
		}
	}
	throw new IllegalArgumentException("Given parameter [" + parameter +
			"] does not match any parameter in the declaring executable");
}

Deliverables

  1. Ensure that MethodParameter.findParameterIndex() is thread-safe

Affects: 5.0.10, 5.1 GA

Issue Links:

  • #22065 Parameter resolution in SpringExtension is not thread-safe

Referenced from: commits 81fde5e, f0e69e0

Backported to: 5.0.11

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Nov 23, 2018

Sam Brannen commented

This has been fixed on master (5.1.3) and 5.0.x (5.0.11).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.