fix(archive): recover from P2002 race on cloud create in walk (BIT-33532) #323

Merged
spark merged 1 commits from fix/BIT-33532-cloudfile-p2002-race into main 2026-12-04 10:02:58 +00:00
Member

Problem

ArchiveService.walk used a check-then-create pattern for cloudFolder, cloudFile and cloudVideo. When two concurrent passes (overlapping scans or a folder reachable through two paths) both query existing rows before either creates, the second create hits P2002 on @@unique([sourceId, fileId]) and aborts the whole scan, marking the source ERROR and pushing nextScanAt 15 min out — so the loop recurs.

Fix

Wrap each create in try/catch for P2002 and fall back to an update on the sourceId_fileId compound key. The three-branch logic (new / changed etag / moved) is preserved; only the new-branch create is hardened.

  • apps/api/src/archive/service.ts — cloudFolder.create (walk self), cloudFile.create, cloudVideo.create
  • apps/api/src/archive/service.test.ts — vitest proving the cloudFile fallback updates the compound key and requeues, and that non-P2002 errors still rethrow

No schema change, no migration. Blast radius: archive scan only.

Paperclip-Issue: BIT-33532

## Problem `ArchiveService.walk` used a check-then-create pattern for `cloudFolder`, `cloudFile` and `cloudVideo`. When two concurrent passes (overlapping scans or a folder reachable through two paths) both query existing rows before either creates, the second `create` hits **P2002** on `@@unique([sourceId, fileId])` and aborts the whole scan, marking the source `ERROR` and pushing `nextScanAt` 15 min out — so the loop recurs. ## Fix Wrap each `create` in try/catch for P2002 and fall back to an update on the `sourceId_fileId` compound key. The three-branch logic (new / changed etag / moved) is preserved; only the new-branch create is hardened. - `apps/api/src/archive/service.ts` — `cloudFolder.create` (walk self), `cloudFile.create`, `cloudVideo.create` - `apps/api/src/archive/service.test.ts` — vitest proving the `cloudFile` fallback updates the compound key and requeues, and that non-P2002 errors still rethrow No schema change, no migration. Blast radius: archive scan only. Paperclip-Issue: BIT-33532
bug-squasher added 1 commit 2026-12-04 09:43:44 +00:00
fix(archive): recover from P2002 race on cloud create in walk (BIT-33532)
ci / typescript (push) Skipped
ci / rust (push) Skipped
ci / rust (pull_request) Successful in 5m10s
ci / typescript (pull_request) Successful in 11m12s
5287afb508
ArchiveService.walk used a check-then-create pattern for cloudFolder,
cloudFile and cloudVideo. When two concurrent passes (overlapping scans
or a folder reachable through two paths) both query existing rows before
either creates, the second create hits P2002 on @@unique([sourceId,
fileId]) and aborts the whole scan, marking the source ERROR.

Wrap each create in try/catch for P2002 and fall back to an update on the
sourceId_fileId compound key, so a race no longer fails the scan.

Adds vitest cases proving the cloudFile fallback updates the compound key
and requeues, and that non-P2002 errors still rethrow.

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

auto-approve: CI success on 5287afb508 (gitea_auto_approve_open_prs_once.py)

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

auto-approve: CI success on 5287afb508 (gitea_auto_approve_open_prs_once.py)

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

auto-approve: CI success on 5287afb508 (gitea_auto_approve_open_prs_once.py)

auto-approve: CI success on 5287afb508a3 (gitea_auto_approve_open_prs_once.py)
spark merged commit 7b1596d8fa into main 2026-12-04 10:02:58 +00:00
Sign in to join this conversation.