fix: data race in ehash due to unsynchronized lazy init of Map.typ#4
Open
stuchl4n3k wants to merge 1 commit intolrita:masterfrom
Open
fix: data race in ehash due to unsynchronized lazy init of Map.typ#4stuchl4n3k wants to merge 1 commit intolrita:masterfrom
stuchl4n3k wants to merge 1 commit intolrita:masterfrom
Conversation
lrita
reviewed
Mar 29, 2026
| lock sync.Mutex | ||
| inode unsafe.Pointer // *inode2 | ||
| typ *rtype | ||
| typ atomic.Pointer[rtype] |
Owner
There was a problem hiding this comment.
Could you use unsafe.Pointer here, or upgrade the minimum dependency in go.mod to a version that supports this type?
| m.lock.Unlock() | ||
| } | ||
|
|
||
| typ := m.typ.Load() |
Owner
There was a problem hiding this comment.
We can move this to the beginning of the function so it only loads once for the most common scenarios. Meanwhile, let's refine the double-check logic to make it works fine.
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.
ehash uses double-checked locking to lazily initialize m.typ on first
use. The outer check reads m.typ without synchronization, while the
inner write happens under m.lock. This violates Go's memory model —
a concurrent reader can observe a partially written pointer.
Replace the bare *rtype field with atomic.Pointer[rtype] and use
Load/Store in ehash so the fast path is a single atomic load and the
write under the lock is correctly published to other goroutines.