Fix race condition when updating competition tabs order #4395
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.
Apparently there was competition tab with
NULL
display_order
in our database, which shouldn't be possible. Took me quite some clicking to reproduce this on the staging (as we run multiple workers there), but eventually I managed to get to that state. After analyzing SQLUPDATE
queries log I found out this race condition:If these actions happen simultaneously then tabs' desired state is
A | 2
andB | 2
, so one of the updates must fail. The thing is that to preserve display order uniqueness an update is done in three steps, e.g.:A = NULL
B = 1
A = 2
So if the step 3. fails, we end up with A being
NULL
.Wrapping these 3 steps in a transaction should solve the issue.