cli: go test -race ./internal/cli has 14 data races on main (testenv.Zone vs time.Now, syncAgentsOnChange, three tests' stubs)
go test -race -count=1 ./internal/cli fails at origin/main 7300be4d0 (run on mica, 14 DATA RACE reports). CI doesn't run this package under -race, so nothing shows it. Seen while reviewing #7017. The same reds appear at that stack's head, which doesn't touch these files.
Tests that fail: TestAccountsImport, TestPluginOffAndRemovePutTheAgentBack, TestGoneAgentDefault, TestServeFollowDrainIsBounded, TestSessionsCmd, TestSessionsMoreCountsTheWholeSet, TestSessionsRead, TestSessionsSearch, TestSessionsShow, TestSessionsSearchFlags, TestSignalSavesTheSessionIndex.
Race sites (top frames):
testenv.Zoneandtime.Now().testenv.Zone(internal/testenv/testenv.go:305) writestime.Localin its cleanup, while a gateway goroutine still callstime.Now()(gateway.(*Server).releaseIfResting, idle_memory.go:66). This one is behind the Sessions* tests: the zone is swapped back while a server the test started is still running.syncAgentsOnChange.cli.syncAgentsOnChange(main.go:288, fromrun) writes a package var thatcatalog.Touched(live.go:536) reads on another goroutine. Seen in TestPluginOffAndRemovePutTheAgentBack and TestGoneAgentDefault. This may be a real race outside tests too: a secondrunin one process re-registers it while the catalog's goroutine reads it.- TestAccountsImport.func1. accounts_import_cli_test.go:39 is a handler writing a captured variable from concurrent requests.
- TestServeFollowDrainIsBounded. serve_follow_test.go:109 writes from
(*serveFollow).check(serve_follow.go:122, started byanswerNow) a value the test reads at :114 with no sync. - TestSignalSavesTheSessionIndex. The test swapped
sessionsSaved/saveOnSignalbetween signals while the handler goroutine read them. Fixed in the #7017 stack (its last commit).
To reproduce: go test -race -count=1 -timeout 30m ./internal/cli (about 140s on mica).
针对报告第 3、4 项的测试侧候选修复,基于 dc507aefd3f8d50225628a4e2f2e4eda138c9e7c,按 docs/contributors.md 在 issue 提供完整补丁。只修复这两处,不表示 #7103 已全部解决。
TestAccountsImport的假 HTTP 服务会并发处理请求,asked所有访问改为 atomic,保留未确认导入不得发请求的断言。TestServeFollowDrainIsBounded等待异步 stop 回调关闭 channel 后才读取stops,保留次数、强制重启、排空界限断言;3 秒失败上限不变,不再轮询未同步变量。- 没有生产代码、凭据逻辑、依赖或文档契约变化。catalog 回调、时区/服务生命周期和 signal 项均未修改。
验证
Linux amd64,Go 1.26.8,临时 HOME,固定隔离 Go 缓存,仅本地测试假服务。
原代码运行 go test -p 1 -race -tags nogui ./internal/cli -run '^(TestAccountsImport|TestServeFollowDrainIsBounded)$' -count=10,真实失败:
WARNING: DATA RACE
TestAccountsImport.func1(): accounts_import_cli_test.go:39
Read ... Previous write ... (两个 HTTP handler)
--- FAIL: TestAccountsImport
WARNING: DATA RACE
TestServeFollowDrainIsBounded.func4(): serve_follow_test.go:109
Previous read: TestServeFollowDrainIsBounded(): serve_follow_test.go:114
--- FAIL: TestServeFollowDrainIsBounded
FAIL github.com/yetone/magpie/internal/cli 0.460s
修复后 go test -p 1 -race -tags nogui ./internal/cli -run '^(TestAccountsImport|TestServeFollow.*)$' -count=20 通过(1.694s,包含相邻重启测试);go vet -p 1 -tags nogui ./internal/cli 通过;改动文件 gofmt、git diff --check 干净。独立只读审查未发现阻断问题。
未运行整个 CLI/仓库 suite、跨平台 build/vet、GUI 或真实账号验证。最初尝试因临时盘不足在编译阶段失败,迁移本任务编译缓存后重跑得到上述实际红绿结果;编译失败未算作复现。未创建 PR、未合入。
根因报告来自 @yetone;采用时请保留报告者与补丁作者 credit(报告者 Co-authored-by: yetone <[email protected]>)。AI 辅助实现与验证。
diff --git a/internal/cli/accounts_import_cli_test.go b/internal/cli/accounts_import_cli_test.go
index cf9a5f1..1f503e5 100644
--- a/internal/cli/accounts_import_cli_test.go
+++ b/internal/cli/accounts_import_cli_test.go
@@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"strings"
+ "sync/atomic"
"testing"
"time"
@@ -32,11 +33,11 @@ func TestAccountsImport(t *testing.T) {
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
t.Setenv("CLAUDE_CONFIG_DIR", filepath.Join(home, ".claude"))
t.Setenv("CODEX_HOME", filepath.Join(home, ".codex"))
- asked := 0
+ var asked atomic.Int32
fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var body map[string]string
json.NewDecoder(r.Body).Decode(&body)
- asked++
+ asked.Add(1)
who := strings.TrimPrefix(body["refresh_token"], "r-")
if who == body["refresh_token"] {
w.WriteHeader(400)
@@ -85,8 +86,8 @@ func TestAccountsImport(t *testing.T) {
// not agreed to: the takeover said, nothing asked of ChatGPT
out, err := run("codex", a, b)
- if err == nil || !strings.Contains(err.Error(), "canceled") || asked != 0 || len(provider.Logins("codex")) != 0 {
- t.Fatalf("imported without --yes: %v %d\n%s", err, asked, out)
+ if err == nil || !strings.Contains(err.Error(), "canceled") || asked.Load() != 0 || len(provider.Logins("codex")) != 0 {
+ t.Fatalf("imported without --yes: %v %d\n%s", err, asked.Load(), out)
}
if !strings.Contains(out, "signed out of that account") || !strings.Contains(out, "only read") {
t.Fatalf("the takeover wasn't said:\n%s", out)
diff --git a/internal/cli/serve_follow_test.go b/internal/cli/serve_follow_test.go
index 8c9296a..92bd42b 100644
--- a/internal/cli/serve_follow_test.go
+++ b/internal/cli/serve_follow_test.go
@@ -106,13 +106,17 @@ func TestServeFollowDrainIsBounded(t *testing.T) {
if g.check(time.Now().Add(24 * time.Hour)) {
t.Fatal("an unasked follow cut a request")
}
- h := &serveFollow{exe: exe, started: st, busy: busy, released: true, stop: func() { stops++ }}
+ // answerNow stops on another goroutine; observe its counter only after
+ // the callback is done (#7103).
+ stopped := make(chan struct{})
+ h := &serveFollow{exe: exe, started: st, busy: busy, released: true, stop: func() { stops++; close(stopped) }}
if a := h.answerNow(""); a.State != "restarting" || !a.Forced {
t.Fatalf("forced: %+v", a)
}
- eventually := time.Now().Add(3 * time.Second)
- for stops < 2 && time.Now().Before(eventually) {
- time.Sleep(5 * time.Millisecond)
+ select {
+ case <-stopped:
+ case <-time.After(3 * time.Second):
+ t.Fatal("--force did not finish stopping serve")
}
if stops != 2 {
t.Fatal("--force didn't stop serve while busy")