MyBB Community Forums

Full Version: Race condition with poll voting loses counts
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Currently the forum will store and use a list of all poll votes and the counts for each option in the DB.
This essentially amounts to "look up the previous stored count of votes, increment by one, store back in database"
This is susceptible to a race condition when two or more people vote at the same time.

E.G. for a two-option poll with options Cats and Dogs
Bob: looks up the count of poll votes, it reads Cats=0, Dogs=0 from the database
Bob: selected Cats, so it sets Cats=1, Dogs=0
Jane: looks up the count of poll votes, it reads Cats=0, Dogs=0 from the database
Bob: saves Cats=1, Dogs=0 in the database
Jane: selected Dogs, so it sets Cats=0, Dogs=1
Jane: saves Cats=0, Dogs=1 in the database

Now the poll has two votes, but the counts for Cats=0 and Dogs=1

I will attempt to recreate it here with a little luck.

EDIT:
First shot, score.
[Image: 8RPiNyC.png]
After looking through the code on Github, it appears this is near line 928 on polls.php. What could be done differently is two queries to update it instead of one. The first one updates everything except the vote count. The other query can then be

$db->write_query("UPDATE " . TABLE_PREFIX . "polls SET `numvotes`=(SELECT COUNT(vid) FROM pollvotes WHERE pid=" . $poll['pid'] . ")");