Skip to content

fix(coderd): let only one request create the first user (#32417) - #32536

Merged
BobbyHo merged 3 commits into
release/2.38from
backport/32417-to-2.38
Nov 23, 2026
Merged

BobbyHo merged 3 commits into
release/2.38from
backport/32417-to-2.38

Conversation

@coder-backporter-app

@coder-backporter-app coder-backporter-app Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

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 only LockIDFirstUserSetup. The conflict came from nearby chat lock IDs that are not on this branch.

The automatic cherry-pick of cf68914 to release/2.38 had conflicts.
Please cherry-pick manually:

    git cherry-pick -x -m1 cf68914
@coder-backporter-app coder-backporter-app Bot added the backport/v2.38 Backport PR targeting release/2.38 label Nov 20, 2026
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)
@BobbyHo BobbyHo changed the title fix(coderd): let only one request create the first user (#32417) (conflicts) fix(coderd): let only one request create the first user (#32417) Nov 21, 2026
@BobbyHo

BobbyHo commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator

/coder-agents-review Please limit the review to P0, P1, and P2 issues only.

@coder-agents-review

coder-agents-review Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Chat: Review posted | View chat
Requested: 2026-11-21 00:36 UTC by @BobbyHo
Spend: $9.79 / $100.00

Review history
  • R1 (2026-11-21): 13 reviewers, COMMENT. Review

deep-review v0.13.0 | Round 1 | e0bab67..570e7c5

Last posted: Round 1, no findings, COMMENT. Review

Finding inventory

Findings

# Sev Status Location Summary Round Reviewer Posted
CRF-1 Note Note (body) firstusersetup_test.go:426 TrialRequestedBeforeLock and NotTakenWhenUsersExist pass without the fix, by design R1 Netero No
CRF-2 Note Note (body) userauth.go:1794 lockFirstUserSetup error returns in oauthLogin and GetUserCount error have no coverage R1 Netero No
CRF-3 P3 Not posted (requester limited review to P0-P2) users.go:256 Losing Trial:true request inserts a trial license, then returns 409 R1 Mafuuu P3, Hisoka P3, Knuckle Note, Pariston Note, Kurapika OOS, Takumi OOS No
CRF-4 P3 Not posted (requester limited review to P0-P2) firstusersetup_test.go:291 PasswordAndOAuth discards response statuses, so 500s for losers pass R1 Bisky No
CRF-5 Nit Not posted (requester limited review to P0-P2) userauth.go:1790 Comment clauses restate adjacent code (5 sites: userauth.go:1790, users.go:257, firstusersetup_test.go:148, :283, :341) R1 Gon P2 No
CRF-6 P3 Not posted (requester limited review to P0-P2) users.go:179 Edited comment says "via oidc"; oauthLogin also serves GitHub R1 Gon No
CRF-7 Nit Not posted (requester limited review to P0-P2) firstusersetup_test.go:230 wantOther should be wantOtherStatus R1 Gon No
CRF-8 Nit Not posted (requester limited review to P0-P2) firstusersetup_test.go:137 sync.Once plus closure could be sync.OnceFunc R1 Ging-Go No
CRF-9 Note Not posted (requester limited review to P0-P2) firstusersetup.go:13 errFirstUserExists repeats the 409 message text in a third literal R1 Gon No
CRF-10 P4 Not posted (requester limited review to P0-P2) firstusersetup_test.go:155 setDefaultRepeatableRead never resets ALTER DATABASE; leaks when CODER_PG_CONNECTION_URL reuses a DB R1 Hisoka Note No
CRF-11 Note Not posted (requester limited review to P0-P2) userauth.go:2067 All OAuth login transactions now run at read committed regardless of server default R1 Zoro No
CRF-12 P4 Not posted (requester limited review to P0-P2) lock.go:37 PR body "Differences" bullet describes no actual difference from #32417 R1 Leorio No
CRF-13 OOS Out of scope (body) userauth.go:1897 OAuth first-user telemetry is sent inside the tx before IdP sync can roll it back R1 Hisoka No
CRF-14 OOS Out of scope (body) users.go:2232 CreateUser enqueues the account-created notification outside the caller's tx R1 Mafuuu No
CRF-15 OOS Dropped by orchestrator (httpapi.Read validates username before TrialGenerator, so the invalid-username path is rejected first) users.go:224 Trial license installed before CreateUser validates username R1 Mafuuu No
CRF-16 OOS Out of scope (body) users.go:163 firstUser and postFirstUser 409 messages differ by "!" R1 Gon No

Contested and acknowledged

None.

Round log

Round 1

Full 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-review

CRF = Coder Review Finding (P0-P4, Nit, Note)

Reviewer Focus
Bisky tests
Chopper ops/errors
Churn-guard change verification
Ging language modernization
Gon naming
Hisoka edge cases
Killua perf
Kite change integrity
Knov contracts
Knuckle SQL
Komugi flake/determinism
Kurapika security
Law decomposition
Leorio docs
Luffy product
Mafu-san process
Mafuuu contracts
Melody dispatch/pairing
Meruem structural
Nami frontend
Netero mechanical checks
Pariston premise testing
Pen-botter product gaps
Razor verification
Robin duplication
Ryosuke Go arch
Takumi concurrency
Zoro shape

🤖 Managed by Coder Agents.

@coder-agents-review coder-agents-review Bot 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.

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: CreateUser enqueues the account-created notification through api.NotificationsEnqueuer, not the caller's tx, so a later rollback in oauthLogin still sends a notification for a user that does not exist.
  • coderd/users.go:163: firstUser returns "The initial user has already been created!" and postFirstUser returns the same text without "!", so clients see two wordings for one condition.

Notes:

  • TrialRequestedBeforeLock and NotTakenWhenUsersExist pass without the fix because they check where the lock is taken; the other TestFirstUserSetup subtests fail without it.
  • The lockFirstUserSetup error return in oauthLogin (coderd/userauth.go:1794) and the GetUserCount error in lockFirstUserSetup have no test coverage.

🤖 This review was automatically generated with Coder Agents.

@BobbyHo
BobbyHo requested a review from denisra November 21, 2026 02:44
@BobbyHo
BobbyHo merged commit 56985b9 into release/2.38 Nov 23, 2026
38 of 39 checks passed
@BobbyHo
BobbyHo deleted the backport/32417-to-2.38 branch November 23, 2026 17:16

This branch had an error being deployed

1 failed deployment
docs-preview — 570e7c57 Deployed Nov 23, 2026 by BobbyHo via cleanup #353
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport/v2.38 Backport PR targeting release/2.38

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants