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 1 commit into from Aug 3, 2019


Copy link

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 bug label Aug 2, 2019


jfly approved these changes Aug 3, 2019

Copy link

left a comment

LGTM! Nice catch :)

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


This comment has been minimized.

Copy link
Member Author

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

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
coverage/coveralls Coverage increased (+0.0002%) to 96.269%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
2 participants
You can’t perform that action at this time.