Guard discv4/discv5 protocol pointers on the generic node - #35
Merged
pk910 merged 1 commit intoJul 29, 2026
Merged
Conversation
The generic node published its v4Node and v5Node pointers with no synchronization. SetV4 and SetV5 wrote them lock free while V4, V5, HasV4, HasV5, PeerID, Enode, UpdateENR and CalculateScore read them lock free. These run on different goroutines in normal operation, for example protocol detection updating the pointers while a scoring sweep, a database batch or the web UI reads them, so the access is a data race. The check then deref readers are worse: SetV5(nil) can clear the pointer between the nil check and the call, so PeerID and CalculateScore can dereference nil and crash the daemon. Guard both pointers with the node mutex that already protects addr. Simple accessors take the read lock. SetV4 and SetV5 take the write lock around the pointer store only. The check then deref readers capture the pointer under the read lock and release before the call, so a concurrent clear can no longer be observed mid call.
MysticRyuujin
approved these changes
Jul 21, 2026
pk910
approved these changes
Jul 29, 2026
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
The generic node in the nodes package published its
v4Nodeandv5Nodepointers with no synchronization.SetV4andSetV5wrote them lock free whileV4,V5,HasV4,HasV5,PeerID,Enode,UpdateENRandCalculateScoreread them lock free. These run on different goroutines in normal operation (protocol detection updates the pointers while a scoring sweep, a database batch or the web UI reads them), so the access is a data race. The check then deref readers are worse:SetV5(nil)can clear the pointer between the nil check and the call, soPeerIDandCalculateScorecan dereference nil and crash the daemon.Fix
Guard both pointers with the node mutex that already protects
addr. Simple accessors take the read lock.SetV4andSetV5take the write lock around the pointer store only. The check then deref readers capture the pointer under the read lock, release, then operate on the local copy, so a concurrent clear can no longer be observed mid call.Tests
SetV4/SetV5against every reader, which fails under the race detector if any pointer access is unsynchronizedPeerIDrunsBoth pass under the race detector, alongside
go build ./...andgo vet.Notes
addrand was already read forv4NodeinsideSetAddr, so reusing it keeps the locking model simple and consistent.UpdateENRstill writes theenrfield lock free, and there is a separate unsynchronized stats pointer inside the discv5 node. Both are distinct from the protocol pointer race fixed here and are left for their own changes.