fix(finality-grandpa): eliminate data races in voter, timer, and test infrastructure#4849
Merged
Merged
Conversation
haikoschol
approved these changes
May 14, 2026
EclesioMeloJunior
approved these changes
May 14, 2026
timwu20
reviewed
May 14, 2026
timwu20
requested changes
May 14, 2026
Contributor
timwu20
left a comment
There was a problem hiding this comment.
Nice cleanup overall — the race fixes in timer.go, wakerChan, environment_test.go, and the missing Unlock in processBestRound all look right.
My one piece of feedback is on the VoterState snapshot change — left an inline comment on publishVoterStateSnapshot. tl;dr: the underlying Get() race is worth fixing, but the snapshot/atomic.Pointer pattern feels heavy given that the only current callers of Get() are tests.
Otherwise LGTM.
Contributor
Author
|
@timwu20 changes applied. Could you please take one more look? |
timwu20
approved these changes
May 19, 2026
…ound env.FinalizeBlock
1cd3bd8 to
542ae31
Compare
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.
Changes
Fix all data races detected by
go test -race ./pkg/finality-grandpa(137 warnings ondevelopment, 0 after this PR):voter.go—wakerChan.waker: changed from*wakertoatomic.Pointer[waker]so the goroutine instart()and callers ofsetWaker()no longer race on pointer publication.voter.go—sharedVoteState.Get(): replaced ad-hocsvs.mtx(which never synchronized with the writer) with a snapshot pattern — the voter loop publishes aVoterStateReportintoatomic.PointerandGet()reads it withoutlocking.
voter.go—pruneBackgroundRounds: collect finalize notifications underinner.Mutex, release the lock, then invokeenv.FinalizeBlockoutside it. Holding the mutex across a user-supplied callback was both a deadlock hazard (a slowenvironment could stall observers and
Stop()) and what forcedGet()to give up on locking in the first place. A snapshot is also published right after releasing the lock so concurrent readers see fresh state even while we're inside a slow callback.voter.go—processBestRound: fixed a pre-existing missinginner.Unlock()on the error path frombestRound.poll.timer.go:expiredis nowatomic.Bool(was written under a mutex, read without). The one-shot close ofwakerChan.inis now guarded bysync.Once, replacing the previous mutex + flag combo.environment_test.go—BroadcastNetwork: protectedsenders/historywith a mutex, added astopchannel and a separate WaitGroup so forwarder goroutines exit beforeStop()closes the receiver (previously caused send-on-closed-channel races and potential panics).voter_test.go—TestBuffered: the sharedrunbool is nowatomic.Bool.Tests
go test -race ./pkg/finality-grandpaShould report ok with no WARNING: DATA RACE. Before this PR the same command produced 137 warnings.
Issues
ChainSafe/go-jam#523