[libvirt] Error connecting to virConnectOpen("qemu:///system"); after 20 times

Daniel P. Berrange berrange at redhat.com
Tue Jan 22 11:42:47 UTC 2013


On Tue, Jan 22, 2013 at 11:31:27AM +0000, Pankaj Rawat wrote:
> Hi,
> I am using libvirt api to create an app, I am facing a problem.
> I can connect to connection object 20 number of times in programm
> after that it gives connection object null.

By default libvirtd has a limit of 20 concurrent active connections/

> Here is the sample code.
> 
> Normally code should run 1000 times but it only runs 20 times
> Filename: list.cpp
> 
> #include<pthread.h>
> #include<stdlib.h>
> #include<stdio.h>
> #include<malloc.h>
> #include <iostream>
> #include<string.h>
> #include <libvirt/libvirt.h>
> #include <libvirt/virterror.h>
> static int count;
>     static void customErrorFunc(void *userdata, virErrorPtr err)
>     {
>     }
> 
> int main()
> {
>     virConnectPtr conn;
> 
>   for ( int i=0; i< 1000;i++)
>   {
>      conn = virConnectOpen("qemu:///system");
>      if (conn == NULL) {
>         fprintf(stderr, "Failed to open connection to qemu:///system\n");
>         printf("Failed to open connection to qemu:///system\n");
>         virConnectClose(conn);
>         return NULL;
>         }
>         printf("Hello %d\n",i);
>         virError err;
>         virSetErrorFunc(NULL, customErrorFunc);
>         int j;
>         int numDomains1,numDomains2,numDomains;
>         int *activeDomains;
>         numDomains1 = virConnectNumOfDomains(conn);
>         activeDomains = (int*)malloc(sizeof(int) * numDomains1);
>         numDomains1 = virConnectListDomains(conn, activeDomains, numDomains1);
>         for (int j = 0 ; j < numDomains1 ; j++)
>         {
>                 virDomainPtr dom;
>                 dom = virDomainLookupByID(conn,activeDomains[j]);
>                 free(dom);

You should be using virDomainFree(dom), not 'free(dom)' The latter
is leaking memory & loosing the reference on virConnectPtr causing
the connection to be left open. Hence you are hitting the 20 conn
limit.

>         }
>         free(activeDomains);
>         count--;
>         virConnectClose(conn);
>    }
> }

Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|




More information about the libvir-list mailing list