rpc: Fix client crash when server drops connection
rpc: Fix client crash when server drops connection

Despite the comment stating virNetClientIncomingEvent handler should
never be called with either client->haveTheBuck or client->wantClose
set, there is a sequence of events that may lead to both booleans being
true when virNetClientIncomingEvent is called. However, when that
happens, we must not immediately close the socket as there are other
threads waiting for the buck and they would cause SIGSEGV once they are
woken up after the socket was closed. Another thing is we should clear
all remaining calls in the queue after closing the socket.

The situation that can lead to the crash involves three threads, one of
them running event loop and the other two calling libvirt APIs. The
event loop thread detects an event on client->sock and calls
virNetClientIncomingEvent handler. But before the handler gets a chance
to lock client, the other two threads (T1 and T2) start calling some
APIs. T1 gets the buck and detects EOF on client->sock while processing
its RPC call. Since T2 is waiting for its own call, T1 passes the buck
on to it and unlocks client. But before T2 gets the signal, the event
loop thread wakes up, does its job and closes client->sock. The crash
happens when T2 actually wakes up and tries to do its job using a closed
client->sock.

zhlcindy/libvirt-1.1.4-maintain,libvirt/libvirt,rlaager/libvirt,taget/libvirt,eskultety/libvirt,taget/libvirt,VenkatDatta/libvirt,elmarco/libvirt,rlaager/libvirt,agx/libvirt,elmarco/libvirt,elmarco/libvirt,zippy2/libvirt,jardasgit/libvirt,libvirt/libvirt,taget/libvirt,jfehlig/libvirt,elmarco/libvirt,zhlcindy/libvirt-1.1.4-maintain,jardasgit/libvirt,jfehlig/libvirt,crobinso/libvirt,eskultety/libvirt,zhlcindy/libvirt-1.1.4-maintain,libvirt/libvirt,rlaager/libvirt,fabianfreyer/libvirt,zhlcindy/libvirt-1.1.4-maintain,agx/libvirt,VenkatDatta/libvirt,agx/libvirt,crobinso/libvirt,crobinso/libvirt,fabianfreyer/libvirt,fabianfreyer/libvirt,jardasgit/libvirt,nertpinx/libvirt,eskultety/libvirt,eskultety/libvirt,cbosdo/libvirt,andreabolognani/libvirt,datto/libvirt,olafhering/libvirt,rlaager/libvirt,shugaoye/libvirt,datto/libvirt,taget/libvirt,datto/libvirt,zippy2/libvirt,VenkatDatta/libvirt,cbosdo/libvirt,agx/libvirt,datto/libvirt,jardasgit/libvirt,olafhering/libvirt,VenkatDatta/libvirt,crobinso/libvirt,datto/libvirt,jfehlig/libvirt,olafhering/libvirt,taget/libvirt,andreabolognani/libvirt,shugaoye/libvirt,olafhering/libvirt,shugaoye/libvirt,jardasgit/libvirt,VenkatDatta/libvirt,agx/libvirt,cbosdo/libvirt,jfehlig/libvirt,shugaoye/libvirt,cbosdo/libvirt,elmarco/libvirt,shugaoye/libvirt,fabianfreyer/libvirt,zippy2/libvirt,andreabolognani/libvirt,nertpinx/libvirt,cbosdo/libvirt,andreabolognani/libvirt,nertpinx/libvirt,libvirt/libvirt,rlaager/libvirt,nertpinx/libvirt,zippy2/libvirt,nertpinx/libvirt,andreabolognani/libvirt,fabianfreyer/libvirt,eskultety/libvirt,zhlcindy/libvirt-1.1.4-maintain