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-projects-issues opened this issue Nov 23, 2018 · 1 comment
Closed
Assignees
Labels
in: core status: backported type: bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues 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-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Nov 23, 2018

Sam Brannen commented

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

@spring-projects-issues spring-projects-issues added type: bug status: backported in: core labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 5.1.3 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core status: backported type: bug
Projects
None yet
Development

No branches or pull requests

2 participants