Skip to content

fix(coderd/rbac): guard builtInRoles with atomic.Pointer - #26384

Merged
Emyrk merged 2 commits into
mainfrom
fix/rbac-builtin-roles-race
Jun 15, 2026
Merged

fix(coderd/rbac): guard builtInRoles with atomic.Pointer#26384
Emyrk merged 2 commits into
mainfrom
fix/rbac-builtin-roles-race

Conversation

@Emyrk

@Emyrk Emyrk commented Jun 15, 2026

Copy link
Copy Markdown
Member

Fixes coder/internal#1575.

coderd/rbac.builtInRoles was a package-level map that ReloadBuiltinRoles overwrites on every coderd.New. Production calls Reload once at init, so the race was latent. Enterprise tests start many coderds in parallel while live handlers call RoleByName, so test-go-race-pg started failing with a write-vs-read race after #25994 added a new RoleByName lookup inside patchOrganization.

  • Wrap the map in atomic.Pointer[builtInRoleMap].
  • ReloadBuiltinRoles builds a local map and publishes it with Store. Readers go through loadBuiltinRoles() and see a consistent snapshot.
  • Matches the existing atomic.Bool pattern already used for minimumImplicitMember in the same package.

Repro of the originally racing trio is now clean:

go test -race -count=1 -timeout=240s ./enterprise/coderd \
  -run "TestPatchOrganizationsByUser|TestUserOIDC/Refresh|TestTemplates/CleanupTTLs"
ok  github.com/coder/coder/v2/enterprise/coderd  2.572s

go test -race ./coderd/rbac/... is also clean.

Coder Agents on behalf of @Emyrk.

The built-in roles map is a package-level global that ReloadBuiltinRoles
overwrites whenever a coderd starts up. Production reads it once at init,
so the race was latent; enterprise tests spin many coderds in parallel
while live handlers call RoleByName, so test-go-race-pg started failing
with a write-vs-read race after PR #25994 added a new RoleByName lookup
inside patchOrganization.

Wrap the map in atomic.Pointer so readers see a consistent snapshot
without locking. ReloadBuiltinRoles builds the map locally, then
publishes it with Store. Matches the existing atomic.Bool pattern used
for minimumImplicitMember.

Fixes coder/internal#1575.
@Emyrk
Emyrk marked this pull request as ready for review June 15, 2026 17:21

@cstyan cstyan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Emyrk
Emyrk enabled auto-merge (squash) June 15, 2026 17:34
@Emyrk
Emyrk merged commit 195c545 into main Jun 15, 2026
53 of 55 checks passed
@Emyrk
Emyrk deleted the fix/rbac-builtin-roles-race branch June 15, 2026 18:11
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

flake: Data race in rbac roles - TestPatchOrganizationsByUser/TestUserOIDC/TestTemplates

2 participants