go runtime_error ai_generated true

panic: race detected during execution of test

ID: go/race-detector-in-test

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-10-11First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.1+ active

Root Cause

A test running under -race spawns goroutines that share state without synchronization, and the race detector aborts the test binary.

generic

中文

在 -race 下运行的测试启动了共享状态且无同步的 goroutine,竞争检测器中止了测试二进制。

Workarounds

  1. 92% success
    var wg sync.WaitGroup
    wg.Add(1)
    go func() { defer wg.Done(); mutateShared() }()
    wg.Wait()
  2. 90% success
    go test -race -run TestFoo ./...
    // read the two conflicting stacks and add a mutex around both

Dead Ends

Common approaches that don't work:

  1. 80% fail

    Shuffling changes timing but the unsynchronized access remains and the race recurs.

  2. 75% fail

    The race is inside the test's own goroutines, not between tests, so serializing tests does not help.