Skip to content

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

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

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

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

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

    git cherry-pick -x -m1 cf68914
@coder-backporter-app coder-backporter-app Bot added the backport/v2.37 Backport PR targeting release/2.37 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: $11.67 / $100.00

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

deep-review v0.13.0 | Round 1 | fd4197f..33649ad

Last posted: Round 1, 5 findings (2 P3, 3 P4), COMMENT. Review

Finding inventory

Findings

# Sev Status Location Summary Round Reviewer Posted
CRF-1 Note Note (body) coderd/userauth.go:2068 Read committed now applies to every OAuth login transaction R1 Netero No
CRF-2 Note Note (body) CI "Analyze PR for Documentation Updates Needed" failed, log not readable R1 Netero No
CRF-3 P3 Open coderd/users.go:253 Race loser with trial: true inserts a trial license, then gets 409 R1 Pariston P3, Knuckle P4, Kurapika P4, Mafuuu P4, Takumi Note, Ryosuke Note, Bisky Note, Hisoka OOS Yes
CRF-4 P3 Open coderd/userauth.go:1793 Lock waiters hold pool connections while the holder's member sign-up needs another for notifications; burst stalls all DB requests R1 Takumi Yes
CRF-5 P4 Open coderd/firstusersetup_test.go:290 PasswordAndOAuth discards HTTP statuses, so a 500 on either path passes R1 Bisky, Chopper Yes
CRF-6 P4 Dropped by orchestrator (refactor-only test change; backport should match #32417, make it on main) coderd/firstusersetup_test.go:185 Concurrent fan-out loop written three times R1 Bisky No
CRF-7 P4 Open coderd/userauth.go:1799 OAuth race-loss warning has no account identity R1 Leorio, Chopper Yes
CRF-8 P4 Open coderd/users.go:284 Password race-loss warning has no account identity R1 Leorio Yes
CRF-9 P4 Dropped by orchestrator (idiom-only test change; backport should match #32417, make it on main) coderd/firstusersetup_test.go:137 sync.Once plus closure could be sync.OnceFunc R1 Ging-Go No
CRF-10 Note Dropped by orchestrator (asks no change; ptr.Ref matches the branch) coderd/users.go:273 ptr.Ref could be new() R1 Ging-Go No
CRF-11 Nit Dropped by orchestrator (wording-only; backport should match #32417, make it on main) coderd/firstusersetup.go:15 firstUserSetupTxOptions doc states the rule twice R1 Gon P2 No
CRF-12 Nit Dropped by orchestrator (wording-only; backport should match #32417, make it on main) coderd/userauth.go:1790 Third comment sentence repeats the second R1 Gon P2 No
CRF-13 Nit Dropped by orchestrator (wording-only; backport should match #32417, make it on main) coderd/firstusersetup_test.go:64 githubCallbackStatus doc repeats the name R1 Gon P2 No
CRF-14 Nit Dropped by orchestrator (wording-only; backport should match #32417, make it on main) coderd/firstusersetup_test.go:148 setDefaultRepeatableRead doc repeats the name R1 Gon P2 No
CRF-15 Nit Dropped by orchestrator (naming-only; backport should match #32417, make it on main) coderd/firstusersetup_test.go:227 wantUsers should be wantUserCount R1 Gon P2 No
CRF-16 Nit Dropped by orchestrator (wording-only; "via oidc" is pre-existing text; backport should match #32417) coderd/users.go:178 Comment says oidc, shared path is oauthLogin (GitHub too) R1 Gon P4 No
CRF-17 Nit Dropped by orchestrator (naming-only; backport should match #32417, make it on main) coderd/firstusersetup_test.go:229 wantOther should be wantOtherStatus R1 Gon No
CRF-18 Note Note (body) coderd/firstusersetup.go:27 "tx must be started with firstUserSetupTxOptions" is not checked R1 Ryosuke No
CRF-19 Note Note (body) coderd/userauth.go:1797 OAuth race loser becomes a member or gets "Signups are disabled", with no error naming the race R1 Mafuuu No
CRF-20 OOS Out of scope (body) coderd/database/lock.go:24 Comment says FNV-1a 64, GenLockID uses fnv.New64 (FNV-1) R1 Knuckle No
CRF-21 OOS Out of scope (body) cli/server_createadminuser.go:199 create-admin-user grants owner without the lock R1 Kurapika No
CRF-22 OOS Out of scope (body) coderd/userauth.go:1896 First OAuth user telemetry reported before commit R1 Takumi No
CRF-23 OOS Out of scope (body) coderd/users.go:2084 CreateUser enqueues notifications on api.Database while the caller's tx holds a connection R1 Takumi No
CRF-24 OOS Out of scope (body) PR description Backporter template inserts an em dash R1 Leorio No

Contested and acknowledged

None.

Round log

Round 1

Netero 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.

Reviewed against fd4197f..33649ad.

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 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: CreateUser calls NotificationsEnqueuer.EnqueueWithData on api.Database while the caller's transaction holds a pool connection, so concurrent sign-ups at or above --postgres-conn-max-open stall 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 says GenLockID is FNV-1a 64, but it calls fnv.New64(), which is FNV-1, so an ID recomputed from the comment does not match.
  • cli/server_createadminuser.go:199: coder server create-admin-user grants owner without LockIDFirstUserSetup; 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, and ServerDefaultRepeatableRead covers 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.

Comment thread coderd/users.go Resolved
Comment thread coderd/userauth.go Resolved
Comment thread coderd/firstusersetup_test.go Resolved
Comment thread coderd/userauth.go Resolved
Comment thread coderd/users.go Resolved
@BobbyHo
BobbyHo requested a review from denisra November 21, 2026 16:59
@BobbyHo
BobbyHo merged commit b5616a7 into release/2.37 Nov 23, 2026
37 of 38 checks passed
@BobbyHo
BobbyHo deleted the backport/32417-to-2.37 branch November 23, 2026 17:16

This branch had an error being deployed

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

Labels

backport/v2.37 Backport PR targeting release/2.37

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants