pculture/unisubs — pull request #1802 by bendk (2014-10-22 15:47:02+00:00)
Title: Hopefully fix a deadlock that I've been seeing fairly often

The deadlock happens like this:
- We get a request to add a version at the same time as a request to
regain the lock on the subtitles

- In the request to add the version, we SELECT the language and get
a read lock on it.
- In the request to regain the lock, we UPDATE the language and get
a wrote lock on it (but are now waiting on the read lock to
finish)
- In the request to add the version, we UPDATE the language (because
we are changing subtitles_complete). This is now waiting on the
other request's write lock -- and we now have a deadlock.

I think that's the order of events at least. I was able to simulate
something like that and did in fact get a deadlock error.

The solution is to use SELECT FOR UPDATE to get a write lock before
doing any work. This makes the regain-lock request wait to get its
writelock and prevents the deadlock.