Repository navigation
fix(coderd): let only one request create the first user (#32417) - #32537
Conversation
Prevent multiple Owner accounts from being created during concurrent first-user setup requests. Refs https://linear.app/codercom/issue/CORE-853 Use a transaction-scoped PostgreSQL advisory lock shared by password setup and GitHub/OIDC first sign-ins. Recheck the user count under the lock to ensure only one account receives the Owner role, while preserving existing responses for subsequent requests. - Add a stable, backport-safe lock ID and shared first-user setup helper. - Apply locking only when no users exist, leaving normal sign-ins unaffected. - Use `READ COMMITTED` isolation to ensure accurate counts regardless of database defaults. - Keep validation and trial licence requests outside the lock. - Preserve existing API responses, status codes, and telemetry behavior. **Testing** - Add concurrent password, OAuth, and mixed-path tests verifying exactly one Owner. - Cover lock contention, cancellation, isolation levels, telemetry, and lock bypass. - Verify tests fail when locking is removed. - Pass targeted race tests, 124 related tests, lint, and `intxcheck`. **Known limitation:** A burst of concurrent OAuth sign-ins on an empty deployment could exhaust the database connection pool while waiting for the lock. This remains unverified and is outside the scope of this fix. **Note:** Stacked on #32324; will be rebased onto `main` after it merges. (cherry picked from commit cf68914)
|
/coder-agents-review Please limit the review to P0, P1, and P2 issues only. |
|
Chat: Review posted | View chat Review history
deep-review v0.13.0 | Round 1 | Last posted: Round 1, 5 findings (2 P3, 3 P4), COMMENT. Review Finding inventoryFindings
Contested and acknowledgedNone. Round logRound 1Netero first pass: no P0, 2 Notes. Full panel (round 1): ging-go, knuckle, kurapika, takumi, ryosuke, killua (domain specialists; Killua triggered by the connection-pool limitation stated in #32417), pariston, mafuuu (core), bisky, gon, leorio, hisoka (pool), chopper (wildcard, random from distilled set). Size 12 + wildcard; multi-domain target 9-12 because the change is auth-sensitive (owner creation on an unauthenticated endpoint). Netero verified the backport diff equals #32417 except the two documented differences. Cross-check: trial-license finding raised by 8 reviewers at Note to P3; higher severity (Pariston P3) kept. Takumi's connection-pool deadlock was listed as out of scope by Takumi; split into the in-PR consequence (lock waiters add connection holders, CRF-4, P3) and the root cause (CRF-23, OOS). Takumi reproduced at pool size 4 with 8 sign-ups: all 8 stalled at head, some on base. Drop/downgrade gate for style-only items (CRF-6, CRF-9, CRF-11 to CRF-17). Keep-argument: the lines are added by this diff and the no-follow-up rule says unfixed wording stays forever. Drop reason: none change behavior or test coverage, and this PR is a backport whose contract is identity with #32417; wording and refactor edits here make the release branch drift from main and make later cherry-picks into these files conflict. Gon's P2 ratings were downgraded to Nit before the drop because no item has a behavioral consequence. Behavior and coverage findings (CRF-3, CRF-4, CRF-5, CRF-7, CRF-8) were kept; the body notes that they apply to main as well. About deep-reviewCRF = Coder Review Finding (P0-P4, Nit, Note)
|
There was a problem hiding this comment.
This backport serializes first-user creation with an advisory lock in the password and OAuth paths, and its diff matches #32417 except the two differences the description lists. Findings: 2 P3, 3 P4. Each finding also applies to #32417 on main, so a fix belongs there first and then in this branch.
Out of scope (needs a ticket or explicit acceptance by a human):
coderd/users.go:2084:CreateUsercallsNotificationsEnqueuer.EnqueueWithDataonapi.Databasewhile the caller's transaction holds a pool connection, so concurrent sign-ups at or above--postgres-conn-max-openstall on the base commit too.coderd/userauth.go:1896: telemetry for the first OAuth user is reported inside the transaction, so after a rollback the report names an owner that does not exist and the next sign-up is reported as another first user.coderd/database/lock.go:24: the comment saysGenLockIDis FNV-1a 64, but it callsfnv.New64(), which is FNV-1, so an ID recomputed from the comment does not match.cli/server_createadminuser.go:199:coder server create-admin-usergrants owner withoutLockIDFirstUserSetup; it needs direct database access, so no remote caller reaches it.- PR description: the backporter template writes an em dash after "Original PR: #32417", which goes into the squash commit on the release branch.
Notes:
coderd/userauth.go:2068: every OAuth login transaction now runs at read committed, not only first-user sign-ups. On a server with the Postgres default this changes nothing.coderd/firstusersetup.go:27: "tx must be started with firstUserSetupTxOptions" is not checked. A new caller that omits the options on a repeatable read server would create a second owner; both current callers pass them, andServerDefaultRepeatableReadcovers both.coderd/userauth.go:1797: an OAuth sign-up that loses the race becomes a member or gets "Signups are disabled", with no message that names the race.- The CI check "Analyze PR for Documentation Updates Needed" failed. Its log could not be read from the review environment; the other 22 checks passed.
🤖 This review was automatically generated with Coder Agents.
Backport of #32417
Original PR: #32417 — fix(coderd): let only one request create the first user
Merge commit: cf68914
Requested by: @BobbyHo
The automatic cherry-pick had conflicts. They are resolved by hand; the fix is the same as in #32417.
Differences from #32417
coderd/database/lock.go: adds onlyLockIDFirstUserSetup. The conflict came from nearby chat lock IDs that are not on this branch.coderd/users.go: keeps this branch'sptr.Ref(codersdk.UserStatusActive)where fix(coderd): let only one request create the first user #32417 hasnew(codersdk.UserStatusActive).