Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race condition when updating competition tabs order #4395

Merged
merged 1 commit into from Aug 3, 2019
Merged

Fix race condition when updating competition tabs order #4395

merged 1 commit into from Aug 3, 2019

Conversation

jonatanklosko
Copy link
Member

@jonatanklosko jonatanklosko commented Aug 2, 2019

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 SQL UPDATE queries log I found out this race condition:

tab display order
A 1 (action: down)
B 2
C 3 (action: up)

If these actions happen simultaneously then tabs' desired state is A | 2 and B | 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.:

  1. A = NULL
  2. B = 1
  3. 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.

@jonatanklosko jonatanklosko added the TYPE: bug Bug reported by a stakeholder label Aug 2, 2019
Copy link
Contributor

@jfly jfly left a comment

Choose a reason for hiding this comment

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

LGTM! Nice catch :)

Do we enforce uniqueness of tab order via a mysql constraint?

@jonatanklosko
Copy link
Member Author

jonatanklosko commented Aug 3, 2019

We do, that's one of the updates must've failed and left NULL value.

UNIQUE KEY `index_competition_tabs_on_display_order_and_competition_id` (`display_order`,`competition_id`)

@jonatanklosko jonatanklosko merged commit b6aaffa into thewca:master Aug 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TYPE: bug Bug reported by a stakeholder
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants