fix(coderd): serialize chat model config default election with advisory lock - #27114
Merged
Conversation
ethanndickson
marked this pull request as ready for review
July 9, 2026 06:22
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. Breezy! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
mafredri
reviewed
Jul 9, 2026
mafredri
left a comment
Member
There was a problem hiding this comment.
Can the provider be used to choose to promote a model config to default, but during concurrent creation the first non-default creation that acquires the lock promotes itself resulting in an error creating the one we want as default?
…ry lock Replaces the unique-violation retry loop: the election transactions now take pg_advisory_xact_lock(LockIDChatModelConfigDefault) so only one election runs at a time and the single-default index is never contended. The index and the 409 mapping stay as the schema-level backstop.
ethanndickson
force-pushed
the
chat-config-hxjn
branch
from
July 13, 2026 06:18
3050550 to
00e63d3
Compare
mafredri
approved these changes
Jul 13, 2026
mafredri
reviewed
Jul 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes CODAGT-736
Concurrent chat model config writes on a deployment with no default all elect themselves default: at READ COMMITTED neither transaction sees the other's uncommitted default, so both self-promote and
idx_chat_model_configs_single_defaultrejects the loser as a spurious 409. The coderd Terraform provider hits this routinely, since a singleterraform applycreates or deletes many configs in parallel by design.The fix serializes the election with a transaction-scoped advisory lock: the create, update, and delete handlers run their default election inside a transaction that first takes
pg_advisory_xact_lockon a dedicatedLockIDChatModelConfigDefault, so elections run one at a time and the index is never contended. The partial unique index stays in place as the schema-level invariant, and the existing 409 mapping remains as a backstop for any writer that bypasses the lock.We considered a singleton pointer table (one row holding a
model_config_idFK, making a second default unrepresentable), which would remove the race outright, but it needs a migration, new queries, dbauthz rules, and handler/read-path rework. Not proportionate for an experimental endpoint.