Repository navigation
Conversation
…creation getBeanNamesForType computes its result before caching it. A singleton that is published in between, and matches the requested type only by its instance (for example via a factory method declaring a supertype), evicts matching cache entries before the stale result is put, so the bean stays invisible to by-type lookups until the caches are cleared. The same applies to registerSingleton, where the manual singleton name becomes visible to lookups only after the eviction. A generation counter, incremented on every eviction and cache clear, lets an overlapping lookup remove its own entry again, and registerSingleton evicts once more after registering the name. The lookup no longer uses computeIfAbsent, so that type matching (which may create beans) does not run while holding a lock on the cache. See spring-projectsgh-37516 Signed-off-by: Eric Gross <eric.gross@salesforce.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related: #37517 (same get/put change for the deadlock in gh-37516); this PR additionally fixes a stale cache entry.
With a frozen configuration,
getBeanNamesForTypecomputes the matching bean names and then caches them, whileaddSingletonevicts cached entries that the new singleton instance matches (added in 6.2.10 for gh-35239). Nothing orders the two:getBeanNamesForType(Impl.class). Beanimplis not created yet and its@Beanmethod declaresIface, so its predicted type does not match.impl.addSingleton's eviction finds nothing forImpl, since A has not cached yet.A's own result is just the usual "lookup before creation" outcome. The problem is that it stays cached: every later lookup and autowiring by
Impl(findAutowireCandidates->beanNamesForTypeIncludingAncestors(..., allowEagerInit=true)) missesimpluntil the caches are cleared, failing withNoSuchBeanDefinitionExceptionalthough the bean exists.registerSingletonhas the same gap: its eviction runs insideaddSingleton, before the manual singleton name is visible to lookups. Affects the Class-keyed cache since 6.2.10 and the ResolvableType-keyed cache since 7.1.0 (14a1cbb, gh-37382).From the code, stock Spring can hit this (we have not observed it on unmodified Spring) when:
getBeansOfType,ObjectProvider.stream(), prototype autowiring); none of these take the singleton lock.@Bean(bootstrap = BACKGROUND)) creates beans while a background bean or anObjectProviderdoes a by-type lookup.We found it while investigating an intermittent startup failure (roughly 1 in 200 startups) in a large application running a locally patched Spring with additional concurrent startup: a field injection by concrete type of a bean whose
@Beanmethod declares the interface.Fix: a generation counter, incremented on each eviction and cache clear. A lookup reads it before computing and, if it changed by the time the result is cached, removes its own entry (
remove(key, value)). Either the lookup sees the new instance, or the lookup or the eviction removes the entry.registerSingletonevicts again after registering the name.Like #37517, the lookup uses get/put instead of
computeIfAbsent, so type matching (which may create beans) no longer runs under the cache's segment lock (gh-37516). This PR includes that change; happy to rebase onto #37517 instead.Trade-offs: one
AtomicLongincrement per singleton publication. A lookup that overlaps any publication, matching or not, does not cache its result and computes again on the next call. That is never worse than no cache, but caching is less effective while many singletons are being published concurrently. Concurrent first lookups of one type may compute twice (as with #37517). Not covered (pre-existing): a FactoryBean whose object type only becomes known from its instance.Tests:
BeanNamesByTypeCacheRaceTestspauses the lookup thread in apredictBeanTypecallback for a bean registered afterimpl, publishesimplfrom the test thread, then lets the lookup finish. Covers Class and ResolvableType lookups for both caches, plusregisterSingleton. All 5 fail on main (ced0465) and pass with the fix.