Repository navigation
Description
Since 7.0.11, MethodParameter.getParameterName() can return null for a parameter whose name is
available (compiled with -parameters) when two threads resolve the name of the same
MethodParameter instance for the first time concurrently.
In Spring MVC this shows up as a sporadic failure on the first concurrent requests to a handler
method after startup, for arguments that rely on the reflected name (e.g. @PathVariable String id):
java.lang.IllegalArgumentException: Name for argument of type [java.lang.String] not specified, and parameter name information not available via reflection. Ensure that the compiler uses the '-parameters' flag.
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.updateNamedValueInfo(AbstractNamedValueMethodArgumentResolver.java:184)
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.getNamedValueInfo(AbstractNamedValueMethodArgumentResolver.java:159)
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:106)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:122)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:224)
We saw four concurrent first requests to the same handler: one failed with the exception above, the
other three succeeded, and every later request to that handler succeeded.
Cause
8f03b06 (gh-37625) changed getParameterName() to return a local
variable instead of re-reading the field:
String parameterName = this.parameterName; // (1)
if (parameterName != null) {
return parameterName;
}
ParameterNameDiscoverer discoverer = this.parameterNameDiscoverer; // (2)
if (discoverer != null) {
...
if (parameterNames != null && this.parameterIndex < parameterNames.length) {
parameterName = parameterNames[this.parameterIndex];
this.parameterName = parameterName; // (3)
}
this.parameterNameDiscoverer = null; // (4)
}
return parameterName;Interleaving, with the MethodParameter instances shared across requests by HandlerMethod:
- Thread B reads
this.parameterNameat (1):null. - Thread A, already past (2), executes (3) and (4).
- Thread B reads
this.parameterNameDiscovererat (2):null, skips the block and returns its stale
localnull.
Up to 7.0.10 the method ended with return this.parameterName, so thread B picked up the name
thread A had just stored.
Reproducer
Repro.java:
import java.lang.reflect.Method;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.MethodParameter;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.core.SpringVersion;
public class Repro {
public void handler(String origin, String id) { }
static volatile MethodParameter current;
static volatile int round = 0;
static final AtomicInteger done = new AtomicInteger();
static final AtomicLong nulls = new AtomicLong();
public static void main(String[] args) throws Exception {
int iterations = 3_000_000;
int threads = 4;
Method method = Repro.class.getMethod("handler", String.class, String.class);
ParameterNameDiscoverer discoverer = DefaultParameterNameDiscoverer.getSharedInstance();
for (int t = 0; t < threads; t++) {
Thread thread = new Thread(() -> {
int seen = 0;
while (true) {
int r;
while ((r = round) == seen) {
Thread.onSpinWait();
}
if (r < 0) {
return;
}
seen = r;
MethodParameter parameter = current;
// Same two calls InvocableHandlerMethod + AbstractNamedValueMethodArgumentResolver make per request
parameter.initParameterNameDiscovery(discoverer);
if (parameter.getParameterName() == null) {
nulls.incrementAndGet();
}
done.incrementAndGet();
}
});
thread.setDaemon(true);
thread.start();
}
for (int i = 1; i <= iterations; i++) {
current = new MethodParameter(method, 1);
done.set(0);
round = i;
while (done.get() < threads) {
Thread.onSpinWait();
}
}
round = -1;
System.out.println("spring-core " + SpringVersion.getVersion() + ": fresh parameters=" + iterations
+ ", threads=" + threads + ", getParameterName()==null: " + nulls.get());
}
}In an empty directory containing only Repro.java (macOS/Linux; use ; as the classpath separator
on Windows):
curl -sfO https://repo1.maven.org/maven2/org/springframework/spring-core/7.0.10/spring-core-7.0.10.jar
curl -sfO https://repo1.maven.org/maven2/org/springframework/spring-core/7.0.11/spring-core-7.0.11.jar
curl -sfO https://repo1.maven.org/maven2/org/springframework/spring-core/7.1.0/spring-core-7.1.0.jar
curl -sfO https://repo1.maven.org/maven2/commons-logging/commons-logging/1.3.5/commons-logging-1.3.5.jar
javac -parameters -cp spring-core-7.0.11.jar -d out Repro.java
java -cp out:spring-core-7.0.10.jar:commons-logging-1.3.5.jar Repro
java -cp out:spring-core-7.0.11.jar:commons-logging-1.3.5.jar Repro
java -cp out:spring-core-7.1.0.jar:commons-logging-1.3.5.jar ReproOutput (JDK 25.0.5):
spring-core 7.0.10: fresh parameters=3000000, threads=4, getParameterName()==null: 0
spring-core 7.0.11: fresh parameters=3000000, threads=4, getParameterName()==null: 30191
spring-core 7.1.0: fresh parameters=3000000, threads=4, getParameterName()==null: 7340
The non-zero counts vary from run to run (between about 6,000 and 30,000 in our runs); 7.0.10 is
always 0.
Suggested fix
Re-read the field on the way out, as before:
this.parameterNameDiscoverer = null;
}
return this.parameterName;Both fields are volatile and (3) precedes (4), so a thread that sees a null discoverer left by
another thread also sees the name that thread stored. With this change applied on top of 7.0.11,
the reproducer reports 0 in two runs.
Versions
- Affected: 7.0.11 and 7.1.0 (both reproduced).
7.0.xandmaincontain the same code; not run there. - Not affected: 7.0.10.
- Seen with Spring Boot 4.1.3, Spring MVC on Tomcat, JDK 25.0.5.
Activity
Avoid race condition on initial parameter name discovery
Avoid race condition on initial parameter name discovery