Make SingletonScope scoped Provider a static nested class - #1941
Open
arimu1 wants to merge 1 commit into
Open
Conversation
Anonymous providers retain SingletonScope.this, which can pin the scope if a provider is held from a long-lived thread-local. Pass key/creator explicitly via the constructor instead. Fixes google#1929
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.
Summary
SingletonScope.scope()previously returned an anonymous (non-static)Provider. In Java, that always retains an implicit outer reference toSingletonScope.this, even though the provider only needskeyandcreator(plus static scope state).If such a provider is retained from a long-lived structure (e.g. thread pool /
ThreadLocalcontext), that outer reference can keep the scope instance reachable longer than necessary.This change:
SingletonProviderkey/creatorexplicitly through the constructortoString)Fixes #1929
Test plan
mvn -pl core test -Dtest=ScopesTest,CircularDependencyTest,EagerSingletonTest(JDK 21)testSingletonScopedProviderDoesNotRetainOuterScopeasserts static nested class and nothis$synthetic field, plus still caches instancesNotes