fix(archive): skip rebuildDirectory when another worker holds the advisory lock (BIT-33539) #330

Merged
spark merged 1 commits from fix/BIT-33539-archive-rebuild-directory-pk-race into main 2026-12-04 13:32:30 +00:00
Owner

Root cause

rebuildDue selects a due source with a plain SELECT ... LIMIT 1 (no locking). Two workers can pick the same source, both call rebuildDirectory, and both run deleteMany + createMany in overlapping transactions. The second commit hits archive_team_sides_pkey with P2002.

Fix

Add pg_try_advisory_xact_lock(hashtext(sourceId)) at the start of the rebuildDirectory transaction. If another worker already holds the lock, the second worker skips the delete/create cycle entirely — no PK collision.

The advisory lock is transaction-scoped (xact), so it is released automatically when the transaction commits or rolls back.

Test

Two new tests in service.test.ts:

  • Lock acquired → deleteMany + createMany + updateMany all run, row count returned.
  • Lock held by another worker → all three skipped, returns 0.

Paperclip-Issue: BIT-33539

## Root cause `rebuildDue` selects a due source with a plain `SELECT ... LIMIT 1` (no locking). Two workers can pick the same source, both call `rebuildDirectory`, and both run `deleteMany` + `createMany` in overlapping transactions. The second commit hits `archive_team_sides_pkey` with P2002. ## Fix Add `pg_try_advisory_xact_lock(hashtext(sourceId))` at the start of the `rebuildDirectory` transaction. If another worker already holds the lock, the second worker skips the delete/create cycle entirely — no PK collision. The advisory lock is transaction-scoped (`xact`), so it is released automatically when the transaction commits or rolls back. ## Test Two new tests in `service.test.ts`: - Lock acquired → deleteMany + createMany + updateMany all run, row count returned. - Lock held by another worker → all three skipped, returns 0. Paperclip-Issue: BIT-33539
spark added 1 commit 2026-12-04 13:20:19 +00:00
fix(archive): skip rebuildDirectory when another worker holds the advisory lock (BIT-33539)
ci / typescript (push) Skipped
ci / rust (push) Skipped
ci / rust (pull_request) Successful in 4m1s
ci / typescript (pull_request) Successful in 10m53s
941688d2c9
Two workers could pick the same due source from rebuildDue (the SELECT
has no locking), both call deleteMany + createMany in overlapping
transactions, and the second commit hit archive_team_sides_pkey with
P2002. Add pg_try_advisory_xact_lock(hashtext(sourceId)) at the start
of the transaction: the second worker skips instead of colliding.

Paperclip-Issue: BIT-33539
paperclip-review-bot left a comment
Member

auto-approve: CI success on 941688d2c9 (gitea_auto_approve_open_prs_once.py)

auto-approve: CI success on 941688d2c9fa (gitea_auto_approve_open_prs_once.py)
founding-engineer left a comment
Member

auto-approve: CI success on 941688d2c9 (gitea_auto_approve_open_prs_once.py)

auto-approve: CI success on 941688d2c9fa (gitea_auto_approve_open_prs_once.py)
devops-engineer left a comment
Member

auto-approve: CI success on 941688d2c9 (gitea_auto_approve_open_prs_once.py)

auto-approve: CI success on 941688d2c9fa (gitea_auto_approve_open_prs_once.py)
spark merged commit 6cd8ba0b95 into main 2026-12-04 13:32:30 +00:00
Sign in to join this conversation.