Skip to content

fix: wrap card creation in transaction to prevent race condition#349

Merged
ShantKhatri merged 2 commits into
Dev-Card:mainfrom
Midoriya-w:fix/card-creation-race-condition
Jun 1, 2026
Merged

fix: wrap card creation in transaction to prevent race condition#349
ShantKhatri merged 2 commits into
Dev-Card:mainfrom
Midoriya-w:fix/card-creation-race-condition

Conversation

@Midoriya-w
Copy link
Copy Markdown
Contributor

Summary

Fixes a race condition in card creation where concurrent requests could create multiple default cards for the same user.

Changes Made

  • Wrapped card count check and card creation inside a single Prisma transaction
  • Replaced app.prisma.card.count with transactional tx.card.count
  • Replaced app.prisma.card.create with transactional tx.card.create
  • Ensured atomic execution of the default-card creation logic

Why This Fix Is Needed

Previously, the count check and card creation were executed as separate database operations. Concurrent requests could both read cardCount === 0 and create multiple cards marked as isDefault: true.

Using a Prisma transaction removes the race window and preserves data integrity.

Result

  • Prevents multiple default cards from being created concurrently
  • Maintains consistent user card state
  • Improves backend reliability under concurrent requests

Closes #333

@Harxhit Harxhit added the gssoc:approved Required label for every approved PR. Gives the base +50 points and enables contribution tracking. label May 27, 2026
},
},
});
return tx.card.create({
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come multiple return statements inside single function?

@Midoriya-w
Copy link
Copy Markdown
Contributor Author

hey @ShantKhatri Refactored the transaction callback to use a clearer control flow with a single explicit final return statement for better readability and maintainability.

Updated in the latest commit.

@Midoriya-w Midoriya-w requested a review from ShantKhatri May 27, 2026 17:44
@ShantKhatri
Copy link
Copy Markdown
Contributor

hey @ShantKhatri Refactored the transaction callback to use a clearer control flow with a single explicit final return statement for better readability and maintainability.

Updated in the latest commit.

Now we have so many unnecessary changes, that's not acceptable. If that repeats continuously, will need to close the PRs.

@Midoriya-w Midoriya-w force-pushed the fix/card-creation-race-condition branch from f6efa3d to fec20ff Compare May 27, 2026 17:57
@Midoriya-w Midoriya-w force-pushed the fix/card-creation-race-condition branch from fec20ff to e9e2fce Compare May 27, 2026 18:01
@Midoriya-w
Copy link
Copy Markdown
Contributor Author

hey @ShantKhatri Cleaned up the PR and removed unrelated changes. The branch now contains only the minimal transaction fix required for resolving the race condition issue.

Signed-off-by: dinesh <midoriya54378@gmail.com>
@ShantKhatri ShantKhatri merged commit f6ee844 into Dev-Card:main Jun 1, 2026
1 check failed
Harxhit added a commit that referenced this pull request Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gssoc:approved Required label for every approved PR. Gives the base +50 points and enables contribution tracking.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Race condition in card creation — concurrent requests can create multiple default cards

3 participants