-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
Problem
When submitting a challenge, there's a race condition that can cause duplicate XP transactions and duplicate analytics events.
Root Cause
In server/api/routers/userProgress.ts, the submitChallenge mutation checks if a challenge is already completed:
if (existingProgress?.status === "completed") {
throw new Error("Challenge already completed");
}However, if two requests arrive simultaneously (CLI retry, network issues, double-click):
- Request 1 checks → not "completed" → continues
- Request 2 checks → not "completed" → continues (Request 1 hasn't updated yet)
- Request 1 updates status → creates XP transactions + tracks events
- Request 2 updates status → creates XP transactions + tracks events again
Impact
- Users receive 2x XP for a single challenge completion
- Duplicate
challenge_completedevents in PostHog - Duplicate XP transactions in the database
Proposed Solution
- Add unique constraint on
(userId, challengeId)inuserProgresstable - Use upsert pattern with
INSERT ... ON CONFLICT DO NOTHINGorON CONFLICT DO UPDATE - Check for existing XP transactions before creating new ones (as additional safety)
Files to Modify
server/db/schema/challenge.ts- Add unique index on userProgressserver/api/routers/userProgress.ts- Use upsert pattern in submitChallenge- Create migration file
Additional Notes
This bug exists independently of the onboarding flow - it affects all challenge submissions.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels