]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
Fix race in ref counting when handling RPC jobs
authorDaniel P. Berrange <berrange@redhat.com>
Tue, 26 Jul 2011 14:49:15 +0000 (15:49 +0100)
committerDaniel P. Berrange <berrange@redhat.com>
Tue, 26 Jul 2011 15:22:32 +0000 (16:22 +0100)
When an incoming RPC message is ready for processing,

  virNetServerClientDispatchRead()

will invoke the 'dispatchFunc' callback. This is set to

  virNetServerDispatchNewMessage

This function puts the message + client in a queue for processing by the thread
pool. The thread pool worker function is

  virNetServerHandleJob

The first thing this does is acquire an extra reference on the 'client'.

Unfortunately, between the time the message+client are put on the thread pool
queue, and the time the worker runs, the client object may have had its last
reference removed.

We clearly need to add the reference to the client object before putting the
client on the processing queue

* src/rpc/virnetserverclient.c: Add a reference to the client when
  invoking the dispatch function
* src/rpc/virnetserver.c: Don't acquire a reference to the client
  when in the worker thread

src/rpc/virnetserver.c
src/rpc/virnetserverclient.c

index 2b9dd4d695f8efaf10f4cc3d449e37c8e4cfbc0d..c93c3f8a39a06705d29dd3be8335b7c735948d46 100644 (file)
@@ -131,8 +131,6 @@ static void virNetServerHandleJob(void *jobOpaque, void *opaque)
     virNetServerProgramPtr prog = NULL;
     size_t i;
 
-    virNetServerClientRef(job->client);
-
     virNetServerLock(srv);
     VIR_DEBUG("server=%p client=%p message=%p",
               srv, job->client, job->msg);
index 317d59cf559ab92bfe302a4459f5e708eb3d40e4..3c0dba8d65f9f20e7a454a852ef082c1fbf8fea9 100644 (file)
@@ -763,10 +763,12 @@ readmore:
 
         /* Send off to for normal dispatch to workers */
         if (msg) {
+            client->refs++;
             if (!client->dispatchFunc ||
                 client->dispatchFunc(client, msg, client->dispatchOpaque) < 0) {
                 virNetMessageFree(msg);
                 client->wantClose = true;
+                client->refs--;
                 return;
             }
         }