fix(naming): fix check-then-act race conditions on ConcurrentHashMap in naming module#14751
Merged
KomachiSion merged 1 commit intoalibaba:developfrom Apr 2, 2026
Conversation
…in naming module Replace containsKey()+get() with single get()+null check in 4 methods: - ClientServiceIndexesManager.removePublisherIndexesByEmptyService - NamingMetadataManager.containInstanceMetadata - NamingFuzzyWatchContextService.removeFuzzyWatchContext - ServiceManager.removeSingleton
|
Thanks for your this PR. 🙏 感谢您提交的PR。 🙏 |
KomachiSion
approved these changes
Apr 1, 2026
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
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.
Problem
Four methods in the naming module use
containsKey()followed byget()onConcurrentHashMap/ConcurrentMapfields. Although each method is individually thread-safe, the compound operation is not atomic, creating a check-then-act race condition that can causeNullPointerException.Race Condition Explained
Take
ServiceManager.removeSingleton()as an example:Between Step 1 and Step 2, another thread may remove the entry:
ConcurrentHashMapguarantees thread-safety for individual operations, but NOT for compound operations spanning two separate method calls.Fix
Replace all
containsKey()+get()patterns with a singleget()call and null check:All 4 Fix Points
ClientServiceIndexesManagerremovePublisherIndexesByEmptyServiceConcurrentMap<Service, Set<String>>NamingMetadataManagercontainInstanceMetadataConcurrentMap<Service, ConcurrentMap<String, InstanceMetadata>>NamingFuzzyWatchContextServiceremoveFuzzyWatchContextConcurrentMap<String, Set<String>>ServiceManagerremoveSingletonConcurrentHashMap<String, Set<Service>>All four fields are modified at runtime by event handlers and background tasks, making the race condition window practical.
Tests
All existing tests pass (31 tests across
ClientServiceIndexesManagerTest,NamingMetadataManagerTest,NamingFuzzyWatchContextServiceTest). The fix is behavior-preserving under non-concurrent conditions - it only prevents NPE when concurrent map modifications occur.Impact
Prevents potential NPE in naming service operations (service registration, metadata lookup, fuzzy watch, service removal). No behavioral change under normal single-threaded access.