fix(delivery): idempotent retry when item leaves FAILED between render and click (BIT-33418) #312

Merged
spark merged 1 commits from task/BIT-33418-retry-race-condition into main 2026-11-26 22:31:42 +00:00
Owner

Root cause (BIT-33418)

Race condition in the desktop (Tauri) delivery-retry flow:

  1. User is on tauri://localhost/delivery (DeliveryPage) with a FAILED item.
  2. Clicks "Repetir" — DeliveryPage.tsx:139 called api.retryDelivery(orgId, i.id).then(load) without .catch().
  3. The API (delivery/routes.ts) did updateMany with where: { id, organizationId, status: "FAILED" }.
  4. If result.count === 0 (autopilot/queue worker requeued, retried, or retracted the item between render and click), it threw notFound("failed_item_not_found") → 404.
  5. The rejected promise had no handler → unhandledrejection → error loop.

Fix

  • API (apps/api/src/delivery/routes.ts): when updateMany matches zero rows, fetch the item. If it exists, return its current state (200) — the retry is idempotent. Only 404 (item_not_found) when the row is actually gone.
  • Frontend (apps/app/src/pages/DeliveryPage.tsx): add a rejection handler to .then(load) that reloads the list (() => load()), so a stale FAILED row refreshes instead of becoming an unhandled rejection.

Test

New case in apps/api/test/delivery.test.ts: enqueues a photo, forces it to FAILED via a 401, then flips the row to QUEUED (simulating the worker requeue) and POSTs /retry. Asserts:

  • 200 with the current QUEUED state (no more 404)
  • a truly missing item still 404s with item_not_found

Scope

Two-file product fix + test. No schema, no infra, no queue/autopilot logic.

Paperclip-BIT-33418

## Root cause (BIT-33418) Race condition in the desktop (Tauri) delivery-retry flow: 1. User is on `tauri://localhost/delivery` (DeliveryPage) with a `FAILED` item. 2. Clicks "Repetir" — `DeliveryPage.tsx:139` called `api.retryDelivery(orgId, i.id).then(load)` **without `.catch()`**. 3. The API (`delivery/routes.ts`) did `updateMany` with `where: { id, organizationId, status: "FAILED" }`. 4. If `result.count === 0` (autopilot/queue worker requeued, retried, or retracted the item between render and click), it threw `notFound("failed_item_not_found")` → 404. 5. The rejected promise had no handler → `unhandledrejection` → error loop. ## Fix - **API** (`apps/api/src/delivery/routes.ts`): when `updateMany` matches zero rows, fetch the item. If it exists, return its current state (200) — the retry is idempotent. Only 404 (`item_not_found`) when the row is actually gone. - **Frontend** (`apps/app/src/pages/DeliveryPage.tsx`): add a rejection handler to `.then(load)` that reloads the list (`() => load()`), so a stale `FAILED` row refreshes instead of becoming an unhandled rejection. ## Test New case in `apps/api/test/delivery.test.ts`: enqueues a photo, forces it to `FAILED` via a 401, then flips the row to `QUEUED` (simulating the worker requeue) and POSTs `/retry`. Asserts: - 200 with the current `QUEUED` state (no more 404) - a truly missing item still 404s with `item_not_found` ## Scope Two-file product fix + test. No schema, no infra, no queue/autopilot logic. Paperclip-BIT-33418
spark added 1 commit 2026-11-26 22:19:20 +00:00
fix(delivery): idempotent retry when item leaves FAILED between render and click (BIT-33418)
ci / rust (pull_request) Successful in 7m10s
ci / typescript (pull_request) Successful in 12m3s
a9b7007d5d
Race condition: between the desktop rendering a FAILED delivery row and the
user clicking "Repetir", the autopilot/queue worker can requeue, retry, or
retract the item. The retry endpoint's updateMany (where status=FAILED)
matched zero rows and threw notFound("failed_item_not_found") -> 404, and
the frontend promise had no .catch -> unhandledrejection -> error loop.

API: when the item no longer matches FAILED, fetch it; if it exists return
its current state (200, idempotent), only 404 when the row is gone.
Frontend: add a rejection handler that reloads the list so a stale FAILED
row refreshes instead of becoming an unhandled rejection.

Paperclip-BIT-33418
paperclip-review-bot left a comment
Member

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

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

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

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

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

auto-approve: CI success on a9b7007d5dc6 (gitea_auto_approve_open_prs_once.py)
spark merged commit 92cecc7214 into main 2026-11-26 22:31:42 +00:00
Sign in to join this conversation.