Skip to content

Fixed go test -race -bench=. . when running in parallel systems to prevent data races - #429

Merged
janisz merged 1 commit into
allegro:mainfrom
Aniruddhraam:test-fix/race-condition-fix
Aug 3, 2026
Merged

Fixed go test -race -bench=. . when running in parallel systems to prevent data races#429
janisz merged 1 commit into
allegro:mainfrom
Aniruddhraam:test-fix/race-condition-fix

Conversation

@Aniruddhraam

Copy link
Copy Markdown
Contributor

Fix data races in benchmark test suite when running with -race

Summary

Fixes data races flagged when running go test -race -bench=. in bigcache_bench_test.go.

Root Cause & Changes

  1. Moved b.ReportAllocs() outside b.RunParallel

    • b.ReportAllocs() mutates non-thread-safe internal fields on *testing.B.
    • Calling b.ReportAllocs() inside b.RunParallel(...) caused concurrent write data races on *testing.B across parallel worker goroutines.
    • Fix: Relocated b.ReportAllocs() to the benchmark setup phase outside b.RunParallel across writeToCache, appendToCache, readFromCache, readFromCacheNonExistentKeys, and BenchmarkIterateOverCache.
  2. Isolated EntryInfoIterator instances per worker goroutine

    • EntryInfoIterator is a stateful cursor. While SetNext() locks internal state during element advancement, it releases the lock before returning, allowing Value() to read state without holding the lock.
    • Sharing a single Iterator instance across multiple parallel goroutines inside b.RunParallel caused data races between concurrent SetNext() writes and Value() reads.
    • Fix: Moved it := cache.Iterator() inside the b.RunParallel loop so each worker goroutine operates on its own iterator instance, and added a documentation comment explaining iterator thread isolation.

Verification

Ran benchmarks with the Go race detector enabled:

go test -race -bench=. .

…cs() statistics to instantiate once per test
@Aniruddhraam Aniruddhraam changed the title Added an iterator individually for each test goroutine and ReportAllo… Fixed go test -race -bench=. . when running in parallel systems to prevent data races Aug 3, 2026
@Aniruddhraam Aniruddhraam changed the title Fixed go test -race -bench=. . when running in parallel systems to prevent data races Fixed go test -race -bench=. . when running in parallel systems to prevent data races Aug 3, 2026

@cristaloleg cristaloleg left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM

@janisz
janisz merged commit 838a81b into allegro:main Aug 3, 2026
2 checks passed
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.

3 participants