-
Notifications
You must be signed in to change notification settings - Fork 265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: unprotected read data race #998
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -797,6 +797,15 @@ func (ndb *nodeDB) resetLegacyLatestVersion(version int64) { | |
ndb.legacyLatestVersion = version | ||
} | ||
|
||
// safeGetLatestVersion returns the value of ndb.latestVersion directly | ||
// without additional logic performed by getLatestVersion. | ||
func (ndb *nodeDB) safeGetLatestVersion() int64 { | ||
ndb.mtx.Lock() | ||
defer ndb.mtx.Unlock() | ||
|
||
return ndb.latestVersion | ||
} | ||
|
||
Comment on lines
+800
to
+808
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Direct accesses to latestVersion need to be protected Several unprotected accesses to
These locations should be updated to use the new 🔗 Analysis chainLGTM! Thread-safe implementation for accessing latestVersion. The new method correctly implements thread-safe access to Let's verify that all direct accesses to Let me analyze the direct accesses to verify if they are properly protected. I'll check the mutex usage patterns in these locations. 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Search for any remaining direct access to latestVersion that might not be protected
# We should only see accesses within methods that already hold the mutex
rg -A 3 "ndb\.latestVersion\s*[^=]*" --type go
Length of output: 4880 Script: #!/bin/bash
# Check mutex lock/unlock patterns around these accesses
# Looking for mtx.Lock() calls before these accesses
# First, let's check nodedb.go for mutex patterns
rg -B 5 "mtx\.(Lock|Unlock)" nodedb.go
# Then, check if the test file has any mutex protection
rg -B 5 "mtx\.(Lock|Unlock)" nodedb_test.go
# Finally, check mutable_tree.go for the context of that access
rg -B 10 "version == tree\.ndb\.latestVersion" mutable_tree.go
Length of output: 6644 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mmm, interesting, @cool-develope should I do as suggested here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, will check quickly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
func (ndb *nodeDB) getLatestVersion() (int64, error) { | ||
ndb.mtx.Lock() | ||
latestVersion := ndb.latestVersion | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can use
t.ndb.getLatestVersion()
(thread safe)