Enforce circular reference exception between more than two threads as well

See gh-34672
This commit is contained in:
Juergen Hoeller
2025-04-07 22:37:19 +02:00
parent 463541967a
commit 74ab5e4e25
2 changed files with 44 additions and 32 deletions

View File

@@ -315,7 +315,7 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements
while ((singletonObject = this.singletonObjects.get(beanName)) == null) {
Thread otherThread = this.currentCreationThreads.get(beanName);
if (otherThread != null && (otherThread == currentThread ||
this.lenientWaitingThreads.get(otherThread) == currentThread)) {
checkDependentWaitingThreads(otherThread, currentThread))) {
throw ex;
}
if (!this.singletonsInLenientCreation.contains(beanName)) {
@@ -431,6 +431,16 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements
}
}
private boolean checkDependentWaitingThreads(Thread waitingThread, Thread candidateThread) {
Thread threadToCheck = waitingThread;
while ((threadToCheck = this.lenientWaitingThreads.get(threadToCheck)) != null) {
if (threadToCheck == candidateThread) {
return true;
}
}
return false;
}
/**
* Determine whether the current thread is allowed to hold the singleton lock.
* <p>By default, any thread may acquire and hold the singleton lock, except