fix(coderd/rbac): guard builtInRoles with atomic.Pointer - #26384
Merged
Conversation
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
marked this pull request as ready for review
June 15, 2026 17:21
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes coder/internal#1575.
coderd/rbac.builtInRoleswas a package-levelmapthatReloadBuiltinRolesoverwrites on everycoderd.New. Production callsReloadonce at init, so the race was latent. Enterprise tests start many coderds in parallel while live handlers callRoleByName, sotest-go-race-pgstarted failing with a write-vs-read race after #25994 added a newRoleByNamelookup insidepatchOrganization.atomic.Pointer[builtInRoleMap].ReloadBuiltinRolesbuilds a local map and publishes it withStore. Readers go throughloadBuiltinRoles()and see a consistent snapshot.atomic.Boolpattern already used forminimumImplicitMemberin the same package.Repro of the originally racing trio is now clean:
go test -race ./coderd/rbac/...is also clean.Coder Agents on behalf of @Emyrk.