Poll: Race
You do not have permission to vote in this poll.
Cats
100.00%
2 100.00%
Dogs
0%
0 0%
Total 2 vote(s) 100%
* You voted for this item. [Show Results]

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Race condition with poll voting loses counts
#1
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]
#2
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'] . ")");


Forum Jump:


Users browsing this thread: 1 Guest(s)