Skip to content

Fix transaction atomicity and race conditions in quota management#187

Open
Copilot wants to merge 3 commits intofix/quota-not-workfrom
copilot/sub-pr-186
Open

Fix transaction atomicity and race conditions in quota management#187
Copilot wants to merge 3 commits intofix/quota-not-workfrom
copilot/sub-pr-186

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 28, 2025

Addresses critical concurrency and consistency issues in the quota management system introduced in PR #186.

Changes

  • Transaction atomicity: Wrapped signal-based quota resets in transaction.atomic() to prevent partial updates across users
  • Race condition handling: Replaced select_for_update().get_or_create() with proper get()create()IntegrityErrorget() pattern to handle concurrent quota record creation
  • Submission atomicity: Moved submission.save() inside transaction blocks to ensure quota decrements and submission creation succeed or fail together
  • Filtering consistency: Standardized all assignment_id filtering to use assignment_id=None instead of mixed assignment_id__isnull=True

Example

Before (race condition):

# Two concurrent requests could both call get_or_create
# select_for_update only locks the SELECT, not the INSERT
quota, created = UserProblemQuota.objects.select_for_update().get_or_create(...)

After (race-safe):

try:
    quota = UserProblemQuota.objects.select_for_update().get(...)
except UserProblemQuota.DoesNotExist:
    try:
        quota = UserProblemQuota.objects.create(...)
    except IntegrityError:
        # Another request created it, fetch with lock
        quota = UserProblemQuota.objects.select_for_update().get(...)

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits December 28, 2025 19:04
Co-authored-by: happylittle7 <7501374+happylittle7@users.noreply.github.com>
Co-authored-by: happylittle7 <7501374+happylittle7@users.noreply.github.com>
Copilot AI changed the title [WIP] Add automatic user quota management for problem submissions Fix transaction atomicity and race conditions in quota management Dec 28, 2025
Copilot AI requested a review from happylittle7 December 28, 2025 19:07
@happylittle7 happylittle7 marked this pull request as ready for review December 28, 2025 19:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants