A page appeared to stay stuck on the first item opened, while the system quietly created extra work assignments in the background.
The bug looked like stale UI state at first. The deeper problem was that a request which changed server state was being treated like a cacheable read.
The failure pattern
The frontend used a data-fetching query for an endpoint that claimed the next available item. That endpoint did more than retrieve data: it created an assignment and locked the item for the current user.
Because the request was cached, revisiting the screen could immediately reuse the first response. A background refetch could then run the POST again, creating another assignment even though the UI continued displaying the original item.
There was a second issue: changing only a query-string parameter did not remount the page component. Local state still held the first active item, so the URL and the visible content could disagree.
The fix
The repair had two coordinated parts.
On the backend, the open queue now checks for the caller's oldest in-progress assignment before claiming anything new. If unfinished work exists, the same assignment is returned. A new assignment is created only when the caller has no active work.
On the frontend, the state-changing POST was moved from a cached query to an explicitly triggered mutation. The Annotator view is also remounted when its target changes, so local state cannot leak from one page to another. Sign-out and failed token refreshes clear the server-state cache as well.
After an item is approved or released, navigation returns to a clean queue URL so the next request represents the user's actual next action.
What this reinforced
- Use queries for reads and mutations for operations with server-side effects.
- Treat every POST that creates, locks, charges, sends, or claims something as non-cacheable unless it is deliberately idempotent.
- Keep the server safe even when the client retries, refreshes, or behaves unexpectedly.
- Test browser history and query-parameter navigation, not just full-page loads.
- React development behavior can expose real duplicate-request bugs; it should not be dismissed as noise.
The most useful debugging question was simple: does this request merely read state, or does it create state? Once that distinction was made explicit, both the frontend and backend fixes became straightforward.
This lesson generalizes well beyond assignment queues: checkout flows, reservation systems, job dispatch, email sending, and any UI action where a repeated request has consequences all need the same discipline.
Top comments (2)
The "return the oldest in-progress assignment first" check covers the refetch-after-cache case, but it's still check-then-insert, so two requests that land at the same time both see nothing and both claim. That's exactly what StrictMode's double effect, two tabs or a double tap on the mutation button produce.
I tried it on Postgres 17 with the claim written the way I read the post (select existing, else lock the next free item with
FOR UPDATE SKIP LOCKED, insert the assignment, all in one transaction, default READ COMMITTED), firing two claims per user at once. 497 of 500 users ended up with two active assignments and 997 items locked.Two ways that took it to 0 for me:
The losing request gets a 23505, so the handler has to catch it, roll back and re-read the existing assignment, otherwise the user just sees a 500 instead of a duplicate.
With the lock the second request waits, sees the first one's assignment and returns it, which is the behaviour you actually want. I'd keep the partial index anyway as the backstop, since it's the only part that doesn't depend on every code path remembering to take the lock.
On the React side,
disabled={mutation.isPending}on the claim button is worth adding too, the switch to a mutation stops the refetches but not a double click.Great postmortem. Treating a side-effecting POST like a cacheable read is such an easy mistake to make. On the FastAPI side, an idempotency key (client generates a UUID per action, server stores it with the result) has saved me from exactly this kind of duplicate when retries or double-clicks slip through. Did you end up adding one, or was fixing the client cache enough?