Skip to content

Fix a bug in ConstructorCache when classes are GC'ed but not removed from cache #6186

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

Merged
merged 3 commits into from
Jun 17, 2025

Conversation

alextwoods
Copy link
Contributor

Fix a bug in ConstructorCache when classes are GC'ed but not removed from cache

Motivation and Context

The ConstructorCache uses WeakReferences to hold Class objects, which can be garbage-collected, leading to Optional.empty() results even though the class may still be load-able. The ConstructorCache is using a WeakHashMap which handles GC of the keys only, but not when values (the class Objects in this case) are cleaned up.

The current design of the constructor cache was introduced to store the classloader and class with a weak reference, so that we don't prevent them from being unloaded. It was intended to follow We do something similar in DynamoDB enhanced's BeanTableSchema but, unlike the BeanTableSchema, this cache also uses weakreferences to the values being cached.

Modifications

Adds an additional check in the getClass method to check if the referent in the WeakReference has been garbage collected and if so, remove it from the cache.

Q: Why not use a ReferenceQueue to handle removals as in the implementation of WeakHashMap?
Short answer: it increases complexity more than is justified to handle an edge case error. Long answer: The schema of the cache (specially Map<String, Map<ClassLoader, Optional<WeakReference<Class<?>>>>>) makes this more difficult to track - we would need to create a new entry class that extends WeakReference and stores the className and classLoader and would need to ensure the reference to classLoader is also a weak reference in this entry. We would then need to add synchronization around check the reference queue and logic to remove cache entries from the nested cache correctly.

Testing

Testing this edge case is extremely difficult - I was able to manually reproduce the issue with a hacked custom class loader + multiple threads, but its not consistently reproducible, so I have not added an automated unit test to cover this case.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have read the CONTRIBUTING document
  • Local run of mvn install succeeds
  • My code follows the code style of this project
  • My change requires a change to the Javadoc documentation
  • I have updated the Javadoc documentation accordingly
  • I have added tests to cover my changes
  • All new and existing tests passed
  • I have added a changelog entry. Adding a new entry must be accomplished by running the scripts/new-change script and following the instructions. Commit the new file created by the script in .changes/next-release with your changes.
  • My change is to implement 1.11 parity feature and I have updated LaunchChangelog

License

  • I confirm that this pull request can be released under the Apache 2 license

@alextwoods alextwoods requested a review from a team as a code owner June 16, 2025 21:15
Comment on lines 67 to 71
if (classRef.isPresent() && classRef.get().get() == null) {
classesByClassLoader.remove(classLoader);
return getClass(className);
}
return classRef.map(WeakReference::get);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it still possible for the weak ref to get GC'd between 67 and 71? Would it be better to store classRef.get().get() to a temporary var to avoid that happening?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooff - yeah, good catch - I've updated.

On a related note - I had been focused on checking that this was thread safe - and because of the usage of synchronized maps, I believe it is - in particular the classesByClassLoader.remove(classLoader) and classesByClassLoader.computeIfAbsent generally ensure the cache is consistent. There is an issue where a class could be removed and re-loaded multiple times which would result in extra work, but not in an inconsistent/incorrect cache state.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, yeah agree don't think there's an issue where the cache could get corrupted in that case

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
72.7% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@alextwoods alextwoods added this pull request to the merge queue Jun 17, 2025
Merged via the queue into master with commit 569917b Jun 17, 2025
27 of 28 checks passed
Copy link

This pull request has been closed and the conversation has been locked. Comments on closed PRs are hard for our team to see. If you need more assistance, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 17, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants