Affects: 5.1.9.RELEASE
During singleton creation, DefaultSingletonBeanRegistry synchronises on this.singletonObjects:
|
synchronized (this.singletonObjects) { |
While synchronized, it then uses the singletonFactory to create the singleton:
|
singletonObject = singletonFactory.getObject(); |
This call into user code while holding a lock can result in deadlock. We've seen one example reported in this Spring Boot issue where Micrometer is also involved. I've also reproduced a very similar problem without Micrometer and with no synchronization in user code:
package example;
import javax.annotation.PostConstruct;
import javax.validation.Validator;
import javax.validation.constraints.Max;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.validation.annotation.Validated;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
public class SingletonCreationDeadlockTests {
@Test
public void create() {
new AnnotationConfigApplicationContext(Config.class).close();;
}
private static final class Registry {
private final ConfigProperties properties;
Registry(ConfigProperties properties) {
this.properties = properties;
}
void register() {
this.properties.getSetting();
}
}
@Validated
static class ConfigProperties {
@Max(10)
private int setting = 5;
public int getSetting() {
return this.setting;
}
public void setSetting(int setting) {
this.setting = setting;
}
}
@Configuration
static class Config {
@Bean
public Registry registry(ConfigProperties properties) {
return new Registry(properties);
}
@Bean
public ConfigProperties properties() {
return new ConfigProperties();
}
@Bean
public LocalValidatorFactoryBean localValidatorFactoryBean() {
return new LocalValidatorFactoryBean();
}
@Bean
public static MethodValidationPostProcessor methodValidationPostProcessor(@Lazy Validator validator) {
MethodValidationPostProcessor postProcessor = new MethodValidationPostProcessor();
postProcessor.setValidator(validator);
return postProcessor;
}
@Bean
public Registrar registrar(Registry registry) {
return new Registrar(registry);
}
}
static class Registrar {
private final Registry registry;
Registrar(Registry registry) {
this.registry = registry;
}
@PostConstruct
void register() {
Thread thread = new Thread(() -> {
registry.register();
});
thread.start();
try {
thread.join();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
}
Here's a zip of a complete project containing the above test: singleton-creation-deadlock.zip
The deadlock occurs because the main thread has locked singletonObjects and then waits for the thread created by Registrar to complete. The thread created by Registrar ends up waiting to lock singletonObjects due to ConfigProperties being @Validated and the resolution of the @Lazy Validator requiring a call to DefaultListableBeanFactory.doResolveDependency which results in a call to DefaultSingletonBeanRegistry.getSingleton where the attempt to lock singletonObjects is made.
Affects: 5.1.9.RELEASE
During singleton creation,
DefaultSingletonBeanRegistrysynchronises onthis.singletonObjects:spring-framework/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java
Line 204 in b1171d8
While synchronized, it then uses the
singletonFactoryto create the singleton:spring-framework/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java
Line 222 in b1171d8
This call into user code while holding a lock can result in deadlock. We've seen one example reported in this Spring Boot issue where Micrometer is also involved. I've also reproduced a very similar problem without Micrometer and with no synchronization in user code:
Here's a zip of a complete project containing the above test: singleton-creation-deadlock.zip
The deadlock occurs because the main thread has locked
singletonObjectsand then waits for the thread created byRegistrarto complete. The thread created byRegistrarends up waiting to locksingletonObjectsdue toConfigPropertiesbeing@Validatedand the resolution of the@LazyValidatorrequiring a call toDefaultListableBeanFactory.doResolveDependencywhich results in a call toDefaultSingletonBeanRegistry.getSingletonwhere the attempt to locksingletonObjectsis made.