Skip to content

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

Closed

Description

@ibetitsmike

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

Problem

Archiving a root chat whose nested subagent family is still running can fail with a Postgres deadlock. The family is interrupted, but nothing is archived, and the user gets a 500.

Found by W7's pre-gate pass on the fork UI UAT server (:3100, v2.39.0-devel+20eb8dc198), run 1 of 2:

  • Root d0de9b51, worker child c9cafce0 running sleep 600.
  • PATCH /api/v2/chats/d0de9b51-49ef-4b0f-90fa-d3faab06870f with archived: true returned 500 after about 1 s at 23:01:38Z:
    Failed to archive chat. / execute transaction: execute transaction: lock chat and bump snapshot: pq: deadlock detected (coderd/x/chatd/chatstate/machine.go:166).
  • Retrying on the now idle family returned 204 and archived the whole family. Run 2 on a fresh family (root a2f75873) returned 204 at once.

Postgres log ([email protected], 00:01:39 CET):

Process 79929 waits for ShareLock on transaction 1634408; blocked by process 81262.
Process 81262 waits for ShareLock on transaction 1634420; blocked by process 79929.
Process 79929: LockChatAndBumpSnapshotVersion   (SELECT ... FROM chats WHERE id = $1 FOR UPDATE)
Process 81262: UpdateChatExecutionState         (UPDATE chats SET status, archived, worker_id, ...)
CONTEXT: while locking tuple (1,1) in relation "chats"

So the archive cascade (coderd/x/chatd/fork_cascade.go, #42) and a concurrent state transition on the same family lock two chats rows in opposite order. The cascade takes the family root-first; the other transaction holds one family row and then updates another.

Expected

Archiving an active family is atomic and never deadlocks: every transaction that locks more than one chat row in a family takes them in the same order (root first, then a stable order), or the cascade retries a serialization or deadlock failure.

Acceptance

  • A regression test with a real Postgres reproduces the interleaving (archive the root while a child's turn ends or is interrupted) and passes with the fix.
  • Archiving a root with a running depth-1 or depth-2 family returns 204 and archives every descendant in one transaction.

Activity

  1. added
    bug
    on Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions