Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.6 transactions to avoid race conditions

I'm trying to use Django 1.6 transactions to avoid race conditions on a game I'm developing. The game server has one simple goal: to pair two players.

My current approach is:

  1. User wants to play
  2. The server checks if there is anyone else waiting to play.
    1. If there is not, it creates a GameConnection object (that has a unique identifier - uuid4).
    2. If there is, it gets the GameConnection identifier and deletes the GameConnection.

This is the code:

# data['nickname'] = user's choice
games = GameConnection.objects.all()
if not games:
    game = GameConnection.objects.create(connection=unicode(uuid.uuid4()))
    game.nick1 = data["nickname"]
    game.save()

    response = HttpResponse(json.dumps({'connectionId': game.connection, 'whoAmI': 1,  'nick1': game.nick1, 'nick2': ""}))
else:
    game = games[0]
    conn = game.connection
    nick1 = game.nick1
    nick2 = data["nickname"]
    game.delete()
    response = HttpResponse(json.dumps({'connectionId': conn, 'whoAmI': 2,  'nick1': nick1, 'nick2': nick2}))

return response

Obviously there is a race condition on the code above. As this code is not atomic, it can happen that:

  • A checks for game connections. Finds none.
  • A creates a game connection.
  • B checks for game connections. Finds one (A).
  • C checks for game connections. Finds one (A).
  • B gets A's connection identifier and starts a game.
  • C gets A's connection identifier and starts a game.

I tried do but this whole block under with transaction.atomic():, or to use the @transaction.atomic decorator. But still, I am able to reproduce the race condition.

I am sure there is something about the transaction dynamics I am missing here. Can anyone shed a light?

like image 650
Alexandre Cisneiros Avatar asked May 09 '14 14:05

Alexandre Cisneiros


People also ask

Why do we use transactions in Django?

See below for details. Django uses transactions or savepoints automatically to guarantee the integrity of ORM operations that require multiple queries, especially delete () and update () queries. Django’s TestCase class also wraps each test in a transaction for performance reasons.

Is Django autocommit?

If your MySQL setup does not support transactions, then Django will always function in autocommit mode: statements will be executed and committed as soon as they’re called. If your MySQL setup does support transactions, Django will handle transactions as explained in this document.

Why does atomic() not handle PostgreSQL errors in Django?

This problem cannot occur in Django’s default mode and atomic () handles it automatically. Inside a transaction, when a call to a PostgreSQL cursor raises an exception (typically IntegrityError ), all subsequent SQL in the same transaction will fail with the error “current transaction is aborted, queries ignored until end of transaction block”.

What happens when you exit an atomic block in Django?

When exiting an atomic block, Django looks at whether it’s exited normally or with an exception to determine whether to commit or roll back. If you catch and handle exceptions inside an atomic block, you may hide from Django the fact that a problem has happened. This can result in unexpected behavior.


1 Answers

@Sai is on track... the key is that the lock/mutex won't occur until a write (or delete). As coded, there will always be a time between "discovery" (read) of the pending connection and "claim" (write/lock) of the pending connection, with no way to know that a connection is in the process of being claimed.

If you are using PostgreSQL (pretty sure MySQL supports it, too), you can force the lock with "select for update", which will prevent another request from getting the same row until the transaction completes:

game = GameConnection.objects.all()[:1].select_for_update()
if game:
    #do something, update, delete, etc.
else:
    #create

Final note - consider something other than all() to be explicit about which game might be picked up (e.g., order by a "created" timestamp or something). Hope that helps.

like image 166
bimsapi Avatar answered Sep 20 '22 05:09

bimsapi