Seen on PR #25928 in test-go-race-pg:
https://github.com/coder/coder/actions/runs/26822469196/job/79080628504?pr=25928
Failing subtests:
TestAIGatewayKeysTableConstraints/name_with_underscore
TestAIGatewayKeysTableConstraints/name_longer_than_64_characters
TestAIGatewayKeysTableConstraints/duplicate_name
Root cause
err is declared once in the outer test function at coderd/database/querier_test.go:14749 (_, err := db.InsertAIGatewayKey(ctx, preExsiting)) and then reassigned with = inside each parallel subtest at line 14831:
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitShort)
_, err = db.InsertAIGatewayKey(ctx, tc.params) // shared `err` from outer scope
require.Error(t, err)
requireAIGatewayKeysViolation(t, err, tc.expectUniqueErr, tc.expectCheckErr)
})
}
All parallel subtests share the outer err. The race detector report points exactly at this:
WARNING: DATA RACE
Write at 0x00c000dc2ad0 by goroutine 855:
...TestAIGatewayKeysTableConstraints.func1()
coderd/database/querier_test.go:14831 +0x144
Previous read at 0x00c000dc2ad0 by goroutine 846:
...TestAIGatewayKeysTableConstraints.func1()
coderd/database/querier_test.go:14833 +0x1c7
Fix
Change _, err = ... to _, err := ... on line 14831 so each subtest gets its own err.
Provenance
Introduced by 32aee9ea4c feat: add DB queries for ai_gateway_coderd_keys (#25564).
Filed by Coder Agents on behalf of @Emyrk.
Seen on PR #25928 in
test-go-race-pg:https://github.com/coder/coder/actions/runs/26822469196/job/79080628504?pr=25928
Failing subtests:
TestAIGatewayKeysTableConstraints/name_with_underscoreTestAIGatewayKeysTableConstraints/name_longer_than_64_charactersTestAIGatewayKeysTableConstraints/duplicate_nameRoot cause
erris declared once in the outer test function atcoderd/database/querier_test.go:14749(_, err := db.InsertAIGatewayKey(ctx, preExsiting)) and then reassigned with=inside each parallel subtest at line 14831:All parallel subtests share the outer
err. The race detector report points exactly at this:Fix
Change
_, err = ...to_, err := ...on line 14831 so each subtest gets its ownerr.Provenance
Introduced by 32aee9ea4c feat: add DB queries for ai_gateway_coderd_keys (#25564).
Filed by Coder Agents on behalf of @Emyrk.