fix(api): make winner selection atomic and cap winners inside the transaction - #450
Open
MRTEEworld02 wants to merge 4 commits into
Open
MRTEEworld02 wants to merge 4 commits into
MRTEEworld02 wants to merge 4 commits into
Conversation
|
@MRTEEworld02 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
POST /api/posts/[id]/select-winnersread the post'sstatus, existingwinnersand entries, then computed eligibility and ran the "already completed" guard before opening the$transaction. Two concurrent requests could both pass that guard on stale data, each pick a winner set and each flipstatustocompleted.skipDuplicatesprevented duplicatePostWinnerrows but did not stop the total from exceedingmaxWinners(nor duplicate winner notifications).This moves the read, the guards, the selection and the writes into a single
Serializableinteractive transaction:status, existingwinners, entries) is re-read inside the transaction, and the "already completed" / selectable-status guards run against that read.updateManywhere the status is stillopen/active/in_progress). A concurrent request that already completed the post affects 0 rows and gets the existing400response.remainingSlots = maxWinners - existingWinnersis computed inside the transaction; every selection method is capped by it, and the committedPostWinnercount is re-checked before writing, so concurrent requests can never collectively over-assign.P2034serialization failures are surfaced as409instead of a generic500.Tests
Added
app/tests/api/select-winners.test.ts:maxWinnersis never exceeded;400.Notes
This is the assignee's PR for #426 (assignee @MRTEEworld02). PR #449 by macsonfleek is a separate third-party submission on the same issue.
Closes #426