[SharovBot] db/state: fix DATA RACE between Aggregator.Close and MergeLoop goroutine - #22227
Closed
erigon-copilot[bot] wants to merge 1 commit into
Closed
erigon-copilot[bot] wants to merge 1 commit into
erigon-copilot[bot] wants to merge 1 commit into
Conversation
…eLoop goroutine The background MergeLoop goroutine called a.wg.Add(1) inside MergeLoop itself, but the spawning goroutine in backend.go could race with Close() calling a.wg.Wait(): if Close() observed the WaitGroup counter at zero before the goroutine executed wg.Add(1), Wait() returned early and Close proceeded to write fields (ctxCancel = nil) concurrently with MergeLoop starting up. The race detector reported: WARNING: DATA RACE Write at ... (*Aggregator).Close() aggregator.go:630 Previous read at ... (*Aggregator).MergeLoop() aggregator.go:1228 Fix: add StartMergeLoopBackground to Aggregator (and the TemporalDebugDB interface) which calls wg.Add(1) synchronously before launching the goroutine. This guarantees the counter is non-zero before Close/wg.Wait can be called, eliminating the race. Verified: go test -race -count=3 -run TestImportClosesChaindataOnInitError ./cmd/utils/app/... passes cleanly. Co-authored-by: Giulio Rebuffo <giulio.rebuffo@gmail.com>
erigon-copilot
Bot
requested review from
AskAlexSharov,
sudeepdino008 and
yperbasis
as code owners
July 4, 2026 03:48
Collaborator
|
fixed by #22203 |
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.
[SharovBot]
Problem
The CI job
race-tests / tests-linux (ubuntu-latest, other, parallel)on commitbc4670467failed with:Root cause: The background merge goroutine spawned in
backend.gocalleda.wg.Add(1)insideMergeLoop(), after the goroutine was scheduled. A concurrentClose()could observe a zero WaitGroup counter, return fromwg.Wait()immediately, and then write to struct fields (e.g.ctxCancel = nil) concurrently with the goroutine'swg.Add(1)/ mergeLoop startup — a classic wg.Add/wg.Wait race.Fix
Added
StartMergeLoopBackground(ctx, onErr)toAggregator(and theTemporalDebugDBinterface) which callswg.Add(1)synchronously before spawning the goroutine. This guarantees the counter is non-zero before anyClose()/wg.Wait()can execute.backend.gonow usesStartMergeLoopBackgroundinstead of a manual goroutine wrappingMergeLoop.Verification
No DATA RACE warnings. 3/3 passes.
Fixes: https://github.com/erigontech/erigon/actions/runs/28674562917/job/85045122311