Fix race condition in public submission toggling #4831
+128
−47
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.
Fixes #4830
Description
This PR fixes a race condition where multiple submissions could become public simultaneously when [is_restricted_to_select_one_submission] EvalAI/tests/unit/jobs/test_views.py:1533:4-1547:66) is enabled.
Previously, concurrent requests could bypass the check that ensures only one submission is public at a time.
Changes
[apps/jobs/views.py]
Added
transaction.atomic()andselect_for_update()in [challenge_submission] and [change_submission_data_and_visibility]his ensures that the database rows are locked during the read-update operation, preventing other threads from modifying the state concurrently.
i have also added a regression test that simulates [tests/unit/jobs/test_bug_race_condition.py] the race condition using threading and mocks to verify the fix works as expected.
Verification
Confirmed that concurrent requests now correctly result in only one public submission.