Review a Submission Handler for Double-Submit, Partial Writes, and Unsafe Retries
You are a careful backend reviewer. I will paste the code for a handler that accepts a user-submitted action that changes state (a checkout, a form submission, a booking, a payment confirmation, a signup). Review it for what happens when the same submission arrives twice, arrives half-finished, or is retried after an ambiguous failure. Work through these questions in order and report each as "ok", "risk", or "unclear", quoting the lines that justify the verdict: 1. Double submit: if the user double-clicks, or a client retries after a timeout, can the handler create two orders, two charges, two emails, or two rows? Is there an idempotency key, a unique constraint, or a state check that makes the second request a no-op or a replay of the first response? 2. Check-then-act races: does the code read a value (stock, balance, status, existing record) and then write based on it without a lock, a transaction, a conditional update, or a constraint? Describe the exact interleaving of two requests that breaks it. 3. Partial writes: list every side effect in order (database writes, external API calls, emails, queue messages). If the process dies between any two of them, what state is left behind? Which steps can be safely repeated, and which cannot? 4. Ambiguous failures: if an external call times out, does the code know whether it succeeded? What does the user see, and what happens if they retry? 5. Response behavior: does a duplicate request return something the client can treat as success, or an error that will make the user try yet again? 6. Ordering: could any side effect (like an email) fire before the transaction commits, and then be sent for work that is rolled back? Output format: a table of the six areas with verdicts, then up to five concrete findings ranked by likelihood times damage. For each finding give the failing scenario as a numbered sequence of events, and a minimal fix (for example, "add a unique index on (user_id, client_request_id) and return the stored result on conflict"). Finish with the two tests you would write first to prove the worst finding, and say plainly if the code is too short or missing context to judge a particular area. When to use it: before shipping any handler where duplicates cost money or trust, and after an incident involving duplicate records. How to adapt it: add your database, queue, and payment provider by name so the fixes use the right primitives; tell it which side effects are already idempotent so it doesn't flag them; ask for a language-specific patch only after you've agreed on the strategy. Example input: a Node handler that reads the cart, calls a payment API, inserts an order row, then sends a confirmation email, with no request identifier. Good output: it points out that a retry after the payment API timeout can charge twice, shows the sequence (request A charges and times out client-side, request B charges again), proposes a client-generated key stored with a unique constraint and passed to the payment provider as its idempotency token, notes the email should be sent from a queue after commit, and suggests a test that fires two concurrent identical requests and asserts exactly one order and one charge.
Sign in to upvote this prompt.