Skip to content

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

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

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

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 because #32324 was not on this branch yet. With #32324 backported, the cherry-pick applies cleanly and the change is the same as in #32417.

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

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

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

deep-review v0.13.0 | Round 1 | dbab9ed..2656317

Last posted: Round 1, 8 findings (1 P3, 4 P4, 3 Nit), COMMENT. Review

Finding inventory

Findings

# Sev Status Location Summary Round Reviewer Posted
CRF-1 P3 Open +coderd/userauth.go:1792 Lock waiters hold pool connections; a waiter that creates a member needs a second connection for notifications, so a sign-up burst at first-user time can exhaust the pool R1 Komugi Yes
CRF-2 P4 Open coderd/users.go:224 Losing Trial: true first-user request stores a trial license, then gets 409 R1 Mafuuu P3, Ryosuke P4, Pariston P4 (Bisky, Hisoka, Zoro listed OOS) Yes
CRF-3 P4 Open +coderd/firstusersetup_test.go:170 requireLockWaiters counts all ungranted advisory locks, not only LockIDFirstUserSetup R1 Bisky, Komugi Yes
CRF-4 P4 Open +coderd/users.go:179 postFirstUser comment says "oidc" flow; the path is oauthLogin (GitHub and OIDC) R1 Gon P2 Yes
CRF-5 P4 Open +coderd/firstusersetup_test.go:229 wantOther comment says "but the first"; excluded sign-in is the one that creates the owner R1 Gon P2 Yes
CRF-6 Nit Open +coderd/firstusersetup_test.go:137 Use sync.OnceFunc instead of sync.Once plus closure R1 Ging-Go Yes
CRF-7 Nit Open +coderd/firstusersetup_test.go:279 githubSignupConfig takes a bare boolean at call sites R1 Gon Yes
CRF-8 Nit Open +coderd/firstusersetup_test.go:334 Subtest comment repeats the reason documented on firstUserSetupTxOptions R1 Gon P2 Yes
CRF-9 P2 Dropped by orchestrator (Go helper doc comment; states database-wide scope) +coderd/firstusersetup_test.go:148 setDefaultRepeatableRead doc repeats the body R1 Gon No
CRF-10 P2 Dropped by orchestrator (struct field doc adds when the count is taken) +coderd/firstusersetup_test.go:227 wantUsers comment repeats the field name R1 Gon No
CRF-11 P2 Dropped by orchestrator (struct field doc; standard Go practice, no wrong claim) +coderd/firstusersetup_test.go:341 request field comment repeats the type R1 Gon No
CRF-12 Note Note (body) +coderd/firstusersetup_test.go:276 PasswordAndOAuth ignores statuses; passes when requests return 500 R1 Bisky No
CRF-13 Note Note (body) coderd/firstusersetup.go:1 Cherry-picked commit body says "Stacked on #32324; will be rebased onto main", false on release/2.39 R1 Leorio No
CRF-14 Note Note (body) +coderd/userauth.go:1791 Two concurrent callbacks for the same new email: second returns an error, no second owner R1 Takumi No
CRF-15 OOS Out of scope (body) coderd/users.go:302 RefreshEntitlements failure returns without writing a response R1 Ryosuke No
CRF-16 OOS Out of scope (body) enterprise/coderd/scim/users.go:160 SCIM creates a member at user count 0 without the lock; deployment left with no owner R1 Ryosuke No
CRF-17 OOS Out of scope (body) coderd/users.go:2187 CreateUser enqueues notifications on the pool while caller transaction is open R1 Komugi No
CRF-18 OOS Out of scope (body) enterprise/trialer/trialer.go:121 Trial InsertLicense runs outside the first-user transaction R1 Mafuuu, Bisky, Hisoka, Zoro No
CRF-19 OOS Out of scope (body) coderd/users.go:316 Unchanged comment contains an em dash R1 Netero No

Contested and acknowledged

None.

Drop and downgrade decisions

  • CRF-1 kept at P3. Keep-argument: an unauthenticated burst at first-user time can stall every query on the replica until clients disconnect. Downgrade argument considered: needs signups enabled, an empty database, and more concurrent sign-ups than the pool size, and the same two-connection pattern exists for any concurrent user creation. The consequence sets severity; kept.
  • CRF-2 P3 (Mafuuu) to P4. Keep-argument for P3: the licensor records a trial for a person who never got an account. Downgrade reason: the PR adds no licensor call; before the PR the same request also stored a license and created a second owner; multiple licenses are supported.
  • CRF-4 P2 to P4. Keep-argument: a reader can miss the GitHub path. Downgrade reason: the new second sentence names lockFirstUserSetup, which grep resolves to both callers.
  • CRF-5 P2 to P4. Keep-argument: "first" implies send order the test lacks. Downgrade reason: wording only; the assertions are correct.
  • CRF-8 P2 to Nit. Keep-argument: project rule to keep each fact in one place. Downgrade reason: convention issue, code works.
  • CRF-9, CRF-10, CRF-11 dropped. Keep-argument: AGENTS.md requires substantive comments. Drop reason: each is a one-line doc on a helper or struct field, standard in Go, makes no wrong claim, and on a byte-identical backport the edit would diverge from main for no behavior change.

Round log

Round 1

Full panel (round 1). Netero: no findings. Law not run (effective +579). Panel: ging-go, takumi, knuckle, kurapika, ryosuke (specialists); pariston, mafuuu (core); bisky, komugi, gon, leorio, hisoka (pool); zoro (wildcard). Size 12 + wildcard; security-sensitive first-user creation path. Backport diff verified identical to upstream cf68914. 1 P3, 4 P4, 3 Nit, 3 Note (body), 5 out of scope, 3 dropped. Reviewed against dbab9ed..2656317.

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 in postFirstUser and oauthLogin with a transaction-scoped advisory lock and a user count taken under it at read committed. Findings: 1 P3, 4 P4, 3 Nit. The +/- lines are identical to upstream cf68914, so every change made for these findings also needs to go to main.

Out of scope (needs a ticket or explicit acceptance by a human):

  • coderd/users.go:302: when RefreshEntitlements fails after the first user is created, the handler returns without writing a response, so the client gets an empty 200 although the owner exists.
  • enterprise/coderd/scim/users.go:160: SCIM can create a member while the user count is 0 without taking LockIDFirstUserSetup; after that /users/first returns 409 and the deployment has no owner.
  • coderd/users.go:2187: CreateUser enqueues account-created notifications through a pool connection while the caller's transaction is open, so every transactional caller needs two connections and the notification is sent before the user row commits.
  • enterprise/trialer/trialer.go:121: InsertLicense runs outside the first-user transaction, so a first-user request that is later refused keeps its trial license.
  • coderd/users.go:316: this unchanged comment contains an em dash, which make lint/emdash rejects.

Notes:

  • TestFirstUserSetup_PasswordAndOAuth discards every response status and asserts only that one owner exists, so it passes when some requests return 500.
  • The cherry-picked commit body ends with "Stacked on #32324; will be rebased onto main after it merges.", which is false on release/2.39. A squash merge replaces it with the PR description; delete it before a merge commit or rebase merge.
  • Two concurrent OAuth callbacks for the same new email at user count 0 create one owner; the second callback returns 403 (signups disabled) or fails on the duplicate email (signups enabled), and a retry signs in as the owner.

coderd/users.go:224

P4 [CRF-2] A POST /api/v2/users/first request with Trial: true that loses the race stores a trial license and then gets 409. (Mafuuu, Ryosuke, Pariston)

TrialGenerator runs here, before the lock, and in enterprise trialer.Generate calls the licensor and then InsertLicense (enterprise/trialer/trialer.go:121) outside this transaction. The deployment ends up with one owner and an extra trial license registered to the contact details of the refused request. Keeping the licensor call outside the lock is correct, as TrialRequestedBeforeLock requires. If the extra license is acceptable, state that in the comment on TrialRequestedBeforeLock; otherwise this needs a human decision on the trialer change listed in the review body.

🤖

🤖 This review was automatically generated with Coder Agents.

Comment thread coderd/userauth.go Resolved
Comment thread coderd/firstusersetup_test.go Resolved
Comment thread coderd/users.go Resolved
Comment thread coderd/firstusersetup_test.go Resolved
Comment thread coderd/firstusersetup_test.go Resolved
Comment thread coderd/firstusersetup_test.go Resolved
Comment thread coderd/firstusersetup_test.go Resolved
@BobbyHo
BobbyHo requested a review from denisra November 21, 2026 02:43
@BobbyHo
BobbyHo merged commit 03538ce into release/2.39 Nov 23, 2026
39 checks passed
@BobbyHo
BobbyHo deleted the backport/32417-to-2.39 branch November 23, 2026 17:18

This branch had an error being deployed

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

Labels

backport/v2.39 Backport PR targeting release/2.39

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants