I am facing a potential race condition in a web application:
submissions = cache.get('user_data')
submissions[user_id] = submission
submissions = cache.update('user_data', submissions)
if len(submissions) == some_number:
...
The logic is simple: we first extract the shared dictionary stored in the cache of the web server, add the send (delivered by each request to the server) to its local copy, and then update the cached copy, replacing it with an updated local copy. Finally, we do something else if we get a certain amount of data. note that
submissions = cache.update('user_data', submissions)
will return the last copy of the dictionary from the cache, i.e. updated.
Since the server can serve several requests at the same time (each in its own thread), and all these threads access the general dictionary in the cache, as described above, thereby creating potential race conditions.
, - , , , . .