Thanks to visit codestin.com
Credit goes to github.com

Skip to content

flake: TestAIGatewayKeysTableConstraints (race on shared err) #25979

Description

@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_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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions