[SharovBot] node/eth: fix data race between MergeLoop and Aggregator.Close - #22245
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
When eth.New is called, it creates its own sentryCtx/sentryCancel pair (derived from context.Background, not from the ctx parameter). Stop() cancels sentryCtx via sentryCancel(), then waits on bgComponentsEg. Previously, MergeLoop was launched with the outer ctx parameter, so sentryCancel() did NOT cancel the MergeLoop goroutine. bgComponentsEg.Wait() would return immediately because MergeLoop was still running (its context was the outer ctx, not yet cancelled), and then chainDB.Close() → Aggregator.Close() would write Aggregator fields while MergeLoop still read them, triggering a DATA RACE in TestImportClosesChaindataOnInitError. Fix: capture backend.sentryCtx into a local variable and pass it to MergeLoop, so Stop() → sentryCancel() reliably terminates the goroutine before bgComponentsEg.Wait() returns. Co-authored-by: Giulio Rebuffo <giulio.rebuffo@gmail.com>
erigon-copilot
Bot
requested review from
AskAlexSharov and
yperbasis
as code owners
July 5, 2026 09:57
Contributor
Author
|
[SharovBot] Closing this PR as it's superseded by the already-merged PR #22244 which fixes the same DATA RACE via a complementary approach (tracking MergeLoop in bgComponentsEg). The root cause analysis was correct — MergeLoop was using the outer ctx instead of sentryCtx — but #22244 + #22203 together fully resolve the race with a cleaner approach. |
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
TestImportClosesChaindataOnInitErrorwas triggering a DATA RACE detected by-race:Root Cause
eth.Newcreates its ownsentryCtx/sentryCancelpair (derived fromcontext.Background(), not from thectxparameter).Stop()cancels viasentryCancel(), then waits onbgComponentsEg.However,
MergeLoopwas launched with the outerctxparameter — notbackend.sentryCtx. SosentryCancel()did not terminate the MergeLoop goroutine.bgComponentsEg.Wait()returned immediately (the goroutine was still running), and thenchainDB.Close()→Aggregator.Close()would write Aggregator fields while MergeLoop concurrently read them.This race surfaces in the test because
importChaincallsdefer ethereum.Stop()andrunErigonCommandcallsdefer cancel()— the outer context is only cancelled afterStop()has already returned.Fix
Capture
backend.sentryCtxinto a local variable and pass it toMergeLoop, soStop()→sentryCancel()reliably cancels the goroutine beforebgComponentsEg.Wait()returns.Verification
3 consecutive passing runs with
-race, no DATA RACE warnings.CI job: https://github.com/erigontech/erigon/actions/runs/28674562917/job/85045122311