Duplicate Leaderboard Records Race Condition#78
Merged
Conversation
|
@Ayaanshaikh12243 is attempting to deploy a commit to the csxark's projects Team on Vercel. A member of the Team first needs to authorize it. |
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.
Fix Duplicate Leaderboard Records Race Condition
Overview
This PR fixes a critical race condition where concurrent correct flag submissions create duplicate leaderboard records for the same team+challenge combination.
Issue #72: Duplicate Leaderboard Records Race Condition
Problem
When multiple users from the same team (or rapid successive submissions) submit the correct flag simultaneously, the system creates duplicate completion records in the leaderboard table.
Race Condition Scenario:
Root Causes:
validate-flagfunction only validates, frontend inserts separatelySolution
Implemented database-level duplicate prevention and atomic operations:
(team_name, question_id)WHEREcompleted_at IS NOT NULLvalidate-flagEdge Function23505) and return appropriate responseidempotency_keycolumn for retry scenariosChanges Made
Database Migrations
Migration:
20260203_add_leaderboard_unique_constraint.sqlKey Features:
completed_at IS NOT NULLcompleted_at = NULL)Migration:
20260204_add_leaderboard_idempotency_key.sqlKey Features:
Backend Changes
Updated:
supabase/functions/validate-flag/index.tsAdded Request Fields:
Atomic Leaderboard Insertion:
Response Format:
Frontend Changes
File:
src/components/ChallengePage.tsxFunction:
handleSubmit()Before (Vulnerable):
After (Protected):
Key Improvements:
Technical Details
How the Unique Constraint Works
Behavior:
Race Condition Prevention Flow
Testing Recommendations
Concurrent Submission Tests
Database Constraint Tests
Edge Function Tests
Migration Path
For Existing Deployments
Step 1: Clean up existing duplicates
Step 2: Deploy migrations
Step 3: Deploy backend changes
# Deploy updated validate-flag Edge Function supabase functions deploy validate-flagStep 4: Deploy frontend changes
Step 5: Verify
For New Deployments
Breaking Changes
validate-flagEdge Function now expects additional fields for leaderboard insertiontime_spent,attempts,hints_used, etc. tovalidate-flagNon-Breaking Changes
idempotency_keyis optional (backwards compatible)Performance Implications
Security & Compliance
Issues Addressed
Data Integrity
Rollback Plan
If issues occur:
Revert frontend changes
git revert {commit_hash} npm run build && deployRevert Edge Function
# Deploy previous version git checkout {previous_commit} supabase functions deploy validate-flagDrop unique constraint (not recommended)
Keep migrations (no harm in keeping user_id and idempotency_key columns)
Checklist
Related Issues
Files Changed
Review Notes
This PR completely eliminates the duplicate leaderboard records race condition using database-level constraints and atomic operations, ensuring data integrity even under high concurrency.