Skip to content

fix(coderd): lock a nested family leaves first when archiving it - #89

Merged
ibetitsmike merged 5 commits into
mainfrom
mike/issue-79-archive-deadlock
Nov 10, 2026
Merged

ibetitsmike merged 5 commits into
mainfrom
mike/issue-79-archive-deadlock

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 10, 2026 •

Copy link
Copy Markdown
Owner

Xum opened this PR on behalf of @ibetitsmike.

Fixes #79. Part of #8 (W2 nested subagents).

With fork-nested-subagents on, 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: LockChatAndBumpSnapshotVersion then UpdateChatExecutionState. Postgres rechecks the foreign keys on the second update of a row that the same transaction already updated, so it takes FOR KEY SHARE on the parent_chat_id and root_chat_id rows. Upstream's SetFamilyArchived holds the root FOR UPDATE, which conflicts with FOR 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 (new coderd/x/chatd/fork_family_lock.go) locks the family first, in one transaction:

  • The root FOR NO KEY UPDATE, through a new forkdb query LockChatForNoKeyUpdate. This mode still blocks spawns, which lock the root FOR SHARE, so the family cannot grow. It does not block the foreign key checks.
  • Each descendant FOR UPDATE, leaves first (GetChatSubtreeIDs order). 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 SetFamilyArchived in the same transaction, where every row is already locked. Its publications flush only after commit. Upstream's setChatFamilyArchived and the auto-archive worker each change by one call, so manual archive, unarchive and auto-archive all use the new path. fork_cascade.go is unchanged. The new query has dbauthz (chat update) and dbmetrics wrappers. ARCHITECTURE.md gets only a TODO for the author.

Validation
  • New TestForkSetFamilyArchivedConcurrentTransition with 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) and AutoArchive (the worker's archiveCandidate). Both pass 3 of 3 runs. Each of these breaks fails both subtests with pq: deadlock detected 2 of 2 times: the upstream call at both sites, members locked root first, and the root locked FOR UPDATE instead of FOR NO KEY UPDATE.
  • go test passes for ./coderd/x/chatd/..., ./coderd/database/dbauthz/, ./coderd/database/forkdb/... and ./coderd/database/dbmetrics/. forkdb/generate.sh leaves no diff.
  • UAT ran at 4415ba3, before the auto-archive hook, in the disposable dev container, with GLM-5.3-Flash through oMLX:
    • At 4415ba3 with a real depth-2 family, a psql session held a member's row lock, as a transition does, while the API patched the root. Archiving with the depth-1 member held, unarchiving with the depth-2 member held, and archiving with the depth-2 member held each returned 204 after the held transaction committed. Both updates of that transaction succeeded, and every member flipped.
    • At the same commit, archiving a root while its coordinator and worker were running returned 204 in 0.13 s and 0.15 s in two runs, with all three chats archived.
    • Control at base e6cd44a, with the same held-lock script: both depth-2 runs returned ERROR: deadlock detected ... FOR KEY SHARE OF x in the held transaction.

…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
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum commented on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-10T01:08:27.670074Z c7df195 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread coderd/x/chatd/chat_mutations.go Resolved
Comment thread coderd/x/chatd/ARCHITECTURE.md Resolved
…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.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum commented on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: c7df1953c5

ℹ️ 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".

@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum (AI agent) closed and reopened this on behalf of @ibetitsmike.

Closing and reopening only to make GitHub recompute the merge ref against current main (1ef5ccc60b) and run fork-ci on it. The head is unchanged.

@ibetitsmike ibetitsmike reopened this Nov 10, 2026
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum (AI agent) closed and reopened this again on behalf of @ibetitsmike.

The merge ref now points at current main (1ef5ccc60b), but the run started by the first reopen was queued against the old merge ref. This second reopen starts fork-ci on the recomputed merge. The head is unchanged.

@ibetitsmike ibetitsmike reopened this Nov 10, 2026
@ibetitsmike
ibetitsmike merged commit d559632 into main Nov 10, 2026
21 of 28 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-79-archive-deadlock branch November 10, 2026 01:43
@ibetitsmike

ibetitsmike commented Nov 10, 2026 •

Copy link
Copy Markdown
Owner Author

Xum (AI agent) merged this on behalf of @ibetitsmike.

Merge evidence for head c7df1953c51238729dcb700474e3682c1990bc59:

  • fork-ci pull_request run 42735988009 passed; its build job checked out Merge c7df1953c5 into 1ef5ccc60b (current main, deployed and healthy at 01:42Z). All checks green.
  • Codex review round 2 on this exact head: no major issues. The 3 round 1 threads are replied to and resolved; 0 unresolved review threads. Merge state CLEAN.
  • TestForkSetFamilyArchivedConcurrentTransition (ArchiveAndUnarchive, AutoArchive) runs on real Postgres and passes 3 of 3. Each of three breaks (the upstream root-first call at both sites, a root-first member order, root FOR UPDATE) fails both subtests with pq: deadlock detected 2 of 2.
  • UAT at 4415ba3 (before the auto-archive hook, which the AutoArchive subtest covers): with a member's row lock held, archive and unarchive at depth 1 and depth 2 returned 204 3 of 3, and archiving a running depth-2 family returned 204 2 of 2. On base e6cd44a the same held-lock script deadlocked 2 of 2.
  • The only change to coderd/x/chatd/ARCHITECTURE.md is an owner TODO line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(coderd/x/chatd): archiving an active nested family can deadlock

1 participant