From a75de4c21ac28be98af8334643077d8b0bf47df7 Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 13 Oct 2026 18:44:49 +0800 Subject: [PATCH] gateway tests: TestKeptLoginStandsAtItsPlace counts the replies its fake Codex gave, under a lock The macOS race job of run 38380155723 (5f3a49254) failed with a DATA RACE at keep_login_as_test.go:22: two handler goroutines of the test's fake Codex appended to `tried` at once. Logging the fake's requests shows why: besides the /responses calls the test makes, the gateway reads /backend-api/wham/usage for each account behind a reply, on a goroutine of its own, and that read reached the same handler and was also counted. The handler now records only /responses calls, under a mutex, and the checks read a copy. The usage read can no longer race the record, nor land in the list the test compares with "acct-1". Run: go test -tags nogui -race -count=30 -run TestKeptLoginStandsAtItsPlace ./internal/gateway (ok); go vet; gofmt -l. Not reproduced locally on the old test (-race -count=60 -cpu 1,4 stayed clean: the usage read rarely overlaps a reply here); the CI stacks are the evidence. --- internal/gateway/keep_login_as_test.go | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/internal/gateway/keep_login_as_test.go b/internal/gateway/keep_login_as_test.go index ff70e4f68..93104cf70 100644 --- a/internal/gateway/keep_login_as_test.go +++ b/internal/gateway/keep_login_as_test.go @@ -5,6 +5,7 @@ import ( "net/http" "net/http/httptest" "strings" + "sync" "testing" "github.com/yetone/magpie/internal/provider" @@ -16,10 +17,20 @@ import ( // and the one Codex is signed in to at its own place behind it. func TestKeptLoginStandsAtItsPlace(t *testing.T) { codexSignedIn(t, "spare@example.com") - var tried []string + // a usage read the gateway starts behind a reply reaches the same server + // on a goroutine of its own, so only the replies are counted, under mu + var ( + mu sync.Mutex + tried []string + ) + replies := func() []string { mu.Lock(); defer mu.Unlock(); return append([]string(nil), tried...) } up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { io.ReadAll(r.Body) - tried = append(tried, r.Header.Get("chatgpt-account-id")) + if strings.HasSuffix(r.URL.Path, "/responses") { + mu.Lock() + tried = append(tried, r.Header.Get("chatgpt-account-id")) + mu.Unlock() + } io.WriteString(w, sse( `data: {"type":"response.created","response":{"id":"r1","model":"gpt-5.5"}}`, `data: {"type":"response.output_text.delta","delta":"pong"}`, @@ -41,8 +52,8 @@ func TestKeptLoginStandsAtItsPlace(t *testing.T) { } } code, body := codexPost(t, `{"model":"gpt-5.5","stream":true,"input":"ping"}`) - if code != 200 || strings.Join(tried, ",") != "acct-1" { - t.Fatalf("%d %s: tried %v, not the first (acct-1)", code, body, tried) + if code != 200 || strings.Join(replies(), ",") != "acct-1" { + t.Fatalf("%d %s: tried %v, not the first (acct-1)", code, body, replies()) } if got := provider.InUseLogin("codex"); got != "me@example.com" { t.Fatalf("in use %q", got) @@ -60,9 +71,11 @@ func TestKeptLoginStandsAtItsPlace(t *testing.T) { t.Fatalf("let go, signed in as %+v", l) } } + mu.Lock() tried = nil + mu.Unlock() codexPost(t, `{"model":"gpt-5.5","stream":true,"input":"ping"}`) - if strings.Join(tried, ",") != "acct-1" { - t.Fatalf("let go, tried %v", tried) + if strings.Join(replies(), ",") != "acct-1" { + t.Fatalf("let go, tried %v", replies()) } }