fix: eliminate data races on controller logger and term fields#907
Merged
mattisonchao merged 4 commits intomainfrom Feb 24, 2026
Merged
fix: eliminate data races on controller logger and term fields#907mattisonchao merged 4 commits intomainfrom
mattisonchao merged 4 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>
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>
Follow the same pattern as leaderController: create the logger once
without embedding the term, and pass slog.Int64("term", fc.term.Load())
explicitly in each log call. The previous slog.Any("term", term) with
*atomic.Int64 did not format correctly as an integer.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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
lcand readlc.logwithout holding the lock, whilesetLogger()called fromNewTerm()under the write lock could reassignlc.log. Fixed by capturinglc.loginto a local variable before spawning goroutines.termfield and logger recreation pattern withatomic.Int64, so the logger is created once and never reassigned. This eliminates the data race onlc.logwithout needing local captures in closures.slog.Any("term", term)with*atomic.Int64did not format correctly as an integer. Now passesslog.Int64("term", fc.term.Load())explicitly in each log call.Closes #902
Test plan
go build ./oxiad/dataserver/controller/follow/...compilesgo test ./oxiad/dataserver/controller/follow/...passes-raceflag to verify no data races remain on leader/follower controller log and term fields