[bug] chat task issue-number allocation races, failing concurrent enqueues with a duplicate key #1450

Closed
opened 2026-10-27 13:40:27 +00:00 by sam · 0 comments
Owner

InsertChatTask and EnqueueCallTask both allocate the synthetic chat issue_number as COALESCE(MAX(issue_number) ...) + 1 in a single statement. Under READ COMMITTED the second concurrent statement computes MAX from the snapshot taken before it blocked on the CTE's SELECT ... FOR UPDATE, so both pick the same number and the second INSERT fails with SQLSTATE 23505 on tasks_org_identity_owner_repo_number_key.

Measured (independent verification of archie-core-32ae, a fresh probe against pgstore with a throwaway postgres:18): 2 goroutines on one caller, 4/20 rounds failed; two callers in one repo, 1/20 failed. Reachable from a supported configuration, not only from calls: workflow.call is explicitly allowed in parallel branches (parallelSafe), and any two chat or binding dispatches can land together. Pre-existing: the allocator is unchanged by the workflow.call key work and is shared by InsertChatTask (queries/tasks.sql) and EnqueueCallTask (queries/call_tasks.sql). The FOR UPDATE does not close it because the snapshot is fixed at statement start.

Smallest producer-side fix: allocate the number from a sequence seeded past max(issue_number) per repo (a migration plus the two queries) instead of reading MAX in the insert. A retry-on-23505 loop would paper over it at the consumer. Done when two concurrent chat enqueues in one repo both commit, and an idempotency test covers it.

Close reason

Verified: fixed in code or doc/file no longer exists (see burndown 2026-10-13)


Imported from beads archie-core-mo0x · GitHub #1563 · type bug · priority P2 · created 2026-10-08 · closed 2026-10-12

InsertChatTask and EnqueueCallTask both allocate the synthetic chat issue_number as COALESCE(MAX(issue_number) ...) + 1 in a single statement. Under READ COMMITTED the second concurrent statement computes MAX from the snapshot taken before it blocked on the CTE's SELECT ... FOR UPDATE, so both pick the same number and the second INSERT fails with SQLSTATE 23505 on tasks_org_identity_owner_repo_number_key. Measured (independent verification of archie-core-32ae, a fresh probe against pgstore with a throwaway postgres:18): 2 goroutines on one caller, 4/20 rounds failed; two callers in one repo, 1/20 failed. Reachable from a supported configuration, not only from calls: workflow.call is explicitly allowed in parallel branches (parallelSafe), and any two chat or binding dispatches can land together. Pre-existing: the allocator is unchanged by the workflow.call key work and is shared by InsertChatTask (queries/tasks.sql) and EnqueueCallTask (queries/call_tasks.sql). The FOR UPDATE does not close it because the snapshot is fixed at statement start. Smallest producer-side fix: allocate the number from a sequence seeded past max(issue_number) per repo (a migration plus the two queries) instead of reading MAX in the insert. A retry-on-23505 loop would paper over it at the consumer. Done when two concurrent chat enqueues in one repo both commit, and an idempotency test covers it. ### Close reason Verified: fixed in code or doc/file no longer exists (see burndown 2026-10-13) --- _Imported from beads `archie-core-mo0x` · GitHub #1563 · type bug · priority P2 · created 2026-10-08 · closed 2026-10-12_
sam added the mediumbug labels 2026-10-27 13:40:27 +00:00
sam self-assigned this 2026-10-27 13:40:27 +00:00
sam closed this issue 2026-10-27 13:40:27 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: sam/archie#1450