fix: resolve data race in storagev2 cacheProvider #173
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.
Requested by @zhangzqs
Summary
This PR fixes the data race issue in storagev2 package's cacheProvider component, specifically in the
FeedbackGood()method ofcacheResolver.Problem
The race detector detected concurrent read/write operations on shared memory locations within the
resolverCacheValuestruct. Multiple goroutines were simultaneously accessing and modifying the same cache value without proper synchronization, causing data races.Root Cause
The issue was in the
FeedbackGood()method where the code directly modified the cachedresolverCacheValueobject in place:rcv.IPs[i].ExpiredAt(line 266)rcv.RefreshAfter(line 271)rcv.ExpiredAt(line 272)When multiple goroutines called this method concurrently, they would read and write to the same memory locations simultaneously.
Solution
Instead of modifying the cached object in-place, the fix creates a copy of the
resolverCacheValuebefore making any modifications:resolverCacheValuestructThis ensures that each goroutine works with its own copy of the data, eliminating the race condition.
Test Plan
Fixes #172
Generated with codeagent