AbstractBeanFactory's interaction with BeanPostProcessorCacheAwareList is not fully thread-safe #29299
Labels
in: core
Issues in core modules (aop, beans, core, context, expression)
type: regression
A bug that is also a regression
Milestone
Affects: 5.3.x
Hi,
AbstractBeanFactory implementation is causing issues in our application since version 5.3. The application randomly failed to initialize with the following error :
WARN o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'errorPageFilterRegistration' defined in org.springframework.boot.web.servlet.support.Error PageFilterConfiguration: Unsatisfied dependency expressed through method 'errorPageFilterRegistration' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'errorPageFilter' defined in org.springframework.boot.web.servlet.support.ErrorPageFilterConfiguration: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.spring framework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.autoconfigu re.web.servlet.error.ErrorMvcAutoConfiguration]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration.<init>() []
I finally managed to understand what is happening and believe this can and should be fixed in spring-beans.
BeanPostProcessorCacheAwareList was introduced in AbstractBeanFactory in 5.3.
BeanPostProcessorCacheAwareList and accesses to the beanPostProcessors instance are not Thread safe. If multiple Threads are running during initialization and a Thread calls getBeanPostProcessorCache() while another Thread is calling addBeanPostProcessors, you can end up with a cache which does not contain all BeanPostProcessor instances and thus doesn't find the appropriate constructor.
Making BeanPostProcessorCacheAwareList and its usages in AbstractBeanFactory thread safe is relatively straightforward with synchronized blocks and would prevent such very obscure initialization issues.
Kind regards,
Xavier
The text was updated successfully, but these errors were encountered: