go runtime_error ai_generated true

panic: 执行测试期间检测到竞争

panic: race detected during execution of test

ID: go/race-detector-in-test

其他格式: JSON · Markdown 中文 · English
80%修复率
88%置信度
0证据数
2024-10-11首次发现

版本兼容性

版本状态引入弃用备注
1.1+ active

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 80% 失败

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

  2. 75% 失败

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