This blog is the solution to the earlier stated problem : Solve the Deadlock
: Read the error message carefully. Its more helpful than we tend to think.
The error tells us that the problem happened during the update of the ‘person’ object.
So, let us start looking at the lines that update the ‘person’ object. A bit of digging gives us … line #28 and #37. That is …
person_queryset.update(**kwargs)
And
Person.objects.update(session=Subquery(mapping_queryset))
Since these are the only lines that update ‘person’ object. So, we can be (reasonably) sure that these lines are the cause of the problem
If I knew nothing about the python programming language or the framework used. Then, I would think that line #28 updates a sub-set of ‘person’ objects.
person_queryset.update(**kwargs)
And line #37, updates another subset of ‘person’ objects.
Person.objects.update(session=Subquery(mapping_queryset))
It might be possible for each DB update to be going via a different worker thread. The threads might be locking on different tables/ rows … causing the deadlock.
Turns out – this in fact was the problem. !
But, the method annotation did cause me some confusion. Methods marked transactional usually use a single DB transaction for DB updates.
However, each library/ framework might implement transactional behavior differently. (I was discounting this fact )
: Each library and framework implements a functionality differently.
The method updated a fixed number of ‘persons’. And did NOT need to update different sets.
Knowing this, line #37 was re-written from:
Person.objects.update(session=Subquery(mapping_queryset))
to :
person_queryset.update(session=Subquery(mapping_queryset))
Voila ! problem solved.
Shoutout to the guy who figured this out. You rock !
1 thought on “Thread Deadlock : Solved”
Comments are closed.