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.
66 lines
2.4 KiB
Go
66 lines
2.4 KiB
Go
package database
|
|
|
|
import (
|
|
"hash/fnv"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// Well-known lock IDs for lock functions in the database. These should not
|
|
// change. If locks are deprecated, they should be kept in this list to avoid
|
|
// reusing the same ID.
|
|
const (
|
|
LockIDDeploymentSetup = iota + 1
|
|
LockIDEnterpriseDeploymentSetup
|
|
LockIDDBRollup
|
|
LockIDDBPurge
|
|
LockIDNotificationsReportGenerator
|
|
LockIDCryptoKeyRotation
|
|
LockIDReconcilePrebuilds
|
|
LockIDReconcileSystemRoles
|
|
LockIDBoundaryUsageStats
|
|
// Deprecated: Reserved to prevent reuse. Do not use at runtime.
|
|
LockIDAIProvidersEnvSeed
|
|
// Deprecated: Reserved to prevent reuse. Do not use at runtime.
|
|
LockIDChatModelConfigWrites
|
|
LockIDChatCapacityAdmission
|
|
LockIDNotifyUnpricedAIModels
|
|
)
|
|
|
|
// Per-setting advisory lock IDs for the chat instruction settings. These
|
|
// derive from the exact site_configs key with GenLockID (FNV-1a 64) instead
|
|
// of the sequential LockID* block above, so writers of different settings
|
|
// never contend and the IDs cannot collide with any sequentially allocated
|
|
// lock ID (different derivation space) or with another subsystem's
|
|
// GenLockID output (the key strings are unique to these settings).
|
|
var (
|
|
LockIDChatInstructionSystemPrompt = GenLockID("agents_chat_system_prompt")
|
|
LockIDChatInstructionPlanMode = GenLockID("agents_chat_plan_mode_instructions")
|
|
)
|
|
|
|
// LockIDFirstUserSetup serializes creation of the first user. It is
|
|
// derived from a string because the sequential list above has a
|
|
// different length on each release branch, and this lock must have the
|
|
// same value on all of them.
|
|
var LockIDFirstUserSetup = GenLockID("first_user_setup")
|
|
|
|
// LockIDChatOrganizationSystemPrompt returns the advisory lock ID that
|
|
// serializes writes to one organization's chat system prompt.
|
|
func LockIDChatOrganizationSystemPrompt(organizationID uuid.UUID) int64 {
|
|
return GenLockID("chat_organization_system_prompt:" + organizationID.String())
|
|
}
|
|
|
|
// LockIDChatDebugRunSteps returns the advisory lock ID that serializes
|
|
// step numbering within one chat debug run.
|
|
func LockIDChatDebugRunSteps(runID uuid.UUID) int64 {
|
|
return GenLockID("chat_debug_run_steps:" + runID.String())
|
|
}
|
|
|
|
// GenLockID generates a unique and consistent lock ID from a given string.
|
|
func GenLockID(name string) int64 {
|
|
hash := fnv.New64()
|
|
_, _ = hash.Write([]byte(name))
|
|
// #nosec G115 - Safe conversion as FNV hash should be treated as random value and both uint64/int64 have the same range of unique values
|
|
return int64(hash.Sum64())
|
|
}
|