From a9b7007d5dc6fe1b723721386628da1efbc502c5 Mon Sep 17 00:00:00 2001 From: paula-alves Date: Thu, 26 Nov 2026 22:19:13 +0000 Subject: [PATCH] fix(delivery): idempotent retry when item leaves FAILED between render and click (BIT-33418) 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 --- apps/api/src/delivery/routes.ts | 9 +++++++-- apps/api/test/delivery.test.ts | 26 ++++++++++++++++++++++++++ apps/app/src/pages/DeliveryPage.tsx | 2 +- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/apps/api/src/delivery/routes.ts b/apps/api/src/delivery/routes.ts index fcfa0fa..2acb084 100644 --- a/apps/api/src/delivery/routes.ts +++ b/apps/api/src/delivery/routes.ts @@ -392,8 +392,13 @@ export async function registerDeliveryRoutes( where: { id: itemId, organizationId: orgId, status: "FAILED" }, data: { status: "QUEUED", attempts: 0, nextAttemptAt: new Date(), lastError: null }, }); - if (!result.count) throw notFound("failed_item_not_found"); - return toItemSummary(await db.deliveryItem.findUniqueOrThrow({ where: { id: itemId } })); + if (result.count) return toItemSummary(await db.deliveryItem.findUniqueOrThrow({ where: { id: itemId } })); + // Race: the item was no longer FAILED between the render and the retry (autopilot/queue worker + // requeued, retried, or retracted it). The retry is idempotent — return the current state instead + // of a 404, so the desktop does not unhandledrejection into the error loop (BIT-33418). + const current = await db.deliveryItem.findUnique({ where: { id: itemId } }); + if (!current) throw notFound("item_not_found"); + return toItemSummary(current); }); /** ZeroZero: the event's game on the photo desk (chosen, or found by teams and time). */ diff --git a/apps/api/test/delivery.test.ts b/apps/api/test/delivery.test.ts index 3eb0c97..21cea1b 100644 --- a/apps/api/test/delivery.test.ts +++ b/apps/api/test/delivery.test.ts @@ -851,6 +851,32 @@ describe("delivery", () => { expect(broadcaster.sent.filter((s) => s.message.type === "delivery.updated").map((s) => (s.message as { item: { status: string } }).item.status)).toEqual(["QUEUED", "QUEUED", "FAILED", "DONE"]); }); + it("retry is idempotent when the item leaves FAILED between render and click (BIT-33418)", async () => { + const { orgId, token, target } = await setup(); + const id = await photo("race.jpg", "2026:09:23 21:10:00"); + await delivery.enqueue({ organizationId: orgId, targetId: target.id, photoIds: [id], again: false, requestedById: (await ctx.db.user.findFirstOrThrow()).id }); + const [claimed] = await delivery.claim(5); + nc.status = 401; + expect(await delivery.process(claimed!)).toMatchObject({ status: "FAILED", attempts: 1 }); + nc.status = undefined; + + // Between the desktop rendering the FAILED row and the user clicking "Repetir", the queue worker + // requeued the item (autopilot, queueEdited, queueOrphanRetracts, …) so it is no longer FAILED. + const itemId = (await ctx.db.deliveryItem.findFirstOrThrow({ where: { photoId: id, targetId: target.id } })).id; + await ctx.db.deliveryItem.update({ where: { id: itemId }, data: { status: "QUEUED", attempts: 0, nextAttemptAt: new Date(), lastError: null } }); + + // The retry must not 404 — it returns the current state, so the desktop's promise resolves and no + // unhandledrejection reaches the error hub. + const retry = await ctx.app.inject({ method: "POST", url: `/v1/orgs/${orgId}/deliveries/${itemId}/retry`, headers: bearer(token) }); + expect(retry.statusCode).toBe(200); + expect(retry.json()).toMatchObject({ id: itemId, status: "QUEUED", attempts: 0 }); + + // A truly missing item still 404s. + const ghost = await ctx.app.inject({ method: "POST", url: `/v1/orgs/${orgId}/deliveries/00000000-0000-4000-8000-000000000000/retry`, headers: bearer(token) }); + expect(ghost.statusCode).toBe(404); + expect(ghost.json()).toMatchObject({ error: "item_not_found" }); + }); + it("writes long venues whole in IIM and XMP, flagged as UTF-8", async () => { const src = path.join(dir, "src.jpg"); await writeFile(src, await sharp({ create: { width: 300, height: 200, channels: 3, background: "#123456" } }).jpeg().toBuffer()); diff --git a/apps/app/src/pages/DeliveryPage.tsx b/apps/app/src/pages/DeliveryPage.tsx index 30b1620..1a4150b 100644 --- a/apps/app/src/pages/DeliveryPage.tsx +++ b/apps/app/src/pages/DeliveryPage.tsx @@ -136,7 +136,7 @@ export function DeliveryPage() { {i.status === "FAILED" && ( - )} -- 2.47.3