Fix for SetOwner race condition

We are a pretty high volume RT user. Our customer service
people need to chug through so many tickets that I even
had to customize RT so that when a ticket is resolved the
next ticket in the queue is automatically taken and
displayed.

Several times in one week 2 people we able to Take the
same ticket. What is up with that?

Armed with 2 debugger sessions I stepped through the
code for SetOwner and was able to figure out that
the decision to allow a user to take a ticket was
happening outside of the critical section of code. This
means that the decision to allow a user to take a ticket
may be invalidated between the time the decision was
made and the time the ticket was actually taken.

By moving around the locking and a few other tricks
I was able to eliminate the race condition.
We have been using this new code for about 2 weeks with
no problems.

Patch is attached.

-Todd

set_owner_race_condition.patch (5.81 KB)