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

ClassCastException in AbstractBeanDefinition.getBeanClassName() when getting bean multithreaded #30773

Closed
mathekcbo opened this issue Jun 29, 2023 · 7 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: regression A bug that is also a regression
Milestone

Comments

@mathekcbo
Copy link

mathekcbo commented Jun 29, 2023

When calling prototype Beans the first time in a multithreaded manner, the following exception can happen:

Exception in thread "Thread-2" java.lang.ClassCastException: class java.lang.Class cannot be cast to class java.lang.String (java.lang.Class and java.lang.String are in module java.base of loader 'bootstrap')
        at org.springframework.beans.factory.support.AbstractBeanDefinition.getBeanClassName(AbstractBeanDefinition.java:393)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1535)
        at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1502)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:492)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:344)
       ...

If this happens, the context/runtime will never raise this exception again. So it seems to be an issue only on startup.
This seems to happen since Spring Boot 3. There is nothing to find on google. We assume this error to be unknown so far.

To simplify things and for reproduction, we created a repository that can demonstrate the behaviour:
Git Demo Repo for this issue

Please elaborate.
Thanks in advance

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 29, 2023
@wilkinsona
Copy link
Member

AbstractBeanDefinition is part of Spring Framework. We'll transfer this issue so that they can investigate.

@bclozel bclozel transferred this issue from spring-projects/spring-boot Jun 29, 2023
@quaff
Copy link
Contributor

quaff commented Jun 29, 2023

Reproduced with my local environment.

@jhoeller jhoeller changed the title java.lang.ClassCastException in BeanFactory when getting Bean multithreaded ClassCastException in AbstractBeanDefinition.getBeanClassName() when getting bean multithreaded Jun 29, 2023
@wilkinsona
Copy link
Member

Thanks for trying it, @quaff. Looking at the code in AbstractBeanDefinition I can see that getBeanClassName isn't thread-safe, but I couldn't reproduce it with the sample even after experimenting with different numbers of threads.

@quaff, @mathekcbo, what OS, JVM, and so on did you use and how many iterations were required before the failure occurred?

@jhoeller jhoeller added type: regression A bug that is also a regression in: core Issues in core modules (aop, beans, core, context, expression) and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jun 29, 2023
@jhoeller jhoeller self-assigned this Jun 29, 2023
@jhoeller jhoeller added this to the 6.0.11 milestone Jun 29, 2023
@jhoeller
Copy link
Contributor

jhoeller commented Jun 29, 2023

This is a side effect of f64cc08 where we tried to apply instanceof pattern matching on our new JDK 17 baseline across the codebase, and accidentally lost the defensive access to the beanClass field that we had in 5.3.x there. I'll restore this for 6.0.11, with pattern matching but on a defensive copy of the field still.

@mathekcbo
Copy link
Author

Thanks for trying it, @quaff. Looking at the code in AbstractBeanDefinition I can see that getBeanClassName isn't thread-safe, but I couldn't reproduce it with the sample even after experimenting with different numbers of threads.

@quaff, @mathekcbo, what OS, JVM, and so on did you use and how many iterations were required before the failure occurred?

OS: Windows and Linux
JVM: JDK 17 Temurin
Reproduction: It may occur after some seconds (in the shell loop) or after 15 minutes. It differs

@jhoeller
Copy link
Contributor

This should be restored for the upcoming 6.0.11 snapshot now, in sync with the original behavior in the 5.3.x line.

@mathekcbo
Copy link
Author

Thank you for this quick response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: regression A bug that is also a regression
Projects
None yet
Development

No branches or pull requests

5 participants