Repository navigation
fix(coderd): lock a nested family leaves first when archiving it - #89
Conversation
…ansitions cannot deadlock Archiving a root locked the root FOR UPDATE and then each member. A member transition updates its row twice, and the second update takes FOR KEY SHARE on the parent and the root through the foreign keys, so the two transactions deadlocked. Behind fork-nested-subagents the archive now locks the root FOR NO KEY UPDATE, then the members leaves first, and then runs the upstream SetFamilyArchived. Fixes #79
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7f578b3fa2
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
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".
…ly lock The auto-archive worker called the root-first SetFamilyArchived directly, so a candidate that became active after the scan could deadlock the same way. The regression tests now hold the transition on a raw connection and wait until the archive is blocked by that backend, for manual archive, unarchive and auto-archive.
@codex review |
|
Codex Review: Didn't find any major issues. 🎉 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". |
Closing and reopening only to make GitHub recompute the merge ref against current main ( |
The merge ref now points at current main ( |
Merge evidence for head
|
Fixes #79. Part of #8 (W2 nested subagents).
With
fork-nested-subagentson, archiving or unarchiving a root, by hand or through auto-archive, no longer deadlocks with a state transition of one of its subagents. With the experiment off, nothing changes.Cause. A chatstate transition updates its chat row twice in one transaction:
LockChatAndBumpSnapshotVersionthenUpdateChatExecutionState. Postgres rechecks the foreign keys on the second update of a row that the same transaction already updated, so it takesFOR KEY SHAREon theparent_chat_idandroot_chat_idrows. Upstream'sSetFamilyArchivedholds the rootFOR UPDATE, which conflicts withFOR KEY SHARE, and then waits for each member. If a member is in such a transition, each transaction waits for the other, and Postgres aborts one of them. That is the 500 in the issue.Fix.
forkSetFamilyArchived(newcoderd/x/chatd/fork_family_lock.go) locks the family first, in one transaction:FOR NO KEY UPDATE, through a new forkdb queryLockChatForNoKeyUpdate. This mode still blocks spawns, which lock the rootFOR SHARE, so the family cannot grow. It does not block the foreign key checks.FOR UPDATE, leaves first (GetChatSubtreeIDsorder). A member is locked only after its descendants' transitions have committed, so no transition waits for a parent that the archive holds.Then it runs upstream's
SetFamilyArchivedin the same transaction, where every row is already locked. Its publications flush only after commit. Upstream'ssetChatFamilyArchivedand the auto-archive worker each change by one call, so manual archive, unarchive and auto-archive all use the new path.fork_cascade.gois unchanged. The new query has dbauthz (chat update) and dbmetrics wrappers.ARCHITECTURE.mdgets only a TODO for the author.Validation
TestForkSetFamilyArchivedConcurrentTransitionwith real Postgres (Colima) uses a depth-2 family. A raw connection updates the grandchild once, as the snapshot bump of a transition does, and keeps its transaction open. The test then archives the root and waits until that backend blocks the archive (pg_blocking_pids, no sleep). Then it makes the second update and commits. The test asserts that both succeed and that every member flipped. Subtests:ArchiveAndUnarchive(manual archive, then unarchive) andAutoArchive(the worker'sarchiveCandidate). Both pass 3 of 3 runs. Each of these breaks fails both subtests withpq: deadlock detected2 of 2 times: the upstream call at both sites, members locked root first, and the root lockedFOR UPDATEinstead ofFOR NO KEY UPDATE.go testpasses for./coderd/x/chatd/...,./coderd/database/dbauthz/,./coderd/database/forkdb/...and./coderd/database/dbmetrics/.forkdb/generate.shleaves no diff.ERROR: deadlock detected ... FOR KEY SHARE OF xin the held transaction.