Skip to content

[SharovBot] fix: track MergeLoop goroutine in bgComponentsEg to prevent data race on shutdown - #22244

Merged
AskAlexSharov merged 1 commit into
mainfrom
agent-fix/race-mergeloop-bgcomponentseg
Jul 5, 2026
Merged

AskAlexSharov merged 1 commit into
mainfrom
agent-fix/race-mergeloop-bgcomponentseg

Conversation

@erigon-copilot

@erigon-copilot erigon-copilot Bot commented Jul 5, 2026 •

Copy link
Copy Markdown
Contributor

[SharovBot]

Problem

A data race was detected in TestImportClosesChaindataOnInitError (race-tests CI job, 2026-07-03):

WARNING: DATA RACE
Write at 0x00c0653acb90 by goroutine 321:
  github.com/erigontech/erigon/db/state.(*Aggregator).Close()
      db/state/aggregator.go:630

Previous read at 0x00c0653acb90 by goroutine 373:
  github.com/erigontech/erigon/db/state.(*Aggregator).MergeLoop()
      db/state/aggregator.go:1228
  github.com/erigontech/erigon/node/eth.New.func16()
      node/eth/backend.go:1133

Context

PR #22203 (merged 2026-07-04) addressed this race by replacing sync.WaitGroup with a closingWaitGroup latch in Aggregator, making MergeLoop's TryAdd() properly ordered against Close()'s BeginClose()+Wait().

This PR

This PR provides an additional, complementary fix: track the MergeLoop goroutine in bgComponentsEg so Stop() → bgComponentsEg.Wait() explicitly waits for the MergeLoop goroutine to exit before chainDB.Close() is called.

Without this, bgComponentsEg.Wait() in Stop() returns without waiting for the MergeLoop goroutine (since it was launched as a bare go func()), meaning the goroutine could theoretically still be running when chainDB.Close() begins. The closingWaitGroup handles the WaitGroup reuse race, but this PR makes the shutdown ordering explicit and unambiguous.

Changes:

  • Moves the MergeLoop goroutine from a bare go func() to backend.bgComponentsEg.Go()
  • Fixes the typo: "snapashot" → "snapshot" in the error message
  • Filters context cancellation errors (expected on shutdown) from logger.Error

Testing

  • go test -race -count=10 ./cmd/utils/app/ -run TestImportClosesChaindataOnInitError — all 10 runs PASS, no data race
  • go build ./... — succeeds
  • No test files modified

Fixes CI: https://github.com/erigontech/erigon/actions/runs/28674562917/job/85045122311

…nt data race on shutdown

Co-authored-by: Giulio Rebuffo <giulio.rebuffo@gmail.com>
@AskAlexSharov
AskAlexSharov enabled auto-merge July 5, 2026 06:26
@AskAlexSharov
AskAlexSharov added this pull request to the merge queue Jul 5, 2026
Merged via the queue into main with commit deca419 Jul 5, 2026
92 checks passed
@AskAlexSharov
AskAlexSharov deleted the agent-fix/race-mergeloop-bgcomponentseg branch July 5, 2026 07:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant