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

BeanCurrentlyInCreationException is thrown when multiple threads simultaneously try to create bean (Spring 6.2.2) #34373

Closed
mpwong852 opened this issue Feb 6, 2025 · 5 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: duplicate A duplicate of another issue status: waiting-for-feedback We need additional information before we can continue

Comments

@mpwong852
Copy link

It is similar problem what is described in the issue #34186

The Spring version is "6.2.2" & Spring boot version is "3.4.2"

My application implements CommandLineRunner, and is a jar file.

The BeanCurrentlyInCreationException will be happen when I run the jar file sometime.

Below is the detail log:

2025-02-05 16:55:39,016207| INFO|TID=74|HAControllerThread|DefaultListableBeanFactory:|Creating singleton bean 'inplayIncidentDao' in thread "HAControllerThread" while other thread holds singleton lock for other beans

2025-02-05 16:55:39,065783|DEBUG|TID=1|main|DefaultListableBeanFactory:|Creating shared instance of singleton bean 'inplayIncidentDao'

Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'inplayIncidentDao': Requested bean is currently in creation: Is there an unresolvable circular reference or an asynchronous initialization dependency?

These issues occur randomly, and this problems is start from Spring Framework 6.2.x.

I never face those issue in Spring Framework 6.1.x

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 6, 2025
@mpwong852
Copy link
Author

@jhoeller

In Spring Framework 6.2.0, the BeanCurrentlyInCreationException is 100% happen.
After your help in Spring Framework 6.2.2 467d5f3 , the issue will not 100% happen, but still occur randomly.

Do you have any idea to avoid/ignore the "BeanCurrentlyInCreationException" ?

Many thank.

@jhoeller
Copy link
Contributor

jhoeller commented Feb 6, 2025

You could try the latest 6.2.3 snapshot which has another revision of the locking algorithm.

Where are the other threads actually coming from in your scenario? Spring bootstrapping is generally single-threaded, just with background threads for specific beans possibly. Uncontrolled multi-threaded initialization where multiple threads try to create the same beans is generally not desirable.

Completely avoiding such exceptions in such uncontrolled multi-threaded initialization scenarios is hard. We might have to provide an option to consistently enforce strong locking again, like it was in 6.1.x (at the expense of potentially running into deadlocks which could happen there in some situations).

@jhoeller jhoeller self-assigned this Feb 6, 2025
@jhoeller jhoeller added in: core Issues in core modules (aop, beans, core, context, expression) status: waiting-for-feedback We need additional information before we can continue labels Feb 6, 2025
@jhoeller
Copy link
Contributor

This case should be addressed by #34349. Please give the upcoming 6.2.3 snapshot a try!

If this still does not work for you, feel free to comment here and we'll reopen this issue.

@jhoeller jhoeller added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Feb 12, 2025
@mpwong852
Copy link
Author

@jhoeller

No BeanCurrentlyInCreationException anymore in 6.2.3 for my application
Thank you for your help.

@jhoeller
Copy link
Contributor

@mpwong852, glad to hear! Thanks for the feedback.

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) status: duplicate A duplicate of another issue status: waiting-for-feedback We need additional information before we can continue
Projects
None yet
Development

No branches or pull requests

3 participants