Fix thread-safety issue in DefaultModelValidator (#11618)#11734
Open
abhu85 wants to merge 1 commit intoapache:masterfrom
Open
Fix thread-safety issue in DefaultModelValidator (#11618)#11734abhu85 wants to merge 1 commit intoapache:masterfrom
abhu85 wants to merge 1 commit intoapache:masterfrom
Conversation
The DefaultModelValidator class uses a shared HashSet (validIds) as an instance field while being a @singleton. When multiple threads validate models concurrently (e.g., using breadth-first dependency collector with `aether.dependencyCollector.impl=bf`), concurrent read/write operations on the HashSet cause ClassCastException: java.lang.ClassCastException: class java.util.HashMap$Node cannot be cast to class java.util.HashMap$TreeNode This occurs when the HashMap underlying the HashSet resizes and tree-ifies buckets while another thread is reading. The fix aligns with the Maven 4 implementation (maven-impl module) which already uses ConcurrentHashMap.newKeySet() for thread-safety. Changes: - Replace HashSet with ConcurrentHashMap.newKeySet() for validIds - Add concurrent validation test to prevent regression Fixes apache#11618
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
ClassCastExceptionProblem
DefaultModelValidatoris a@Singletonwith a shared mutableHashSet(validIds). When multiple threads validate models concurrently (e.g., using breadth-first dependency collector withaether.dependencyCollector.impl=bf), concurrent read/write operations on the HashSet cause:This affects users of:
aether.dependencyCollector.impl=bfFix Approach
Replace
new HashSet<>()withConcurrentHashMap.newKeySet()- identical to the solution already implemented in Maven 4'smaven-implmodule (lines 296, 298).Files Changed
compat/maven-model-builder/.../DefaultModelValidator.javaHashSet→ConcurrentHashMap.newKeySet()+ explanatory commentcompat/maven-model-builder/.../DefaultModelValidatorTest.javaTest Plan
testConcurrentValidation()addedmvn -pl compat/maven-model-builder testmvn -Prun-its verify(optional, if reviewer deems necessary)Compatibility
ConcurrentHashMap.newKeySet()provides sameSetinterfacemaven-implmoduleRisk Assessment
Low risk:
Fixes #11618