Repository navigation
fix(coderd): let only one request create the first user (#32417) - #32536
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, no findings, COMMENT. Review Finding inventoryFindings
Contested and acknowledgedNone. Round logRound 1Full panel (round 1). Netero: no findings, 2 Notes; backport verified line-identical to #32417. Law not run (effective +579 < 1000). Panel: ging-go, knuckle, kurapika, ryosuke, takumi, pariston, mafuuu, bisky, hisoka, komugi, gon, leorio, wildcard zoro (random from distilled set). Multi-domain size target (security-sensitive first-user creation); 12 plus wildcard. No in-scope P0 to P2 findings after cross-check. Gon's five P2 comment-padding items downgraded to Nit (CRF-5): keep-argument was the AGENTS.md rule that comments be substantive; the comments are accurate, cause no defect, and editing them on a release branch would diverge from #32417. Requester asked for P0 to P2 only, so P3 and below are recorded and not posted. Reviewed against e0bab67..570e7c5. About deep-reviewCRF = Coder Review Finding (P0-P4, Nit, Note)
|
There was a problem hiding this comment.
This backport makes postFirstUser and the OAuth sign-up path take LockIDFirstUserSetup and recount users at read committed before creating the owner. The added lines match #32417, and the panel found no P0, P1, or P2 issues. P3 and lower items were recorded but are not posted, as requested.
Out of scope (needs a ticket or explicit acceptance by a human):
coderd/userauth.go:1897: the OAuth first-user telemetry snapshot is sent inside the transaction before the IdP sync calls, so a sync error rolls back the user after telemetry reported it.coderd/users.go:2232:CreateUserenqueues the account-created notification throughapi.NotificationsEnqueuer, not the caller'stx, so a later rollback inoauthLoginstill sends a notification for a user that does not exist.coderd/users.go:163:firstUserreturns "The initial user has already been created!" andpostFirstUserreturns the same text without "!", so clients see two wordings for one condition.
Notes:
TrialRequestedBeforeLockandNotTakenWhenUsersExistpass without the fix because they check where the lock is taken; the otherTestFirstUserSetupsubtests fail without it.- The
lockFirstUserSetuperror return inoauthLogin(coderd/userauth.go:1794) and theGetUserCounterror inlockFirstUserSetuphave no test coverage.
🤖 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.