fix: use thread-safe collections in SchemaDescriptorRegistry#16172
Open
daguimu wants to merge 1 commit intoapache:3.3from
Open
fix: use thread-safe collections in SchemaDescriptorRegistry#16172daguimu wants to merge 1 commit intoapache:3.3from
daguimu wants to merge 1 commit intoapache:3.3from
Conversation
SchemaDescriptorRegistry has three thread-safety issues: 1. SERVICES uses a plain HashSet accessed concurrently during service registration. Concurrent HashSet.add() can corrupt internal state. 2. addExtension() has a check-then-act race on EXTENSIONS: two threads can both see containsKey()==false, both put a new HashMap, and one overwrites the other's entries. 3. The inner HashMap in EXTENSIONS is also accessed concurrently via fdMap.put() without synchronization. Fix: Replace HashSet with ConcurrentHashMap.newKeySet(), replace check-then-act with computeIfAbsent(), and use ConcurrentHashMap for the inner map.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## 3.3 #16172 +/- ##
=========================================
Coverage 60.81% 60.82%
+ Complexity 11765 11754 -11
=========================================
Files 1953 1953
Lines 89118 89115 -3
Branches 13444 13443 -1
=========================================
+ Hits 54197 54202 +5
+ Misses 29364 29354 -10
- Partials 5557 5559 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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
SchemaDescriptorRegistryhas three thread-safety issues. This class is called during gRPC service registration, which can happen from multiple threads concurrently.Issue 1:
SERVICESuses plainHashSetIssue 2: Check-then-act race in
addExtension()Issue 3: Inner
HashMapaccessed concurrentlyThe inner
Map<Integer, FileDescriptor>created inaddExtension()is a plainHashMap, butfdMap.put()can be called concurrently from different threads registering different extensions for the same containing type.Fix
Three minimal changes (net -6 lines):
SERVICES:new HashSet<>()→ConcurrentHashMap.newKeySet()addExtension: check-then-act →EXTENSIONS.computeIfAbsent(name, k -> new ConcurrentHashMap<>()).put(number, fd)new HashMap<>()→new ConcurrentHashMap<>()(via thecomputeIfAbsentlambda)All changes are API-compatible drop-in replacements with zero behavioral change.
Tests
No new tests added — this is a pure collection type replacement. The class has no existing tests and creating
FileDescriptorinstances requires protobuf compilation infrastructure. The fix is self-evidently correct: replacing non-thread-safe collections with their concurrent equivalents.Impact