fix: eliminate data race on leaderController.log via atomic term#903
Merged
mattisonchao merged 2 commits intomainfrom Feb 24, 2026
Merged
fix: eliminate data race on leaderController.log via atomic term#903mattisonchao merged 2 commits intomainfrom
mattisonchao merged 2 commits intomainfrom
Conversation
Closures passed to goroutines and async callbacks captured `lc` (the struct pointer) and later read `lc.log` without holding the lock. Meanwhile, `setLogger()` called from `NewTerm()` under the write lock could reassign `lc.log`, causing a data race detected by `-race`. Fix by capturing `lc.log` into a local variable before spawning the goroutine or creating the callback, following the existing pattern used in `propose()` for `lc.wal`, `lc.quorumAckTracker`, `lc.term`. Affected methods: Read, list, RangeScan, GetNotifications, proposeFeaturesEnable, ProposeRecordChecksum. Closes #902 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1cf5145 to
7a81fa5
Compare
90d599c to
ab8495c
Compare
Replace the mutable term field and logger recreation pattern with an atomic.Int64 term, so the logger is created once and never reassigned. This eliminates the data race on lc.log without needing local log captures in closures. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
ab8495c to
00558e2
Compare
Member
Author
Review SummaryComprehensive review of the diff against What changed
Verified
Known limitation
|
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.
Summary
leaderController.terman*atomic.Int64instead of a plainint64atomicInt64Valuetype implementingslog.LogValuerso the logger resolves the term dynamically at log timesetLogger()entirely — the logger is now created once in the constructor and never reassignedlog := lc.loglocal captures (no longer needed sincelc.logis immutable after construction)Term()getter to a lock-freelc.term.Load()Test plan
go build ./...passesgo test -race -count=1 -run TestLeaderController ./...passesgo test -race -count=1 ./...passes (full package)